1. Connecting the MONGO database
New Mongo ("localhost", 27017=mongo.getdb ("test"= Db.getcollection ("Customer");
2. Query Collection All Documents
= Collections.find (); while (Dbcursor.hasnext ()) { System.out.println (Dbcursor.next (). Get ("name")); } mongo.close ();
3. Delete the collection documents
// Delete by ID (ID is a special type) New Basicdbobject ("_id",new ObjectId ("321321fdf21312")); // Delete new Basicdbobject ("name", "Zhangsan") according to other fields ; Collections.remove (o); Mongo.close ();
4. Inserting a document into the collection
New Basicdbobject (); C.put ("name", "Zhangsan"); C.put ("Age", "the"); Collections.insert (c); Mongo.close ();
5. Updating documents in the collection
New Basicdbobject ("_id",new ObjectId ("321321fdf21312")); = (basicdbobject) collections.findone (query); Object.put ("name", "Wangwu"); Collections.update (Query, object); Mongo.close ();
The Java operation of MongoDB