Package com.tesco.mongodb.test;
Import java.util.List;
Import java.util.ArrayList;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import org.bson.Document;
Import Com.mongodb.AggregationOutput;
Import Com.mongodb.BasicDBObject;
Import com.mongodb.MongoClient;
Import com.mongodb.client.AggregateIterable;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoCursor;
Import Com.mongodb.client.MongoDatabase;
public class Test1 {public static void main (string[] args) {Logger log = Logger.getlogger ("Org.mongodb.driver");
Log.setlevel (Level.off);
Mongoclient mongoclient = null;
Mongocursor<document> mongocursor = null;
try {Long L1 = System.currenttimemillis ();
Mongoclient = new Mongoclient ("xxx.xxx.xxx.xxx", 27017);
Create DB//Mongodatabase mongodatabase = Mongoclient.getdatabase ("TestDB");
Mongodatabase.createcollection ("Testcol"); Mongodatabase MongodAtabase = Mongoclient.getdatabase ("TestDB");
Mongocollection<document> collection = Mongodatabase. GetCollection ("Testcol");
Create Document/Document document = new Document ("title", "MongoDB").
Append ("description", "Database2").
Append ("likes", 100).
Append ("by", "Fly2");
list<document> documents = new arraylist<document> ();
Documents.Add (document);
Collection.insertmany (documents); Update document//Collection.updatemany (Filters.eq ("likes"), New//Document ("$set", New document ("Likes", 20
0));
Delete Document//Collection.deletemany (Filters.eq ("likes", 200));
Display fields Basicdbobject fields = new Basicdbobject ();
Fields.put ("_id", 0);
Fields.put ("title", 1);
Fields.put ("description", 1);
Fields.put ("likes", 1);
Fields.put ("by", 1);
Query Basicdbobject query = new Basicdbobject ();
Query.put ("description", "Database2"); QuEry.put ("Likes", New Basicdbobject ("$gt", 0));
Sort Basicdbobject sort = new Basicdbobject ();
Sort.put ("by",-1);
Sort.put ("likes", 1);
Sort.put ("by", 1);
System.out.println (Collection.count ());
finditerable<document> finditerable = collection.find (query). Sort (sort) . projection (Fields). Skip ((20000-1) *10). Li
MIT (10);
Mongocursor = Finditerable.iterator ();
while (Mongocursor.hasnext ()) {//System.out.println (Mongocursor.next ());
Document d = mongocursor.next ();
Jsonobject jsonobj = new Jsonobject (D.tojson ());
System.out.println (D.tojson ());
String description = (string) jsonobj.get ("by");
System.out.println (description);
}//Basicdbobject match1 = new Basicdbobject ();
Basicdbobject match2 = new Basicdbobject (); list<string> list_query_in = new Arraylist<stRing> ();
List_query_in.add ("Database1");
List_query_in.add ("Database2");
Match2.put ("$in", list_query_in);
Match1.put ("description", MATCH2);
Basicdbobject match = new Basicdbobject ("$match", match1);
//Basicdbobject P_fields = new Basicdbobject ();
P_fields.put ("_id", 0);
P_fields.put ("description", 1);
P_fields.put ("likes", 1);
Basicdbobject project = new Basicdbobject ("$project", p_fields);
Basicdbobject groupfields = new Basicdbobject ("_id", "$description");
Basicdbobject groupfields = new Basicdbobject ("_id", null); Groupfields.put ("average", New Basicdbobject ("$avg", "$likes")); 102.5//Groupfields.put ("Total", New Basicdbobject ("$sum", "$likes")); 410////groupfields.put ("Count", New Basicdbobject ("$sum", 1);
4//Basicdbobject group = new Basicdbobject ("$group", groupfields);
Basicdbobject sort1fields = new Basicdbobject ("Total",-1); Basicdbobject Sort1 = newBasicdbobject ("$sort", sort1fields);
list<basicdbobject> List = new arraylist<basicdbobject> ();
List.add (match);
List.add (project);
List.add (group);
List.add (SORT1);
//aggregate//aggregateiterable<document> output = collection.aggregate (list);
mongocursor<document> MongoCursor1 = Output.iterator ();
while (Mongocursor1.hasnext ()) {//Document D = mongocursor1.next ();//System.out.println (D.tojson ());//}
Long L2 = System.currenttimemillis ();
System.out.println (L2-L1);
catch (Exception e) {e.printstacktrace ();
finally {if (null!= mongocursor) mongocursor.close ();
if (null!= mongoclient) mongoclient.close ();
}
}
}
Official Link: http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/perform-read-operations/
Package com.tesco.mongodb.test;
Import Java.util.Date;
Import java.util.List;
Import java.util.ArrayList;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import org.bson.Document;
Import Org.json.JSONObject;
Import Com.mongodb.AggregationOutput;
Import Com.mongodb.BasicDBObject;
Import Com.mongodb.Block;
Import Com.mongodb.DBCursor;
Import Com.mongodb.MongoClientURI;
Import Com.mongodb.ReadConcern;
Import com.mongodb.ReadPreference;
Import Com.mongodb.WriteConcern;
Import com.mongodb.MongoClient;
Import static com.mongodb.client.model.filters.*;
Import static com.mongodb.client.model.projections.*;
Import com.mongodb.client.AggregateIterable;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoCursor;
Import Com.mongodb.client.MongoDatabase;
Import Com.mongodb.client.model.Filters;
Import Com.mongodb.client.model.Sorts; public class Test4 {public static void main (string[] args) {for (int i = 0; i < 3; i++)
{run1 ();
The private static void Run1 () {Logger log = Logger.getlogger ("Org.mongodb.driver");
Log.setlevel (Level.off);
Mongoclient mongoclient = null;
Mongocursor<document> mongocursor = null; block<document> Printblock = new block<document> () {@Override public void apply (final docum
ENT document) {SYSTEM.OUT.PRINTLN (Document.tojson ());
}
};
try {Long L1 = System.currenttimemillis (); * * Mongoclienturi connectionString = new Mongoclienturi ("* mongodb://root:**** @dds-bp114e3f1fc441342.mongodb.rd s.aliyuncs.com:3717, * dds-bp114e3f1fc441341.mongodb.rds.aliyuncs.com:3717/admin?replicaset=mgset-677201 ");
Replace with the root password mongoclient client = new Mongoclient (connectionString); * *///Mongoclienturi connectionString = new Mongoclienturi ("mongodb://myrootadmin:xxx@" +//Mongoclienturi Connect ionstring = new Mongoclienturi ("Mongodb://mytester:xxx@ "+//Mongoclienturi connectionString = new Mongoclienturi (" mongodb://myuseradmin:xxx@ "+ Mongoclienturi Co nnectionstring = new Mongoclienturi ("mongodb://" + "mongodb0.example.net:26001," + "mongodb1.example.net:26002," + "mongodb2.example.net:26003/admin?replicaset=rs0&maxpoolsize=400");//&readpreference=secondary//& Readpreference=nearest//mongodb://host:27017/?replicaset=rs0&maxpoolsize=200//mongoClient = new MongoClient ("
10.254.45.223 ", 27017);
Mongoclient = new Mongoclient ("10.254.45.223", 26001);
Mongoclient = new Mongoclient (connectionString);
Mongoclient = Mongodbpoolutil.pollconnection ();
Mongoclient.setreadpreference (Readpreference.secondary ());
Readpreference.nearest ();
Readpreference.primary ();
Readpreference.primarypreferred ();
Readpreference.secondary ();
Readpreference.secondarypreferred ();
System.out.println (Mongoclient.getreadpreference ()); Create DB// Mongodatabase mongodatabase = mongoclient.getdatabase ("TestDB");
Mongodatabase.createcollection ("Testcol1");
Get db mongodatabase mongodatabase = mongoclient.getdatabase ("test"); Mongodatabase mongodatabase = mongoclient.getdatabase ("local");//.withreadpreference (Readpreference.secondary ()
)//System.out.println (Mongodatabase.getreadpreference ());
Mongodatabase.withreadpreference (Readpreference.secondary ());
Mongodatabase.withwriteconcern (writeconcern.majority);
Mongodatabase.withreadconcern (readconcern.majority);
Readpreference preference = readpreference.secondarypreferred ();
Mongocollection<document> collection = Mongodatabase.getcollection ("Startup_log");
Mongocollection<document> collection = Mongodatabase.getcollection ("testcol123");
Mongocollection<document> collection = mongodatabase.getcollection ("foo");
. Withreadpreference (Readpreference.secondary ()); Readpreference PREference = readpreference.primarypreferred ();
System.out.println (Collection.getreadpreference ());
Dbcursor cur = new Dbcursor (collection, NULL, NULL, preference);
Collection.withreadpreference (Readpreference.secondary ());
Collection.withwriteconcern (writeconcern.majority);
Collection.drop ();
Create Document//list<document> documents = new arraylist<document> (); for (int i = 0; i < 1; i++) {//Document document = new Document ("title", "MongoDB").//Append ("Descript
Ion "," Database2 ").
Append ("likes", I).
Append ("by", "Fly" +string.valueof (i)).
Append ("Long1", long.valueof (i)).
Append ("decimal", New Double ("1.9886443895687")).
Append ("Date1", New Date ());
Documents.Add (document);
//Collection.insertmany (documents);
System.out.println (System.currenttimemillis ());
System.out.println (System.nanotime ()); Collection.find (eq ("A1", 222)) Collection.finD (). Projection (Fields (include ("A1", "A2"), Excludeid ()). Sort (Sorts.descending ("A2"))//. Skip (2). Limit (4). f
Oreach (Printblock);
finditerable<document> finditerable = Collection.find ();
Mongocursor = Finditerable.iterator ();
System.out.println (Mongocursor.hasnext ()); while (Mongocursor.hasnext ()) {//Mongocursor.next ();//System.out.println (Mongocursor.next ());//Documen
T d = mongocursor.next ();
System.out.println (D.tojson ());
Jsonobject jsonobj = new Jsonobject (D.tojson ());
Long D1 = (long) jsonobj.getjsonobject ("Date1"). Get ("$date");
System.out.println (New Date (D1)); }//update Document//collection.updatemany (Filters.eq ("likes", 5), New document ("$unset", New Document ("Lo
Ng2 ", null));
Delete Document//collection.deletemany (Filters.eq ("likes", 5));
//display fields//basicdbobject fields = new Basicdbobject ();
Fields.put ("_id", 0); Fields.put ("tItle ", 1);
Fields.put ("description", 1);
Fields.put ("likes", 1);
Fields.put ("by", 1);
Query//Basicdbobject query = new Basicdbobject ();
Query.put ("description", "Database2");
Query.put ("Likes", New Basicdbobject ("$lt", 104));
Sort//Basicdbobject sort = new Basicdbobject ();
Sort.put ("by",-1);
Sort.put ("Likes",-1);
Sort.put ("by", 1);
System.out.println (Collection.count ()); finditerable<document> finditerable = collection.find (query). Sort (sort). Projection (Fields). Skip (0). Limit (
20);
mongocursor<document> mongocursor = Finditerable.iterator (); while (Mongocursor.hasnext ()) {////System.out.println (Mongocursor.next ());//Document D = mongocursor.next ()
;
Jsonobject jsonobj = new Jsonobject (D.tojson ());
System.out.println (D.tojson ());
String description = (string) jsonobj.get ("by");
System.out.println (description); }////BasicdboBject match1 = new Basicdbobject ();
Basicdbobject match2 = new Basicdbobject ();
list<string> list_query_in = new arraylist<string> ();
List_query_in.add ("Database1");
List_query_in.add ("Database2");
Match2.put ("$in", list_query_in);
Match1.put ("description", MATCH2);
Basicdbobject match = new Basicdbobject ("$match", match1);
Basicdbobject p_fields = new Basicdbobject ();
P_fields.put ("_id", 0);
P_fields.put ("description", 1);
P_fields.put ("likes", 1);
Basicdbobject project = new Basicdbobject ("$project", p_fields);
Basicdbobject groupfields = new Basicdbobject ("_id", "$description");
Basicdbobject groupfields = new Basicdbobject ("_id", null); Groupfields.put ("average", New Basicdbobject ("$avg", "$likes")); 102.5//Groupfields.put ("Total", New Basicdbobject ("$sum", "$likes")); 410////groupfields.put ("Count", New Basicdbobject ("$sum", 1); 4//Basicdbobject group = new Basicdbobject ("$group", groupfields);
Basicdbobject sort1fields = new Basicdbobject ("Total",-1);
Basicdbobject sort1 = new Basicdbobject ("$sort", sort1fields);
list<basicdbobject> List = new arraylist<basicdbobject> ();
List.add (match);
List.add (project);
List.add (group);
List.add (SORT1);
list<basicdbobject> List = new arraylist<basicdbobject> ();
Basicdbobject _lookup = new Basicdbobject ();
_lookup.put ("from", "inventory");
_lookup.put ("Localfield", "item");
_lookup.put ("Foreignfield", "SKU");
_lookup.put ("as", "Inventory_docs");
Basicdbobject _lookup1 = new Basicdbobject ();
_lookup1.put ("$lookup", _lookup);
List.add (_LOOKUP1);
Aggregate//Aggregateiterable<document> output = collection.aggregate (list);
Mongocursor = Output.iterator (); while (Mongocursor.hasnext ()) {//Document D = mongocursor.next ();//System.out.priNtln (D.tojson ());
}//Long L2 = System.currenttimemillis ();
System.out.println (L2-L1);
catch (Exception e) {e.printstacktrace ();
finally {if (null!= mongocursor) mongocursor.close ();
if (null!= mongoclient) {mongoclient.close ();//Mongodbpoolutil.closeconnection (mongoclient);
}
}
}
}