简体   繁体   English

MongoDB。 在子文档中查询数组

[英]MongoDB. Query for array in Subdocument

i am new to mongodb so please correct me if i am using the wrong terms: 我是mongodb的新手,所以如果我使用错误的术语,请纠正我:

I have the following document(excerpt) 我有以下文件(节选)

{
    "_id" : ObjectId("524b0a1a7294ec8a39d4230f"),
    "name" : "Irbesartan",
    "decompositions" : [
            "IRB_444",
            "IRB_442",
            "IRB_446",
            "Valsartan acid"
    ],
    "precursor" : [
            {
                    "mass" : 429,
                    "ion" : "+H",
                    "charge" : "+",
                    "fragments" : [
                            207,
                            195,
                            180
                    ]
            },
            {
                    "mass" : 427.2252,
                    "ion" : "-H",
                    "charge" : "-",
                    "fragments" : [
                            193,
                            399
                    ]
            }
    ]
}

With

db.substances.findOne({name: "Irbesartan"}).precursor

i get the following 我得到以下

[
    {
            "mass" : 429,
            "ion" : "+H",
            "charge" : "+",
            "fragments" : [
                    207,
                    195,
                    180
            ]
    },
    {
            "mass" : 427.2252,
            "ion" : "-H",
            "charge" : "-",
            "fragments" : [
                    193,
                    399
            ]
    }
]

But i want to acces the fields inside, espacially the fragment array (using Mongo Shell) 但是我想访问里面的字段,尤其是片段数组(使用Mongo Shell)

Is this possible in one query? 一个查询有可能吗?

Or is it better to store the precursor as an array instead of a subdocument? 还是将前体存储为数组而不是子文档更好?

For example query 例如查询

db.substances.distinct("precursor.fragments", {name: "Irbesartan"})

gives: 给出:

[ 180, 193, 195, 207, 399 ]

or 要么

db.substances.findOne({name: "Irbesartan"}).precursor.map(function(r) {
  return r.fragments;
})

gives

[ [ 207, 195, 180 ], [ 193, 399 ] ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM