Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
MongoDB - Get arrays with distinct

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!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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 ...

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!