如何从MiniMongo获取嵌套值的文档?

时间:2016-06-28 14:22:04

标签: meteor minimongo

我有一个带有Mongo数据库的Meteor应用程序,其中包含具有以下结构的文档:

_id: "1234567890",
blocks: [{
    type: "block",            
    block_id: "foobar",
    items: [
        {
            type: "sub",
            sub_id: "111",
            items: [
                {
                    type: "question",
                    question_id: "aaa"
                },
                {
                    type: "question",
                    question_id: "bbb"
                }
            ]
        },
        {
            type: "question",
            question_id: "aaa"
        }
    ]
}]

我希望能够通过question_id aaa'找到所有问题。到目前为止,我已尝试过这些查询,但我很难回复任何结果:

questions = MyColl.find({
     $or: [{
         blocks: {
             items: {
                 $elemMatch: {question_id: 'aaa'}
             }
         }
     },{
         blocks: {
             items: {
                 type: "sub",
                 items: {
                     question_id: 'aaa'
                 }
             }
         }
     }]
 }).count();

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这就是我管它的方式:

questions = MyColl.find({
             $or: [{
                 'blocks.items': {
                     $elemMatch: {question_id: 'aaa'}
                 }
             },{
                 'blocks.items.items': {
                     $elemMatch: {question_id: 'aaa'}
                 }
             }]
         }).count();