簡體   English   中英

如何查詢MongoDB以匹配文檔數組中的項目

[英]How to query MongoDB for matching documents where item is in document array

如果我存儲的文檔如下所示:

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

我怎樣才能查詢該文件包含oneThings

我知道$in運算符根據列表查詢文檔項,但這是相反的。 任何幫助都是極好的。

使用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' })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM