Mongoose populate vs object nesting

The first thing to understand about mongoose population is that it is not magic, but just a convenience method that allows you to retrieve related information without doing it all yourself. The concept is essentially for use where you decide you are going to need to place data in a separate collection rather than embedding … Read more

Moongoose aggregate $match does not match id’s

Your ids variable will be constructed of “strings”, and not ObjectId values. Mongoose “autocasts” string values for ObjectId into their correct type in regular queries, but this does not happen in the aggregation pipeline, as in described in issue #1399. Instead you must do the correct casting to type manually: ids = ids.map(function(el) { return … Read more

Mongoose: findOneAndUpdate doesn’t return updated document

Why this happens? The default is to return the original, unaltered document. If you want the new, updated document to be returned you have to pass an additional argument: an object with the new property set to true. From the mongoose docs: Query#findOneAndUpdate Model.findOneAndUpdate(conditions, update, options, (error, doc) => { // error: any errors that … Read more

Mongoose always returning an empty array NodeJS

The call to mongoose.model establishes the name of the collection the model is tied to, with the default being the pluralized, lower-cased model name. So with your code, that would be ‘models’. To use the model with the files collection, change that line to: var Model = mongoose.model(“Model”, fileSchema, “files”); or var Model = mongoose.model(“file”, … Read more

Mongoose and multiple database in single node.js project

According to the fine manual, createConnection() can be used to connect to multiple databases. However, you need to create separate models for each connection/database: var conn = mongoose.createConnection(‘mongodb://localhost/testA’); var conn2 = mongoose.createConnection(‘mongodb://localhost/testB’); // stored in ‘testA’ database var ModelA = conn.model(‘Model’, new mongoose.Schema({ title : { type : String, default : ‘model in testA database’ … Read more

Why can’t you modify the data returned by a Mongoose Query (ex: findById)

For cases like this where you want a plain JS object instead of a full model instance, you can call lean() on the query chain like so: Survey.findById(req.params.id).lean().exec(function(err, data){ var len = data.survey_questions.length; var counter = 0; _.each(data.survey_questions, function(sq){ Question.findById(sq.question, function(err, q){ sq.question = q; if(++counter == len) { res.send(data); } }); }); }); This … Read more

Find document with array that contains a specific value

As favouriteFoods is a simple array of strings, you can just query that field directly: PersonModel.find({ favouriteFoods: “sushi” }, …); // favouriteFoods contains “sushi” But I’d also recommend making the string array explicit in your schema: person = { name : String, favouriteFoods : [String] } The relevant documentation can be found here: https://docs.mongodb.com/manual/tutorial/query-arrays/