1

Let's say I have a list of keywords and I want to match against MongoDB documents in which they all appear in the keys field (that is, a AND clause).

How can I do that in Python?

I've tried:

keywords = ['a', 'b', 'c']
keywords_dict = {'keys' : keywords}
results = collection.find(keywords_dict)

But it seems to return no result.

I'm using Python3.5 with PyMongo.

Any hints?

1
  • I've figured it out: I need to make a list of dictionaries, e.g. { '$and' : [ {'keys' : key1} , {'keys' : key2} ] } - any hints on other ways to do this is welcome! Commented Nov 11, 2015 at 18:37

1 Answer 1

1

You can use the $all operator which is also equivalent to an $and operation of the specified values; i.e. the following statement:

{ 'keys': { '$all': ['a', 'b', 'c'] } }

is equivalent to:

{ '$and': [ { 'key': a }, { 'key': b }, { 'key': c} ] }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.