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

222
Visualizações
Using MongoDB query to show all items in a Collection with a specific value (DiscordJS v13)

I'm trying to make my last command for my discord bot (Discord.js v13), a marketplace with a MongoDB Collection called "Perso".

The problem is that I really don't know how to make it work, even after reading MongoDB and mongoose documentation, also searching similar (not really) problems on Stack Overflow.

Here is the little piece of code I did to test a return of value :

    let persoOnSale = await Perso.count({ onSale: 1}); // There are 2 "Perso" with the "onSale" field equals to 1.
    let perso = Perso.find({ onSale: 1}, 'name value').sort({value: 1});


     for (let index = 0; index < persoOnSale; index++) {
         const element = perso[index];
         console.log(element); // element return "undefined", 2 times.
        
     }

Here is the console.log return when it's console.log(perso) :

[
  {
    _id: new ObjectId("61fc60165a048fa5700de25d"),
    name: 'Perso Name',
    value: 160
  },
  {
    _id: new ObjectId("61fbe486ef3f2c2b31c57dc5"),
    name: 'Perso Name 2',
    value: 10000
  }
]

I was trying to show all the "Perso" with (onSale: 1), showing their name and value, and sort them in order to show the "Perso" with the least value amount first.

First of all, I imagine I need to put an index on the "perso" array, but I clearly don't know how to do that.

I'd be really thankful for your help.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I just found out.

I have to put an await before "Perso.find"

let perso = await Perso.find({ onSale: 1}, 'name value').sort({value: 1});

(Now, the "Persos" are found).

Then, I have to add that :

const element = perso[index].name;
const value = perso[index].value;
about 4 years ago · Juan Pablo Isaza Relatório

0

I guess your problem comes from the usage of findOne() instead of find()

Try this:

    let persoOnSale = await Perso.count({ onSale: 1});
    let perso = Perso.find({ onSale: 1}, 'name value').sort({value: 1});


     for (let index = 0; index < persoOnSale; index++) {
         const element = perso[index];
         console.log(element);
        
     }
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