1. Download the Java driver package on the MONGODB website first
MongoDB Java Driver:
http://mongodb.github.io/mongo-java-driver/
Jar Package Download List
https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/3.5.0/
2. Examples of programs
1 Package com.test;2 3 import java.util.Arrays;4 5 import org.bson.Document;6 7 import com.mongodb.MongoClient;8 import com.mongodb.client.MongoCollection;9 import com.mongodb.client.MongoDatabase;Ten One Public classMongodb_test2 { A Public Static voidMain (string[] args) { -Mongoclient mongoclient =NewMongoclient ("localhost",27017 ); -Mongodatabase Database = Mongoclient.getdatabase ("MyDB"); theMongocollection<document> collection = Database.getcollection ("Test"); -System. out. println (Collection.count ()); -Document doc =NewDocument ("name","MongoDB") -. Append ("type","Database") +. Append ("Count",1) -. Append ("versions", Arrays.aslist ("v3.2","v3.0","v2.6")) +. Append ("Info",NewDocument ("x",203). Append ("y",102)); A Collection.insertone (DOC); atSystem. out. println (Collection.count ()); -System. out. println ("done!"); - } -}
Execution Result:
Database. Mongodb.java Sample Example