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 ] }}
]
});
itemX_valueare actually only single values and not arrays themselves then you do not need$injust to search a document property that is an array. So unless the argument to$inneeds to be a "list" then you just do..find({ '$or': [ { 'item1: 1 }, {'item2': 2 } ]}).$inif 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