MongoDB对SubArray ID的查询过滤

时间:2016-04-01 04:00:43

标签: mongodb nosql

假设我在MongoDB中有3个集合。假设“commitment_id”值是“我的承诺”的Object_ID,并且基金数组中的“id”值是指“我的基金”和“他的基金”。如何获取commitment_attributes集合的资金数组中提到的资金列表?从我读过的所有内容来看,似乎我需要循环遍历光标并逐个获取,但有没有办法在查询中自动执行此操作?

db.commitments.insert({ 
   commitment_nm : "My Commitment",
        due_dt : "1/1/2016",
        ccy: "USD"
    });

db.funds.insert({
    name: "My Fund",
    ccy_iso_cd: "USD"

}, {
    name: "His Fund",
    ccy_iso_cd: "EUR",
})

db.commitment_attributes.insert({
    "commitment_id": ObjectId("56fdd7e371d53780e4a63d58"),
    "attribute_nm": "Original Commitment",
    "effective_dt": "1/10/2016",
    "funds": [ 
        {
            "id": ObjectId("56fdd78c71d53780e4a63d57"),
            "amount": 1000
        }, 
        {
            "id": ObjectId("56fdd78c71d53780e4a63d57"),
            "amount": 500
        }
    ]
})

var commitment = db.commitments.findOne({commitment_nm: "My Commitment"})
var attribute = db.commitment_attribtues.findOne({"commitment_id" : commitment._id});

var funds = ...... want to query for only the funds which have their ID mentioned in the attribute var

0 个答案:

没有答案