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

125
Visualizações
Mongoose array query

I've been trying to query my database for a specific array element thats contained inside a user model, the schema looks a bit like this:

const schemaUser = new mongoose.Schema({

    username: { type: String, required: true, unique: true },

    email: {type: String, unique: true},

    password: { type: String, required: true},

    accounts: [{
        id : String,
        account_name : String,
        name : String,
        password : String,
        description: String
    }]

});

How could I go about selecting a specific account out of the "accounts" array using its custom id shown (not the Mongodb one) that is also contained inside a specific users object?

Cheers.

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

0

If you want to query for a record that contains a specific account depending on its id, you could use :

userSchema.findOne({ "accounts.id": { $eq: id }})
about 4 years ago · Juan Pablo Isaza Relatório

0

Since your question revolves around finding the user based on an array, rather than querying the mongoDB, you could use the array.find() method to return the object you want, based on the id property.

More about array.find() here.

Example:

const accountList = [
    {
        id : 1,
        account_name : 'account_one',
        name : 'John Doe',
        password : 'asdhui"#¤7aysdg',
        description: 'dummy account #1'
    },
    {
        id : 2,
        account_name : 'account_two',
        name : 'Jane Doe',
        password : 'asdhui"#¤7aysdg',
        description: 'dummy account #2'
    },
    {
        id : 3,
        account_name : 'account_three',
        name : 'Derrick Johnson',
        password : 'asdhui"#¤7aysdg',
        description: 'dummy account #3'
    }
];

console.log(accountList.find(account => account.id === 1));

Note that the array.find() method will only return the first instance. If you want to do other types of lookups and return a list of users based on something that isn't unique, then you'd have to use the array.filter() method instead.


In the case you did mean to query the mongoDB, you could query for the field value of the specific document directly by specifying the field you want, followed by the $eq operator and value.

Example:

userSchema.findOne({ "id": { $eq: value } })

value being the specific id of the user you want.

More about the $eq operator here.

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