简体   繁体   中英

MongoDB Query Multiple Specific Documents

I would like to make a single query to my backend database to retrieve a list of results. These results will have a unique "product_id" and they will also have a "variant_id". Items may share product IDs but must have unique variant IDs. For Example

{
  product_id:"123",
  variant_id:"000"
}

{
  product_id:"123",
  variant_id:"111"
}

{
  product_id:"987",
  variant_id:"000"
}

Simple $or can do the job:

db.collection.find({
$or: [
 {
  "product_id": "123",
  "variant_id": "000"
 },
 {
  "product_id": "987",
  "variant_id": "000"
 }
]
})

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