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
sails js mongodb populate

I would like to use .populate() with Sails js and MongoDB and so I did something like this:

I am using the latest version of Sails and MongoDB and Node.

My models look like:

// User.js
module.exports = {
  attributes: {
    name: {
      type: 'string'
    },
    pets: {
      collection: 'pet',
      via: 'owner'
    }
  }
};


// Pet.js
module.exports = {
  attributes: {
    name: {
      type: 'string'
    },

    owner: {
      model: 'user'
    }
  }
};

And I used the following which is the same as the documentation:

User.find()
.populate('pets')
.exec(function(err, users) {
  if(err) return res.serverError(err);
  res.json(users);
});

But I get the following output:

[
  {
    "pets": [],
    "id": "58889ce5959841098d186b5a",
    "firstName": "Foo",
    "lastName": "Bar"
  }
]
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you just need the query for dogs. You could just as easily reverse the query.

Pet.find() //or what you want with {}
 .populate('users')
 .exec(err, petsWithUsers)

But if not:

User.find()
      .populate('pets')
      .exec(err, users) ...

This will return all users (User.find()) and only populate pets of type dog (populate('pets', {type: 'dog'})). In the case you'll have users without dogs in your results.

More information and Reference: here

over 4 years ago · Santiago Trujillo 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