MongoDB nested Query

Source: Internet
Author: User
Tags findone

Today in the project encountered about MongoDB nested query, has not been contacted before, query the relevant data to record the results.

The data in the MongoDB are as follows:

> Db.customer.findOne ()
{
    "_id": ObjectId ("57636c8e35defe029962107e"),
    "_class": " Com.bu2trip.ticket.model.Customer ",
    " name ":" Wang ",
    " phone ":" 18408221624 ",
    " gender ": 1,
    " Birthday ":" 1995-7-9 ","
    Passport ":" 620524 ",
    " Login_user ": {
        " _id ": ObjectId (" 5760e593086659036b77c124 "),
        " email ":" test@bu2trip.com ",
        " phone ":" ""
    }

If you want to query the entire inline document, the query statement is as follows:

Db.customer.find ({"Login_user": {"_id": ObjectId ("5760e593086659036b77c124"), "email": "test@bu2trip.com", "Phone": "110"}})

All values with Login_user as keys must be written out in the query criteria.

Queries for only specific key values for inline documents are as follows:

Db.customer.findOne ({"Login_user.phone": "110"})

You only need to match a specific key value in a nested document.

For to Java clients, it is:

has been mongotemplate as an example

        Criteria Criatira = new criteria ();
        Criatira.andoperator (Criteria.where ("Login_user.phone"). is (a);
        Query query = new query (Criatira);
        Customer customer =  Mongotemplate.findone (query,customer.class);
        Loginuser loginuser = new Loginuser (xxxx);
        Criteria Criatira = new criteria ();
        Criatira.andoperator (Criteria.where ("Login_user"). is (Loginuser));
        Query query = new query (Criatira);
        Customer customer =  Mongotemplate.findone (query,customer.class);
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.