"MongoDB" Nested arrays query scheme

Source: Internet
Author: User

From:http://stackoverflow.com/questions/12629692/querying-an-array-of-arrays-in-mongodb

Data

Db.multiArr.insert ({"id": "fruit1", "Keys": [["Apple", "carrot", "banana"]]}) Db.multiArr.insert ({"id": "Fruit2", " Keys ": [[" Apple "," orange "," Banana "]]}) db.multiArr.find ({' Keys ': {$elemMatch: {$elemMatch: {$in: [' Carrot '}}}}) {" _ ID ": ObjectId (" 506555212AEB79B5F7374CBF ")," id ":" fruit1 "," Keys ": [[" Apple "," carrot "," banana "]]}db.multiarr.fin D ({' Keys ': {$elemMatch: {$elemMatch: {$in: [' Banana '}}}}}} {"_id": ObjectId ("506555212AEB79B5F7374CBF"), "id": "fruit1  "," Keys ": [[" Apple "," carrot "," banana "]]} {" _id ": ObjectId (" 5065587e2aeb79b5f7374cc0 ")," id ":" fruit2 "," Keys ": [["Apple", "orange", "Banana"]]}

Query statement:

Interesting question, this would do the trick

Db.multiArr.find ({' Keys ': {$elemMatch: {$elemMatch: {$in: [' Carrot '}}}})
$elemMatch used to check if a element in an array matches the specified match expression. So nested $elemMatch would go deeper into nested arrays

"MongoDB" Nested arrays query scheme

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.