1

I have been going through the mongodb querying inside an array. I have gotten the example they give to work for me for querying a single array. But how would this work if I had two different arrays and wanted to combine them into a single query? for example item1 and item2 were two different arrays.

# example given in mongodb document
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )

# example of query of what I am trying achieve query two arrays 
db.inventory.find( { item1: { $in: [ item1_value ] } { item2: { $in: [ item2_value ] } } )

mongodb reference doc: https://docs.mongodb.com/manual/reference/operator/query/in/

I should also mention that I am using mongodb to get the idea of what the command would look like, but ultimately this command should work for pymongo as this command will be executed via pymongo.

# correct query example as given by Moshe
db.inventory.find({ 
      $or: [ 
             { item1: { $in: [ item1_value ] }}, 
             { item2: { $in: [ item2_value ] }}
       ]
});
2
  • You should note that if your itemX_value are actually only single values and not arrays themselves then you do not need $in just to search a document property that is an array. So unless the argument to $in needs to be a "list" then you just do. .find({ '$or': [ { 'item1: 1 }, {'item2': 2 } ]}). $in if for "argument lists" .find({ '$or': [ { 'item1': { '$in': [1,2] } }, { 'item2': { '$in': [1,2] } } ] }). MongoDB does not care if 'item1' is actually an array itself, since it will simply search it's members for the "singular" supplied value. And yes the python syntax is identical Commented Jun 15, 2017 at 23:58
  • Thanks for that clarification. I will keep that in mind. Commented Jun 16, 2017 at 2:51

1 Answer 1

2

MongoDB "OR" SYNTAX:

{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

In your case:

db.inventory.find({ 
      $or: [ 
             { item1: { $in: [ item1_value ] }, 
             { item2: { $in: [ item2_value ] }
       ]
});
Sign up to request clarification or add additional context in comments.

5 Comments

I think your correct, but when I run this as a query i keep getting ... as if syntax is not completely terminating the command.
@Matt I had my syntax wrong, I mixed up ] with }, it should work now.
hmm, this is what is killing me with mongodb. the syntax is a bit pain full. I tried this as well seems to be same issue. seeing ... I think { item1: { $in: [ item1_value ] } was missing a bracket because the outermost bracket only matched to the bracket before the $in. I closed that and still seeing issue.
Thank you so much, I would have spent days figuring out this syntax. Their was one small syntax error but pretty easy to figure out with your example. I will show your answer in my question example with updated syntax. Again thanks so much.
My pleasure, glad to help.

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.