如何查询MongoDB以匹配文档数组中的项目

时间:2012-10-05 04:02:39

标签: list mongodb

如果我的存储文档如下所示:

doc = {
    'Things' : [ 'one' , 'two' , 'three' ]
    }

如何查询one中包含Things的文档?

我知道$in运算符会根据列表查询文档项,但这是相反的。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:2)

使用MongoDB的multikeys支持:

  

MongoDB提供了一个有趣的“多键”功能,可以自动索引对象值的数组   [...]

db.articles.find( { tags: 'april' } )
{"name" : "Warm Weather" , "author" : "Steve" , 
 "tags" : ["weather","hot","record","april"] , 
 "_id"  : "497ce4051ca9ca6d3efca323"}

基本上,您不必担心Things的数组,MongoDB会为您处理; MongoDB shell中的这样的东西可以工作:

db.your_collection.find({ Things: 'one' })
相关问题