0

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"
}

Is it possible to write a single query that would only find

the Product with an ID of "123" and variant "000"

And the product with ID "987" and variant "000"

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

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

I have search this issue and only found answers related to the $in operator. but I don't think that will allow me to do what I need here? could someone point me in the right direction or let me know if this is even possible. I'm trying to avoid making individual request the the database for each item.

1 Answer 1

1

Simple $or can do the job:

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

playground

Sign up to request clarification or add additional context in comments.

3 Comments

This works in the playground but not when I use it in development?
maybe provide some more details , this is simple logical operation that is expected to work in all mongodb versions , any error messages or issues seen?
I've accepted your answer as it does what I need and opened a new question related to my specific error thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.