0

我是 mongodb 的新手,所以如果我使用了错误的术语,请纠正我:

我有以下文件(摘录)

{
    "_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
                    ]
            }
    ]
}

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

我得到以下

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

但我想访问里面的字段,尤其是片段数组(使用 Mongo Shell)

这可能在一个查询中吗?

还是将前体存储为数组而不是子文档更好?

4

1 回答 1

1

例如查询

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

给出:

[ 180, 193, 195, 207, 399 ]

或者

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

[ [ 207, 195, 180 ], [ 193, 399 ] ]
于 2013-10-01T19:46:22.280 回答