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.

4

1 回答 1

2

find() 上的 mongo 文档

如果一个字段包含一个数组并且您的查询有多个条件运算符,则如果单个数组元素满足条件或数组元素的组合满足条件,则该字段作为一个整体将匹配。

因此,由于{"mass" : 158.0611}匹配第二个查询条件(小于 160.0100),并且{"mass" : 160.0756}首先匹配(大于 159.9900),因此整个数组匹配。

您正在寻找的是$elemMatch

db.substances.find({
  "precursor": { 
    $elemMatch: {
      mass: { $gt:159.9900, $lt:160.0100 }
    }
  }
}).pretty()
于 2013-10-15T11:43:08.653 回答