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

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

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 Denunciar

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 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