Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda