2

I have a MongoDB document as follows:

{
    "_id" : ObjectId("5c29f3123d8cf714fd9cdb87"),
    "Machine" : "host1",
    "Pools" : [
        "Pool1",
        "Pool2"
    ]
}

How do I find all the documents that have pool Pool1 in "Pools" key in my collection?

I tried the following, but it doesn't seem correct.

 db.Resources.find({Pools: {$elemMatch: { "$in", ['Pool1']}}}).pretty()
Nemo
  • 24,540
  • 12
  • 45
  • 61

1 Answers1

0

There are different ways to get what you want.

Find all records whose Pools' array contains Pool1:

db.Resources.find({Pools: 'Pool1'}).pretty()

Find all records whose Pools' array contains the following array elements, the order does not matter

db.Resources.find({Pools: {$all: ['Pool1', ...]}}).pretty()

To read more on querying arrays, see this mongodb post