Search code examples
mongodbmongo-java

Unwind does not show all data


I have a collection which looks like this:

{

  "consultation_type" : "1",
  "encounter_id" : "12345"
}

I am applying the following query:

db.encounter.aggregate([{ "$unwind" : "$vitals"},{ "$match" : { "$eq" : [ "$vitals.spirometer.FVC" , "null"]}},{$limit:1}])

but I don't get any result. Is there something wrong in my query?


Solution

  • With your aggregate command, I get the error:

    Mon Aug 12 10:01:01.269 JavaScript execution failed: aggregate failed: { "errmsg" : "exception: bad query: BadValue unknown top level operator: $eq",

    You shouldn't need $eq, but instead do:

    db.encounter.aggregate( [
        { "$unwind" : "$vitals" },
        { "$match" : { "vitals.spirometer.FVC" : "null" } },
        { $limit : 1 }
    ] )
    

    $eq is only used for in conditions.