0

For our software I need a mongodb filter to filter specific documents from the database. Here is an example, how my data looks like:

[
  {
    "_id": "v4fv654vae65",
    "Title": "Title 123",
    "Array": [
      "Value1",
      "Value2"
    ]
  },
  {
    "_id": "f46vrwe6vg",
    "Title": "Title 456",
    "Array": [
     "Value3",
     "Value1",
     "Value2"
    ]
  }
]

Now I need a filter which filters the Array. The filter have to check only the values "Value1" AND "Value2". I dont want the document with the value: "Value3".

My current filter looks like this:

.find({ "Array": {$in: ["Value1", "Value2"]}})

This filter works nearly, but also returns the doucment with the "Value3" field in the array.

  • You can use all instead of in . http://stackoverflow.com/questions/5366687/how-to-check-if-an-array-field-contains-a-unique-value-or-another-array-in-mongo – Kiran Syeed Jun 07 '16 at 08:59
  • I think you should look here: http://stackoverflow.com/questions/37599298/in-mongo-how-would-i-match-all-items-of-collection-against-a-larger-array – Relu Mesaros Jun 07 '16 at 10:55

2 Answers2

0

.find({$and:[ {"Array":{$nin:["Value3"]}}, {"Array":{$in: ["Value1", "Value2"]}} ] })

Best regards

0

dont use $in , try this way

 db.getCollection('collectionName').find({"Array":[ 
            "Value1", 
            "Value2"
        ]})

output

[
{
    "_id": "v4fv654vae65",
    "Title": "Title 123",
    "Array": [
      "Value1",
      "Value2"
    ]
  }
]
karthi
  • 880
  • 6
  • 10