distinct with multiple fields and with where condition in mongodb

niren

I want to write a query equivalent to distinct and where in mongodb. the sql query is select DISTINCT key,score from GPC where note="test2" and notetwo = "meet2"

{ "_id" : ObjectId("4dc86fef6a0aa8513ab5f21c"), "key" : "SAGAR","score" : 16, "note" : "test1", "notetwo" : "meet1" } 
{ "_id" : ObjectId("4dc86ffd6a0aa8513ab5f21d"), "key" : "SAGAR456", "score" : 17, "note" : "testjh1", "notetwo" : "meetjh1" } 
{ "_id" : ObjectId("4dc8700b6a0aa8513ab5f21e"), "key" : "SAGAR33", "score" : 37, "note" : "test2", "notetwo" : "meet2" } 
{ "_id" : ObjectId("4dc871686a0aa8513ab5f21f"), "key" : "SAGAR33", "score" : 37, "note" : "test2", "notetwo" : "meet2" } 
{ "_id" : ObjectId("4dc871696a0aa8513ab5f220"), "key" : "SAGAR33", "score" : 37, "note" : "test2", "notetwo" : "meet2" } 
{ "_id" : ObjectId("4dc8716c6a0aa8513ab5f221"), "key" : "SAGAR456", "score" : 17, "note" : "testjh1", "notetwo" : "meetjh1" } 

Expected result from the query is

[{"key" : "SAGAR33", "score" : 37}]

What is the equivalent query in mongodb. I am using mongoose to execute queries.

Anand Jayabalan

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"}}
])

Output:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MongoDB multiple condition in WHERE clause

From Dev

MongoDB multiple condition in WHERE clause

From Dev

MongoDB Distinct with condition in Java

From Dev

MySql Distinct with multiple fields

From Dev

MongoDB select distinct and where

From Dev

MongoDB select distinct and where

From Dev

Multiple distinct counts with where

From Dev

Mongodb find distinct count with condition

From Dev

SQL multiple where and fields

From Dev

mongodb distinct of groupby multiple keys

From Dev

mongodb distinct of groupby multiple keys

From Dev

Multiple Where in Condition in Codeigniter

From Dev

Grouping by multiple fields in MongoDB

From Dev

Grouping by multiple fields in MongoDB

From Dev

MongoDB counts multiple fields

From Dev

mongoDB Join on multiple fields

From Dev

Distinct values from various fields in MongoDB collection

From Dev

Mongodb find distinct array fields and count them

From Dev

MongoDB - Join on multiple condition

From Dev

Multiple limit condition in mongodb

From Dev

search and replace multiple fields condition

From Dev

MySQL LIKE multiple fields condition

From Dev

Select multiple fields from columns with distinct and limit

From Dev

How to get the distinct of multiple fields in MySQL?

From Dev

count multiple distinct fields by group with Mongo

From Dev

django count distinct values of multiple fields

From Dev

Select multiple fields from columns with distinct and limit

From Dev

How to get the distinct of multiple fields in MySQL?

From Dev

Treating multiple delimiter separated fields as distinct rows