2

Given the following documents in the ariticle collection:

{ _id: 1, words: [ "Apple", "IT", "Company", "IOS" ] }

{ _id: 2, words: [ "Google", "Android", "Search Engine","Company" ] }

Now, I wanna to query on the words field to get the documents that hit a specified number of elements in the list ["Company","Apple","IT","Google","Iphone","Smartisan"]. For example, I wanna to get the documents that hits any of 3 in the list above on field words.

How to write the query statements in pymongo, or does pymongo support query like this?

user3872279
  • 121
  • 2

1 Answers1

1

You can use the $size operator to select arrays of a certain size.

db.article.find({ "words" : { "$size" : 3 } })

Obviously, replace db and article with the variables identifying your database and collection.

See also this related question:

Query for documents where array size is greater than 1

Community
  • 1
  • 1
Pascal Bugnion
  • 4,878
  • 1
  • 24
  • 29