The two ways MongoDB uses subdocuments as query conditions

Source: Internet
Author: User

    {      "_id": ObjectId ("52fc6617e97feebe05000000"),      "age": +,      "level": numberint (+),      "name": "Name5" ,      "person": [        {          "level": 5,          "score": +,        "CCC": {"ccc3": "FFF3"          :] Level ": 7,          " score ": All,          " like ":" Music "        }      ],      " score ": Numberint,      " sex ":" Male "    }

Method One:

Db.testColl.find ({"Person.level": 7});d B.testcoll.find ({"PERSON.CCC.CCC3": 56});

  

Can detect the results, that is, this method can be nested use!

Method two, using the $elemmatch operator

Db.testColl.find ({"person": {"$elemMatch": {"Level": 5}});

  

We can find the results.

Db.testColl.find ({"person": {"$elemMatch": {"CCC": {"$elemMatch": {"ccc3": 56}}}});

  

I can't find the results.

Db.testColl.find ({"person": {"$elemMatch": {"CCC": {"ccc3": "FFF3": +}}});d B.testcoll.find ({"person": {"$ Elemmatch ": {" ccc.ccc3 ": 56}});

  

We can find the results.
This means that the second method cannot be nested.

The two ways MongoDB uses subdocuments as query conditions

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.