MongoDB count() versus countDocuments()

The db.collection.find method returns a cursor. The cursor.count() method on the cursor counts the number of documents referenced by a cursor. This is same as the db.collection.count(). Both these methods (the cursor.count() and db.collection.count()) are deprecated as of MongoDB v4.0. From the documentation: MongoDB drivers compatible with the 4.0 features deprecate their respective cursor and … Read more

MongoDB $lookup vs Mongoose populate

Thanks to a github thread shared by GrĂ©gory NEUT in the question’s comments I have been able to establish certain facts: Mongoose’s populate() method does not use MongoDB’s $lookup behind the scenes. It simply makes another query to the database. Mongoose does not have functionalities that MongoDB does not have. populate() just makes two or … Read more

mongoose recursive populate

you can do this now (with https://www.mongodb.com/blog/post/introducing-version-40-mongoose-nodejs-odm) var mongoose = require(‘mongoose’); // mongoose.Promise = require(‘bluebird’); // it should work with native Promise mongoose.connect(‘mongodb://……’); var NodeSchema = new mongoose.Schema({ children: [{type: mongoose.Schema.Types.ObjectId, ref: ‘Node’}], name: String }); var autoPopulateChildren = function(next) { this.populate(‘children’); next(); }; NodeSchema .pre(‘findOne’, autoPopulateChildren) .pre(‘find’, autoPopulateChildren) var Node = mongoose.model(‘Node’, NodeSchema) var … Read more

Mongoose select fields to return from findOneAndUpdate

From the manual, the options argument needs a “fields” key in it since there are other details such as “upsert” and “new” where this applies. In your case you also want the “new” option: User.findOneAndUpdate( { “_id”: “132324” }, { “$set”: { “hair_color”: “yellow” } }, { “fields”: { “first_name”:1, “last_name”: 1 }, “new”: true … Read more

Populate a mongoose model with a field that isn’t an id

This is supported since Mongoose 4.5, and is called virtuals population. You have to define your foreign keys relationships after your schemas definitions and before creating models, like this: // Schema definitions BookSchema = new mongoose.Schema({ …, title: String, authorId: Number, … }, // schema options: Don’t forget this option // if you declare foreign … Read more

Random document from a collection in Mongoose

I found this Mongoose Schema static function in a GitHub Gist, which should achieve what you are after. It counts number of documents in the collection and then returns one document after skipping a random amount. QuoteSchema.statics.random = function(callback) { this.count(function(err, count) { if (err) { return callback(err); } var rand = Math.floor(Math.random() * count); … Read more

JavaScript NoSQL Injection prevention in MongoDB

Sushant‘s answer is not correct. You need to be aware of NoSQL injection in MongoDB. Example (taken from here) User.findOne({ “name” : req.params.name, “password” : req.params.password }, callback); If req.params.password is { $ne: 1 }, the user will be retrieved without knowing the password ($ne means not equals 1). MongoDB Driver You can use mongo-sanitize: … Read more