1. Download the MongoDB jar package and introduce it to the classpath of the project: mongodb2.5 driver package Download
If you use a MAVEN project, the latest dependencies are as follows:
<Dependency> <groupId>Org.mongodb</groupId> <Artifactid>Mongodb-driver</Artifactid> <version>3.2.0</version></Dependency>
It is important to note that if the project is not a MAVEN project but by introducing a jar package, then the mongdb3. After X , in addition to introducing Mongo-java-driver, there is a need to manually introduce the two jar packages of Bson and Mongo-java-core, because driver relies on both packages, and the version is best consistent. Otherwise you will be reported a variety of errors can not find the jar package. And if the project is MAVEN project then only need to introduce MongoDB driver dependency, maven will automatically download Bson and Mongo-java-core dependent jar package. As shown below:
2, link the MongoDB server, and select the database to establish a MONGODB connection, you just specify the database to connect to the can. This database does not necessarily exist, and if it does not exist, MongoDB will set up this library for you first. Also, you can specify the network address and port to connect to at the time of connection:
Mongo m =NewMongo ();//orMongo m =NewMongo ("localhost" );//orMongo m =NewMongo ("localhost", 27017 );//or, to connect to a replica set, supply a seed list of membersMongo m =NewMongo (Arrays.aslist (NewServerAddress ("localhost", 27017), NewServerAddress ("localhost", 27018), NewServerAddress ("localhost", 27019))); DB DB=M.getdb ("MyDB");
3. Security verification (optional)
boolean auth = db.authenticate (userName, password);
4. Get the list of collections. Each database has 0 or more collections, and you can get a list of them when needed:
Set<string> colls = db.getcollectionnames (); for (String s:colls) { System.out.println (s);}
5, get a set. To get a particular collection, you can specify the name of the collection and use the GetCollection () method:
Dbcollection coll = db.getcollection ("blog");
6. Inserting documents
MongoDB stores the JSON-formatted document, and the simplest class in Java that represents this data format is map. The basicdbobject provided in MongoDB Java driver is a map (which inherits from the Linkedhashmap and implements the DBObject interface), which converts the data in the map to the Bson format for transmission to MongoDB.
New Basicdbobject (); Doc.put ("name", "MongoDB"); Doc.put ("type", "Database"); Doc.put ("Count", 1); New Basicdbobject (); Info.put ("x", 203); Info.put ("y", 102); Doc.put ("Info", info); Coll.insert (DOC);
Each inserted document in MongoDB produces a unique identifier of _id. When Coll.insert (DOC) is called, driver checks if there is a _id field, and if not, automatically generates a Objectid instance as the _id value, which is encoded by 4 parts: The current time, machine ID, process number, and self-increment integer. The Insert function also supports inserting a document list: Insert (List list)
7. Enquiry
The find function is a collection of queries, and the dbcursor that it returns is an iterator to the dbobject. The following code:
New Basicdbobject (); Query.put ("i",= coll.find (query); Try while (Cursor.hasnext ()) { System.out.println (Cursor.next ()); finally { cursor.close ();}
8. Building an Index
Create an index statement such as: Coll.createindex (New Basicdbobject ("I", 1)); , where I represents the field to index, and 1 indicates ascending (-1 means descending). As you can see, dbobject becomes a common structure representation for Java clients. View indexes using the Dbcollection.getindexinfo () function.
Simple use of the Java Client connection to the MongoDB database