2014-06-24 73 views
0

我有以下收集記錄:MongoDB中找到的所有記錄與子文檔ID等於

> db.products.find(ObjectId("53a9a6aad901f2961403fc9b")).pretty() 
{ 
    "_id" : ObjectId("53a9a6aad901f2961403fc9b"), 
    "code" : "N39", 
    "name" : { 
     "en-UK" : "N39" 
    }, 
    "weight" : [ 
     90 
    ], 
    "collectionId" : ObjectId("53a9a6a8d901f2961403fbe2"), 
    "fabric_composition" : [ 
     { 
      "fabricId" : ObjectId("53a9a6a9d901f2961403fc69"), 
      "value" : 70 
     } 
     { 
      "fabricId" : ObjectId("53a9a6a9d901f2961403fc6a"), 
      "value" : 30 
     } 
    ], 
    "visible" : "true", 
    "manufacturer" : "53a859d9d901f2e8f81ac83b" 
} 

> db.fabric.find().pretty() 
{ 
    "_id" : ObjectId("53a9a6a9d901f2961403fc69"), 
    "name" : [ 
     { 
      "en-UK" : "Recycled Organic Cotton" 
     } 
    ] 
} 
{ 
    "_id" : ObjectId("53a9a6a9d901f2961403fc6a"), 
    "name" : [ 
     { 
      "en-UK" : "Recycled Polyester" 
     } 
    ] 
} 

如何查詢MongoDB的集合products列出有fabric_composition所有產品以ObjectIdRecycled Organic Cotton爲例?

任何意見大加讚賞

回答

2

您需要使用使用dot notation查詢子文檔:

db.products.find({ 
    "fabric_composition.fabricId" : ObjectId("53a9a6a9d901f2961403fc69") 
}); 

此查詢將返回至少有一個子文檔與您正在尋找fabricId所有文件。

相關問題