简体   繁体   中英

Searching in MongoDB database by multiple conditions

I'm new to mongodb and have this question. I have document like this where project is array of project IDs.

[{
    "_id": {
        "$oid": "62c2e94e65f32725f8f62b79"
    },
    "project": [1,4,5],
    "organization": "test",
    "name": "john",
    "surname": "doe"
},
{
  "_id": {
    "$oid": "62c409eb242b0509c4b5d891"
  },
  "project": [16,2],
  "organization": "test1",
  "name": "john1",
  "surname": "doe1"
}]

For searching the elements by organization I use this expression and it works ok User.find({organization: {$eq: data.org}}) How can I expand my expression so it search by organization and specific ID from the project array. For example I want to find user who work in organization Test and who works on project with ID 16.

Simply use comma to separate:

User.find({organization: "test1", project: 16})

See how it works on the playground example

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