pythonmongodbpymongomongokit

How to use AND condtion in mongokit when a key has multiple values


I have a key in my document named "tag". Now this has the structure like this:

"tag": [
    {
      "schemeName": "http:\/\/somesite.com\/categoryscheme2",
      "name": "Test Tag2",
      "value": 1,
      "slug": "test_tag2"
    },
    {
      "schemaName": "http:\/\/somesite.com\/categoryscheme3",
      "name": "Test Tag3",
      "value": 1,
      "slug": "test_tag3"
    }
  ],

the values of tag can be passed ',' separated. How can i use those values in a AND condition saying return the enrty having tag = test_tag3&test_tag2

Itried it like this : conditions['tag.slug'] = { '$and': tags } where tags is array of tags but i got :

pymongo.errors.OperationFailure

OperationFailure: database error: invalid operator: $and

Solution

  • Use $in instead of $and:

    conditions['tag.slug'] = { '$in': tags }