One, the MongoDB client
MongoDB provides clients in many languages, as well as Java clients. With these clients, we can easily manipulate mongodb in a way that writes code. Here is an example using a Java client. Using a Java client first needs to download the JMONGODB database Java driver. The source address of the drive is: https://github.com/mongodb/mongo-java-driver/. Here the project is built using MAVEN, and the dependencies that need to be added are as follows:
<Dependency><groupId>Org.mongodb</groupId><Artifactid>Mongo-java-driver</Artifactid><version>2.12.4</version></Dependency>
Ii. examples
1. Add a document
DB db =NULL; @Before Public voidGetdb ()throwsunknownhostexception {mongoclient mongoclient=Newmongoclient (); //mongoclient = new Mongoclient ("localhost"); //mongoclient = new Mongoclient ("localhost", 27017); //mongoclient = new Mongoclient (arrays.aslist ("localhost", 27017), New serveraddress ("localhost", 27018), New serveraddress ("localhost", 27019)); //you need to verify the following//mongocredential credential = mongocredential.createmongocrcredential (userName, database, password); //mongoclient mongoclient = new Mongoclient (new ServerAddress (), Arrays.aslist (credential));Mongoclient.setwriteconcern (writeconcern.journaled); System.out.println ("===========databasename============="); for(String s:mongoclient.getdatabasenames ()) {System.out.println (s); } DB= Mongoclient.getdb ("TestDB"); } @Test Public voidadddoc () {dbcollection coll= Db.getcollection ("Person"); Basicdbobject Doc=NewBasicdbobject ("name", "Zhang San")//. Append ("Age", 20)//. Append ("Gender", "Mail")//. Append ("Brithday",NewDate ())//. Append ("info", NewBasicdbobject ("location_x", 203). Append ("Location_y", 303)); Writeresult result=Coll.insert (DOC); SYSTEM.OUT.PRINTLN (result); for(inti = 0; I < 10; i++) {Basicdbobject document=NewBasicdbobject ("name", "Zhangsan" + i)//. Append ("Age", + i)//. Append ("Brithday",NewDate ()); Writeresult result2=Coll.insert (document); System.out.println (RESULT2); } }
2. Querying documents
@Test Public voidFinddoc () {dbcollection coll= Db.getcollection ("Person"); DBObject Person= Coll.findone ();//gets the first document in a collectionSystem.out.println ("===============findone======================"); SYSTEM.OUT.PRINTLN (person); //query by condition; Db.person.find ({age:18});DBObject query =NewBasicdbobject ("Age", 18); DBObject Person2=coll.findone (query); System.out.println (Person2); System.out. println ("===============findbycondtion======================"); //Age less than; Db.person.find ({age:{$lt:)});query =NewBasicdbobject ("Age",NewBasicdbobject ("$lt", 25)); Dbcursor cursor=coll.find (query); Try { while(Cursor.hasnext ()) {System.out.println (Cursor.next ()); } } finally{cursor.close (); } System.out.println ("===============findall======================"); //query All and follow the age ascending; Db.person.find (). Sort ({age:1});cursor = Coll.find (). Sort (NewBasicdbobject ("Age",-1));//1 Ascending,-1 descending Try { while(Cursor.hasnext ()) {System.out.println (Cursor.next ()); } } finally{cursor.close (); } }
3. Update the documentation
@Test Public voidUpdatedoc () {dbcollection coll= Db.getcollection ("Person"); Basicdbobject Query=NewBasicdbobject ("name", "Zhang San"); DBObject Update=coll.findone (query); Update.put ("Info",NewBasicdbobject ("Location_x", 1999). Append ("Location_y", 2999)); //Method OneDBObject person = coll.findandmodify (query, update);//returns the Document object before it was changedSystem.out.println (person); //Method TwoUpdate.put ("Info",NewBasicdbobject ("location_x", 19998). Append ("Location_y", 29998)); Writeresult result=coll.update (query, update); SYSTEM.OUT.PRINTLN (result); }
4. Deleting documents
@Test Public voidDeletedoc () {dbcollection coll= Db.getcollection ("Person"); Basicdbobject Query=NewBasicdbobject ("name", "Zhangsan8"); //Method Onequery =NewBasicdbobject ("name", "Zhangsan9"); DBObject Person= Coll.findandremove (query);//The deleted Document object is returnedSystem.out.println (person); //Method Twoquery =NewBasicdbobject ("name", "Zhangsan9"); Writeresult result=coll.remove (query); SYSTEM.OUT.PRINTLN (result); }
5, set related operations
@Test Public voidtestcollection () {Set<String> colls = Db.getcollectionnames ();//all collections in the database name for(String s:colls) {System.out.println (s); } System.out.println ("======================================="); //Create a collection and add the document to the collectionDbcollection coll = db.createcollection ("Testcollection", NewBasicdbobject ("capped",true). Append ("Size", 1048576)); System.out.println (coll); Coll= Db.getcollection ("Testcollection"); //indexes in the collectionlist<dbobject> list =Coll.getindexinfo (); for(DBObject o:list) {System.out.println (o); } coll.drop (); }
Mongodb--java operation