Java Operation mongodb--Querying data

Source: Internet
Author: User
Tags mongoclient sorts tojson

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.