N
N
Nastya090922019-06-13 22:53:57
MongoDB
Nastya09092, 2019-06-13 22:53:57

How to write such a mongodb query?

{
  "_id" : "10096",
  "demands" : [
    {
      "placementId" : "805012517",
    },
    {
      "placementId" : "538610916",
    }
  ]
}
{
  "_id" : "10097",
  "demands" : [
    {
      "placementId" : "805012997",
    },
    {
      "placementId" : "538771776",
    },
    {
      "placementId" : "538796164",
    }
  ]
}

Above is the collection structure in mongo.
Please help me write a Mongo request to get a collection of unique placementIds

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aves, 2019-06-13
@Nastya09092

.aggregate([
  {$unwind: '$demands'},
  {$group: {
    _id: null,
    placementIdList: {$addToSet: '$demands.placementId'}
  }}
])

although it could be easier
.distinct('demands.placementId')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question