Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

at least one match in list quering

I have a list of items to search:
a = [1,2,3,4,5,6]
and documents in mongo:
doc1 = { a: [1,2] }
doc2 = { a:[3] }
doc3 = { a:[9,10] }
doc4 = { a:[1,10] }

I need to query all documents where at least one parameter match

doc1,doc2,doc4 is ok and doc3 is not

Is it possible?

like image 797
lwolf Avatar asked Dec 31 '25 00:12

lwolf


1 Answers

In MongoDB shell you can retrieve the documents that match with

db.doc.find({"a":{"$in":[1,2,3,4,5,6]}})
{ "_id" : ObjectId("4e8eda300b92a26e01f4bc2f"), "a" : [ 1, 2 ] }
{ "_id" : ObjectId("4e8eda330b92a26e01f4bc30"), "a" : [ 3 ] }
{ "_id" : ObjectId("4e8eda3c0b92a26e01f4bc32"), "a" : [ 1, 10 ] }

when your collection seems like:

db.doc.find()
{ "_id" : ObjectId("4e8eda300b92a26e01f4bc2f"), "a" : [ 1, 2 ] }
{ "_id" : ObjectId("4e8eda330b92a26e01f4bc30"), "a" : [ 3 ] }
{ "_id" : ObjectId("4e8eda370b92a26e01f4bc31"), "a" : [ 9, 10 ] }
{ "_id" : ObjectId("4e8eda3c0b92a26e01f4bc32"), "a" : [ 1, 10 ] }
like image 130
barand Avatar answered Jan 05 '26 11:01

barand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!