且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与多个字段和mongodb中的where条件不同

更新时间:2022-04-28 02:57:26

您需要使用

You'll need to use the aggregate queries for achieving this. Here's an example that will work in shell (which can be translated to Mongoose easily):

db.gpc.aggregate([
    // your where clause: note="test2" and notetwo = "meet2"
    {"$match" : {note:"test2", notetwo:"meet2"}}, 
    // group by key, score to get distinct
    {"$group" : {_id : {key:"$key", score:"$score"}}}, 
    // Clean up the output
    {"$project" : {_id:0, key:"$_id.key", score:"$_id.score"}}
])

输出:

{ "result" : [ { "key" : "SAGAR33", "score" : 37 } ], "ok" : 1 }