Summary:
1. [MongoDB] Installing MongoDB
2. [Mongodb]mongo Basic use:
3. [Advantages and disadvantages of mongodb]mongodb and comparison with relational database
4. [Mongodb]mongodb and Java use crud together
It is also fairly simple to use, because MongoDB is a database of class files, so it is very convenient to operate.
First download the corresponding jar package, here I directly use MAVEN automatically, the following is the configuration in the Pom file:
<dependency>
<groupId>junit</groupId>
<artifactid>junit</artifactid>
<version>4.8.2</version>
</Dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactid>MONGO-java-driver</artifactid>
<version>2.7.2</version>
</Dependency>
A MongoDB jar package and a junit jar package, you can
You can then start writing code that is called directly in the main method of a Java class:
Public static void main (string[] args) throws exception{
// Create a Mongo instances, using local "127.0.0.1" by default, 27017
Mongo mongo1j = new Mongo ();
// Use ip+ port number creation
Mongo Mongo = new Mongo ("127.0.0.1", 27017);
// get the names of all databases
For (String name:mongo.getDatabaseNames ()) {
System. out. println ("DbName:" + name);
}
// get a database for subsequent operations on the database
DB db = Mongo.getdb ("student");
For (String name:db.getCollectionNames ()) {
System. out. println ("CollectionName:" + name);
}
// get the corresponding set
Dbcollection students = db.getcollection ("student");
// querying data in all collections
dbcursor cur = students.find ();
// iterate through all the contents of the collection
While (Cur.hasnext ()) {
System. out. println (Cur.next ());
}
System. out. println ("Cur.count ():" + Cur.count ());
System. out. println ("Cur.getcursorid ():" + Cur.getcursorid ());
System. out. println ("json.serialize ():" + JSON. Serialize(cur));
//DELETE Operation
Students.remove (new basicdbobject ("name", "YANGJX"));
System. out. println ("Cur.count ():" + Cur.count ());
//ADD Operation
DBObject student = new basicdbobject ();
Student.put ("name", "YANGJX");
Student.put ("Age", "the");
Students.save (student);
System. out. println ("Cur.count ():" + Cur.count ());
//UPDATE Operation
DBObject updateobj = new basicdbobject ();
Updateobj.put ("Age", +);
Updateobj.put ("name", "Zhanggy");
Updateobj.put ("Extra", "Markhere");
Students.update (new basicdbobject ("Age", "updateobj");
System. out. println (JSON. Serialize(Students.find (new basicdbobject ("name", "Zhanggy" )));
}
After the execution of the above code is completed, it will be printed as follows:
dbname:local
dbname:student
Collectionname:colname
collectionname:student
collectionName:system.indexes
{"_id": {"$oid": "555ae83a09668180fadccdeb"}, "Age":
{"_id": {"$oid": "555ae84009668180fadccdec"}, "name": "Zhangyc", "Age": 29.0, "Sex": true}
{"_id": {"$oid": "555ae84709668180fadccded"}, "name": "Zhangyyq", "Age": 30.0, "Sex": true}
{"_id": {"$oid": "555aebdb09b332c5a188b620"}, "passwd": "1234"}
{"_id": {"$oid": "555d80d7b7a5042665b195ae"}, "name": "Yangjx", "Age" :
Cur.count (): 5
Cur.getcursorid (): 0
json.serialize (): [{"_id": {"$oid": "555ae83a09668180fadccdeb"}, "Age": +}, {"_id": {"$oid": "555ae840096 68180fadccdec "}," name ":" Zhangyc "," Age ": 29.0," Sex ": true}, {" _id ": {" $oid ":" 555ae84709668180fadccded "}," Name ":" Zhangyyq "," Age ": 30.0," Sex ": true}, {" _id ": {" $oid ":" 555aebdb09b332c5a188b620 "}," passwd ":" 1234 "} {"_id": {"$oid": "555d80d7b7a5042665b195ae"}, "name": "Yangjx", "Age" :--}]
Cur.count (): 4
Cur.count (): 5
[{"_id": {"$oid": "555ae83a09668180fadccdeb"}, "age": +, "name": "Zhanggy", "Extra": "Markhere"}]
Reference:
Http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html
[MongoDB] MongoDB and Java use crud together