0

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

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

如何查询包含onein的文档Things

我知道$in操作员根据列表查询文档项,但这有点相反。任何帮助都是极好的。

4

1 回答 1

2

使用 MongoDB 的多键支持:

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' })
于 2012-10-05T04:23:40.237 回答