List of usage examples for com.mongodb.util JSON parse
public static Object parse(final String jsonString)
Parses a JSON string and returns a corresponding Java object.
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * insert document//from w w w . jav a 2s .c o m * * @param userDB * @param colName * @param jsonStr * @throws Exception */ public static void insertDocument(UserDBDAO userDB, String colName, String jsonStr) throws Exception { DBObject dbObject = (DBObject) JSON.parse(jsonStr); DBCollection collection = findCollection(userDB, colName); WriteResult wr = collection.insert(dbObject); // ? ?? ?(????????????????????????????????) // if(logger.isDebugEnabled()) { // logger.debug( "[writer document]" + wr.toString() ); // logger.debug( wr.getError() ); // logger.debug("[n]" + wr.getN() ); // } }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * create index/*from w w w . java 2 s . com*/ * * @param userDB * @param colName * @param strIndexName * @param jsonStr * @param unique */ public static void crateIndex(UserDBDAO userDB, String colName, String strIndexName, String jsonStr, boolean unique) throws Exception { DBObject dbObject = (DBObject) JSON.parse(jsonStr); DBCollection collection = findCollection(userDB, colName); collection.ensureIndex(dbObject, strIndexName, unique); }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * save java script//from w ww. j av a 2s.c o m * * @param userDB * @param javaScriptDAO * @throws Exception */ public static void insertJavaScript(UserDBDAO userDB, MongoDBServerSideJavaScriptDAO javaScriptDAO) throws Exception { DBObject dbObject = (DBObject) JSON .parse("{'_id':'" + javaScriptDAO.getName() + "', 'value':'" + javaScriptDAO.getContent() + "'}"); findCollection(userDB, "system.js").save(dbObject); }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * update java script//from ww w. j av a 2 s . c o m * * @param userDB * @param _id * @param content * @throws Exception */ public static void updateJavaScript(UserDBDAO userDB, String _id, String content) throws Exception { DBObject dbFindObject = (DBObject) JSON.parse("{'_id':'" + _id + "'}"); DBObject dbUpdateObject = (DBObject) JSON.parse("{'_id':'" + _id + "', 'value':'" + content + "'}"); findCollection(userDB, "system.js").findAndModify(dbFindObject, dbUpdateObject); }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * remove javascript//from ww w. j ava2 s. c om * * @param userDB * @param _id * @throws Exception */ public static void deleteJavaScirpt(UserDBDAO userDB, String _id) throws Exception { DBObject dbFindObject = (DBObject) JSON.parse("{'_id':'" + _id + "'}"); findCollection(userDB, "system.js").remove(dbFindObject); }
From source file:com.hangum.tadpole.mongodb.core.test.ConvertJsonToDBObject.java
License:Open Source License
public static void main(String[] args) throws Exception { ConAndAuthentication testMongoCls = new ConAndAuthentication(); Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port); DB db = mongo.getDB("test"); DBCollection myColl = db.getCollection("objectInsert"); // //from w ww .ja va2 s . c o m // DBObject dbObject = new BasicDBObject(); // dbObject.put("aa", 1); // dbObject.put("bb", "33"); DBObject dbObject = (DBObject) JSON.parse("{'rental_id':1, 'inventory_id':367}"); myColl.insert(dbObject); DBCursor cursorDoc = myColl.find(); while (cursorDoc.hasNext()) { System.out.println(cursorDoc.next()); } System.out.println("Done"); }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestGroup.java
License:Open Source License
/** * @param args/*from w w w. ja v a 2 s .co m*/ */ public static void main(String[] args) throws Exception { ConAndAuthentication testMongoCls = new ConAndAuthentication(); Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port); DB db = mongo.getDB("test"); DBCollection coll = db.getCollection("group"); DBObject dbObjectKey = (DBObject) JSON.parse(key); DBObject dbObjectInitial = (DBObject) JSON.parse(initial); DBObject dbObjectCondition = (DBObject) JSON.parse(condition); DBObject resultDBObject = coll.group(dbObjectKey, dbObjectCondition, dbObjectInitial, reduce, finalizer); System.out.println(resultDBObject); mongo.close(); try { Thread.sleep(1); } catch (Exception e) { } }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestNewCollection.java
License:Open Source License
/** * @param args//from ww w . j ava2 s . c o m */ public static void main(String[] args) throws Exception { ConAndAuthentication testMongoCls = new ConAndAuthentication(); Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port); DB db = mongo.getDB("test"); DBObject dbObject = (DBObject) JSON.parse("{capped:true, size:100000}"); DBCollection dbColl = db.createCollection("newCollection3", dbObject); // db.cappedcoll.insert({"names": {"First": "Gonza", "Last": "Vieira"}}); // db.cappedcoll.update({"names.First": "Gonza"},{$unset: {"_id":1}}); mongo.close(); }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestProfilling.java
License:Open Source License
/** * @param args// www .ja v a 2 s .c om */ public static void main(String[] args) throws Exception { ConAndAuthentication testMongoCls = new ConAndAuthentication(); Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port); DB db = mongo.getDB("test"); // ?? System.out.println("####[profilling ]######################################################"); CommandResult cr = db.command(new BasicDBObject("profile", 0)); System.out.println("[ok]" + cr.ok()); if (!cr.ok()) System.out.println("[Exception ]" + cr.getException().toString()); System.out.println("[toString]" + JSONUtil.getPretty(cr.toString())); System.out.println("[size]" + cr.size()); System.out.println("####[profilling ]######################################################"); // ? System.out.println( "####[profilling collections ]######################################################"); if (db.collectionExists("system.profile")) { DBCollection profileColl = db.getCollection("system.profile"); profileColl.drop(); } System.out.println( "####[profilling collections ]######################################################"); // ? // system.profile collection ? System.out.println( "####[profilling collections ? ]######################################################"); DBObject dbObject = (DBObject) JSON.parse("{capped:true, size:8000000}"); DBCollection dbColl = db.createCollection("system.profile", dbObject); BasicDBObject newProfileColl = (BasicDBObject) dbColl.getStats().copy(); System.out.println( "####[profilling collections ? ]######################################################"); // system.profile collection ? System.out.println("####[profilling ]######################################################"); cr = db.command(new BasicDBObject("profile", 2)); System.out.println("[ok]" + cr.ok()); if (!cr.ok()) System.out.println("[Exception ]" + cr.getException().toString()); System.out.println("[toString]" + JSONUtil.getPretty(cr.toString())); System.out.println("[size]" + cr.size()); System.out.println("####[profilling ]######################################################"); // //####################################################################################################### // //####################################################################################################### // //####################################################################################################### System.out.println("####[start profilling result]######################################################"); DBCollection myColl = db.getCollection("system.profile"); BasicDBObject query = new BasicDBObject(); query.put("millis", new BasicDBObject("$gt", 4)); DBCursor myCursor = myColl.find(); while (myCursor.hasNext()) { DBObject dbObj = myCursor.next(); System.out.println(dbObj.get("ts") + " - " + dbObj.get("ns") + " - " + dbObj.get("nscanned") + "/" + dbObj.get("nreturned") + " millis :" + dbObj.get("millis")); } System.out.println("####[end profilling result]######################################################"); mongo.close(); }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestReferenceCollection.java
License:Open Source License
/** * @param args//www . j a v a 2 s .c o m */ public static void main(String[] args) throws Exception { ConAndAuthentication testMongoCls = new ConAndAuthentication(); Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port); DB db = mongo.getDB("test"); DBObject colInformation = (DBObject) JSON.parse("{capped:true, size:100000}"); DBCollection ref1Coll = db.getCollection(REF_1); if (ref1Coll != null) ref1Coll.drop(); ref1Coll = db.createCollection(REF_1, colInformation); DBObject dbObjRef1 = (DBObject) JSON.parse("{ 'name' : 'cho'}");//"{'names': {'First': 'Gonza', 'Last': 'Vieira'}}"); WriteResult wr = ref1Coll.insert(dbObjRef1); DBObject retDBObj = ref1Coll.findOne(); createRef1Collection(db, retDBObj.get("_id")); mongo.close(); }