简体   繁体   中英

mongodb find all documents with common value

How can I find all the documents with a common 'category'?

Below are my sample documents. How can get the documents that are common in categories?

{
    "_id": {
        "$oid": "59b7fcace893d0450c44ada3"
    },
    "title": "Tesla model 3 questions",
    "category": "movie",
    "questionAndAnswers": '',
    "__v": 0
}
{
    "_id": {
        "$oid": "59c4e12fd5276138f41c0896"
    },
    "title": "SpaceX Quiz",
    "category": "Science",
    "questionAndAnswers": '',
"__v": 0
}
{
    "_id": {
        "$oid": "59c4e12fd5276138f41c0890"
    },
    "title": "Nasa Quiz",
    "category": "Science",
    "questionAndAnswers": '',
"__v": 0
}

db.collection.find({"category": "Science"}) will return a cursor, which you can iterate to get all the documents where "category" is "Science".

Check - https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-documents-that-match-query-criteria

Simply use where category == 'theCommonCategory'.

It will give you all the documents that have theCommonCategory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM