2

i am facing a strange behaviuor with mongo-shell und Spring Data Mongodb

Here is my example query:

> db.substances.find({"precursor.mass": {$gte:159.9900,$lte:160.0100}}).count()
6

So i want to find mass values in a given range (plus/minus). In this example the input was 160 +/- 0.01.

Here are two out of the six results. The frist one is ok, but I can not explain and understand why the second result is found.

{
"_id" : ObjectId("524bfe80729458abfd7698a5"),
"exactMass" : 159.1259,
"nominalMass" : 159,
"molarMass" : 159.22,
"status" : 1,
"decompositions" : [ ],
"precursor" : [
    {
        "mass" : 160,
        "ion" : "+H",
        "charge" : "+",
        "fragments" : [
            55,
            83,
            124,
            97,
            69,
            142
        ]
    }
],
}

{
"_id" : ObjectId("524bfe80729458abfd7695be"),
"exactMass" : 159.068414,
"nominalMass" : 159,
"molarMass" : 159.19,
"status" : 0,
"decompositions" : [ ],
"precursor" : [
    {
        "mass" : 158.0611,
        "ion" : "-H",
        "charge" : "-",
        "fragments" : [ ]
    },
    {
        "mass" : 160.0756,
        "ion" : "+H",
        "charge" : "+",
        "fragments" : [ ]
    }
],
}

What am I overlooking? I Suppose i am using a wrong query?

My goal is to find all substances where ANY of the precursor.mass is between a given value +/- a tolerance.

1 Answer 1

3

From the mongo doc on find():

If a field contains an array and your query has multiple conditional operators, the field as a whole will match if either a single array element meets the conditions or a combination of array elements meet the conditions.

So, since {"mass" : 158.0611} matches second query criterion (is less than 160.0100), and {"mass" : 160.0756} matches first (is greater than 159.9900), the whole array matches.

What you're looking for is $elemMatch:

db.substances.find({
  "precursor": { 
    $elemMatch: {
      mass: { $gt:159.9900, $lt:160.0100 }
    }
  }
}).pretty()
Sign up to request clarification or add additional context in comments.

Comments

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.