有沒有任何方法可以在沒有根文檔的情況下從集合的字段(數組)中選擇所有嵌入式文檔?例如,給定這個集合MongoDB按照無標準條件選擇所有嵌入式文檔
{ "_id" : 1, "cars" : [{"brand": "audi", "color": "red"}, {"brand": "audi", "color": "yellow"}]}
{ "_id" : 2, "cars" : [{"brand": "audi", "color": "blue"}, {"brand": "seat"}]}
我想與下面的輸出選擇所有汽車品牌audi
{"brand": "audi", "color": "red"}
{"brand": "audi", "color": "blue"}
{"brand": "audi", "color": "yellow"}
這似乎並不可能只db.collection.find()
與projection
,因爲它的匹配標準始終與根文檔相關。
@SagarReddy這將選擇至少有一輛汽車匹配brand ='audi'的所有根對象。但是例如,第二份文件中的「座位」車輛將被錯誤地包括在內。 –
'db.collection.find({'cars.brand':'audi'},{'cars。$':1})' – styvane
@Styvane這將只會得到第一個匹配記錄。我認爲OP需要所有匹配的條目。 – Veeram