Querying the document information in a collection through the Find method
--------------------------------------------------------
Find ()
Query all document information, return finditerable<document>
We can get the document information through the Finditerable foreach method
Mongoclient mongoclient = new Mongoclient ();
Mongodatabase db = mongoclient.getdatabase ("test");
mongocollection<document> doc = db.getcollection ("text");
Finditerable<document> iter = Doc.find ();
Iter.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
--------------------------------------------------------------------
Conditional query
Document<field<value)
field is a query, value is a query, or a filter filters,filters provides a static method for a series of query conditions
Equal-=
Finditerable<document> iter = Doc.find (new Document ("name", "Zhang San"));
or finditerable<document> iter = Doc.find ("Age", New document ("$eq", 24));
or finditerable<document> iter = Doc.find (Filters.eq ("name", "Zhang San"));
Iter.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
Unequal-!=
Finditerable<document> iter = Doc.find ("Age", New document ("$ne", 24));
Finditerable<document> iter = Doc.find (filters.ne ("name", "Zhang San"));
Iter.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
Greater than
Finditerable<document> iter = Doc.find ("Age", New document ("$GT", 22));
or finditerable<document> iter = Doc.find (filters.gt ("age", 22));
Iter.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
Greater than or equal to->=
Use the same as above, the symbol is $GTE
Less than-<
finditerable<document> iter1 = doc.find (New document ("Age", New document ("$lt", 22)));
or finditerable<document> iter = Doc.find (filters.lt ("age", 22));
Iter1.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
Less than or equal to-<=
Use the same as above, the symbol is $lte
and-and
finditerable<document> iter1 = Doc.find (New Document ("Age"), append ("name", "Zhang San"));
or finditerable<document> Iter1 = Doc.find (Filters.and (Filters.eq ("name", "Zhang San"), filters.lt ("age", 30));
Iter1.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
or-or.
list<document> list = new arraylist<document> ();
List.add (New Document ("name", "Zhang San"));
List.add (New Document ("Age", 24));
finditerable<document> iter1 = Doc.find (New Document ("$or", list));
finditerable<document> iter1 = Doc.find (Filters.and (Filters.eq ("name", "Zhang San"), filters.lt ("age", 30));
Iter1.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
Presence-in
list<string> list = new arraylist<string> ();
List.add ("Zhang San");
List.add ("John Doe");
Finditerable<document> iter = doc.find (New document ("name", New Document ("$in", list));
Finditerable<document> iter = Doc.find (filters.in ("name", "Zhang San", "John Doe", "Harry"));
Iter.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
Does not exist-not in
Use the same as above, the symbol is $nin
Sort-sort
finditerable<document> iter1 = Doc.find (). Sort (New Document ("Age", 1). Append ("Phone", 1));
finditerable<document> iter1 = Doc.find (). Sort (Sorts.orderby (sorts.ascending ("Age"), Sorts.descending (" (phone ")));
Iter1.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
Java Operation mongodb--Querying data