0

i am new to mongodb so please correct me if i am using the wrong terms:

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)

Is this possible in one query?

Or is it better to store the precursor as an array instead of a subdocument?

1 Answer 1

1

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 ] ]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i will give it a try tomorrow. Is this possible db.substances.distinct("precursor.mass", {name: "Irbesartan"})?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.