0

Ich möchte eine MAX-Abfrage in MongoDB mit Where-Klausel erstellen.Max Abfrage in Mongodb mit Where-Klausel

Ich versuche, diese

db.Collection.aggregate({$group : {_id : "$P_ID", massi : {$max : "$b_val"}}},{P_ID:'XYZ',experiment:'abc'}); 

Meine WHERE-Klausel {P_ID:'XYZ',experiment:'abc'} ist

Ich weiß nicht, warum, aber diese Abfrage nicht funktionieren diesen Fehler erhalten:

assert: command failed: { 
    "ok" : 0, 
    "errmsg" : "A pipeline stage specification object must contain   exactly one field.", 
    "code" : 16435 
} : aggregate failed 
[email protected]/mongo/shell/utils.js:25:13 
[email protected]/mongo/shell/assert.js:13:14 
[email protected]/mongo/shell/assert.js:287:5 
[email protected]/mongo/shell/collection.js:1312:5 
@(shell):1:1 

    2016-10-15T11:55:31.459+0200 E QUERY [thread1] Error: command failed: { 
     "ok" : 0, 
     "errmsg" : "A pipeline stage specification object must contain  exactly one field.", 
    "code" : 16435 
} : aggregate failed : 
[email protected]/mongo/shell/utils.js:25:13 
[email protected]/mongo/shell/assert.js:13:14 
[email protected]/mongo/shell/assert.js:287:5 
[email protected]/mongo/shell/collection.js:1312:5 
@(shell):1:1 

Antwort

0

Where-Klausel muss in einer $match Bühne sein. Außerdem ist es besser, die Pipeline als ein Array als separate Argumente zu haben.

Die Abfrage muss so aussehen.

db.Collection.aggregate([ 
    { 
     $match : {P_ID:'XYZ',experiment:'abc'} 
    }, 
    { 
     $group : { 
      _id : "$P_ID", 
      massi : {$max : "$b_val"} 
     } 
    } 
]); 
0

Wenn Sie sicher, dass die Tatsache, dass Ihre Abfrage, die Sie nur einen einzelnen Datensatz geben, könnten Sie sortieren & Limit verwenden Ihr Ergebnis wie dieses zu bekommen.

db.collection.find({P_id:"XYZ",experiment:'abc'}).sort({"bVal": -1}).limit(1)