Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda