2018-05-30

MongoDB - Multiple fields/keys distinct list

db.COLLECTION_NAME.aggregate([

// Filter
{"$match":{
FIELD1 : VALUE1
, FIELD2 : VALUE2
}}

// Distinct fields list
, {"$group": {
  "_id": {
  FIELD3_ALIAS : "$field3"
  , FIELD4_ALIAS :"$field4"
  }
}}

// Replace root, so the documents are not nested inside an _id sub-doc
   ,{ $replaceRoot: {
     newRoot: "$_id"
   }}

]);//aggregate

No comments:

Post a Comment