2014-12-23 60 views
4

在下面的記錄我想item_details其中類型是「系統」和id獲取子文件中的記錄是「7634e9639f5c25f2434b72d8aMongoDB的使用過濾器

var operator = { 
     'item_details': { 
      $elemMatch: { 
       type: 'custom' 
      } 
     }   
    } 

    var query = { 
    _id : "7634e9639f5c25f2434b72d8a" 
    }; 

    req.db.collection('products').find(query, operator).toArray(function (err, result) { 

    if(err)throw err; 
    console.log(result); 
}); 

它是隻返回的第一個元素。但需要獲取與查詢和投影相匹配的所有記錄。 下面是我想要查詢的示例數據。

 [ 
     { 

      "_id": "7634e9639f5c25f2434b72d8a", 
      "item_details": [ 
       { 

        "quantity": 1, 
        "sub_total": 1201, 
        "type": "system" 
       }, 
       { 
        "quantity": 19, 
        "sub_total": 140, 
        "type": "custom" 

       }, 
       { 
        "quantity": 81, 
        "sub_total": 130, 
        "type": "custom" 

       }, 
       { 
        "quantity": 71, 
        "sub_total": 90, 
        "type": "system" 

       } 
      ] 

     }, 

     { 

      "_id": "564e9639f5c25f2434b72d8a", 
      "item_details": [ 
       { 

        "quantity": 1, 
        "sub_total": 101, 
        "type": "system" 
       }, 
       { 
        "quantity": 9, 
        "sub_total": 40, 
        "type": "custom" 

       }, 
       { 
        "quantity": 8, 
        "sub_total": 30, 
        "type": "custom" 

       }, 
       { 
        "quantity": 7, 
        "sub_total": 60, 
        "type": "system" 

       } 
      ] 

     } 
    ] 

回答