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

241
Visualizações
How can I list all the records to see if they are duplicated?

I have a problem that I cannot resolve. I have a table in MongoDB, and this is structure:

const shopEconomy = new mongoose.Schema({
    guildID: { type: String },
    name: { type: String },
    value: { type: Number },
    description: { type: String },
    rolereq: { type: String },
    roleadd: { type: String },
    roleremove: { type: String },
    buyinfo: { type: String }
});

I need to list all names from the table (shopData.name) and then check if the typed name exists in the database. I tried to do something like the one below, but it doesn't work.

const shopData = await shopEconomy.find({ guildID: message.guild.id });

        let categories = [];
        let data = new Object();
        for(const i in shopData){
            data += `${shopData[i].name}\n`

            categories.push(data)
        }

Could someone take a look at this and help me out?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Part of the issue here comes from the use of a for...in loop which treats shopData as an object and loops over all properties of it. Instead try using a for...of loop which treats shopData as an array and loops over all objects in it.

...

for(const i of shopData) {
    data += `${i.name}\n`

    ...
}

See also this question on for...in vs for...of and this question on JavaScript loops.

about 4 years ago · Santiago Trujillo Relatório

0

The title of the question does not quite match the description of the question. Given the description, let's assume the typed name is assigned to var typedName.
Let's also assume that you have bound your shopEconomy schema to a model that will actually interact with a mongodb collection called shopData. Then this will iterate all the docs in the shopData:

var found = false;
cursor = db.shopData.find();  // get EVERYTHING
cursor.forEach(function(doc) {
  print(doc['name']);
  if(doc['name'] == typedName) {
    found = true;
  }
});
if(found) {
  print(typedName,"was found");
}

It is likely that the OP wants to find duplicate name in the collection, for which this pipeline will work:

db.shopData.aggregate([
    {$group: {_id: '$name', N:{$sum:1}} },
    {$match: {'N':{$gt:1}}}
]);
about 4 years ago · Santiago Trujillo 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