Home > Software design >  MongoDB - Get arrays with distinct
MongoDB - Get arrays with distinct

Time:11-25

I'm trying to get all the values(which are arrays) of the "coordinate" type in my data like that:

[
["51.50064317423898","-0.09372711181640626"],
["51.48465408363687","-0.13149261474609378"]
]

I tried db.collections("mydb").distinct("coordinate") but i get:

[
  '-0.09372711181640626',
  '-0.13149261474609378',
  '51.48465408363687',
  '51.50064317423898'
]

Does anyone have an idea how i can just have all my arrays like i want and not ordered in one array? "mydb" looks like this:

{
"name":"dfbfdbf",
"coordinate":["51.50064317423898","-0.09372711181640626"],
"rating":"8",
"description":"geojzglijsen"
},
{
"name":"qzfgs",
"coordinate":["51.48465408363687","-0.13149261474609378"],
"rating":"5",
"description":"femkndsmnk"
}

Thank you!

CodePudding user response:

If I've understood correctly you can try this aggregate query:

The point is $group by null to get all values and use $addToSet which prevents the duplicates values.

db.collection.aggregate([
  {
    "$group": {
      "_id": null,
      "coordinate": {
        "$addToSet": "$coordinate"
      }
    }
  },
  {
    "$project": {
      "_id": 0
    }
  }
])

Example here where I've duplicated one object to see how the repeated value is not displayed.

CodePudding user response:

If you need to fetch distinct coordinates from your collection:

mongos> db.a.find()
{ "_id" : ObjectId("619ea12e032deead586f3f91"), "name" : "dfbfdbf", "coordinate" : [ "51.50064317423898", "-0.09372711181640626" ] }
{ "_id" : ObjectId("619ea138032deead586f3f92"), "name" : "a", "coordinate" : [ "51.50064317423898", "-0.09372711181640626" ] }
{ "_id" : ObjectId("619ea14c032deead586f3f93"), "name" : "a", "coordinate" : [ "51.50064317423898", "-0.19372711181640626" ] }
{ "_id" : ObjectId("619ea157032deead586f3f94"), "name" : "a", "coordinate" : [ "51.50064317423898", "-0.09372711181640626" ] }
{ "_id" : ObjectId("619ea15b032deead586f3f95"), "name" : "a", "coordinate" : [ "52.50064317423898", "-0.09372711181640626" ] }
{ "_id" : ObjectId("619ea160032deead586f3f96"), "name" : "a", "coordinate" : [ "52.50064317423898", "-0.09372711181640626" ] }
mongos> db.a.aggregate([ 
 { $addFields:{coordinates:{$reduce:{ input: {$slice:["$coordinate",1,{$size:"$coordinate"} ]},initialValue:{$arrayElemAt:["$coordinate",0]},in:{$concat:["$$value",";","$$this" ]}    }} }}    ,
 { $group:{_id:"$coordinates" , cnt:{$sum:1}}} ,
 { $project : { coordinate : { $split: ["$_id", ";"] } ,_id:0}}          
 ])
{ "coordinate" : [ "51.50064317423898", "-0.19372711181640626" ] }
{ "coordinate" : [ "51.50064317423898", "-0.09372711181640626" ] }
{ "coordinate" : [ "52.50064317423898", "-0.09372711181640626" ] }
mongos> 

Example: here

explained: Remove duplicate coordinates from collection

  1. create new field coordinates where you join the coordinates in single string
  2. group the coordinates to remove duplicates
  3. split the coordinates to the original values.

This solution has no limitation of distinct coordinates array summary size of 16MB as well ...

  • Related