Node js

Mongoose js query cheatsheet

Mongo db Mongoose queries cheat sheet

Estimated reading time: 1 minute

Mongoose is a schema-based wrapper for Mongodb. Writing some of the basic functionality into Mongodb can be tiresome and painful. Mongoose has built incasting, validation, query building a business logic

Unfortunately, the documentation for Mongoose is not as straight forward as the application is to use. This article can be used as a reference to the most common Mongoose queries, with syntax definition and examples.

Example schema

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new Schema({
    username: String,
    email : String,
    password : String,
    registration_date : {type: Date, default: Date.now },
    friends : [{
        friend_id : mongoose.Schema.ObjectId
    }]
});

var User = mongoose.model('User', userSchema);
module.exports = User;
);

Find

‘Find’ syntax

User.find(query, fields, options, callback);

In the above syntax example, both fields and options are optional.

‘Find’ example

User.find({ 'username': 'an example username' }, function (err, user) {
    if(err){
        throw err;
    }
    if(user){
        console.log('User found: ' + JSON.stringify(user));
    }else{
        console.log('No user found');
    }
});

Find the username for each user

User.find({}, 'username', function (err, user) {
    if(err){
        throw err;
    }
    if(user){
        console.log('User found: ' + JSON.stringify(user));
    }else{
        console.log('No user found');
    }
});

findOne

Mongoose ‘findOne’ syntax

User.findOne(query, fields, options, callback);

findById

Mongoose ‘findById’ syntax

User.findById(query, fields, options, callback);

‘findById’ example

User.findById('58405e43f905162fe4604fc1', function (err, user) {
    if(err){
        throw err;
    }
    if(user){
        console.log('User found: ' + JSON.stringify(user));
    }else{
        console.log('No user found');
    }
});

findOneAndRemove

Mongoose ‘findOneAndRemove’ syntax

User.findOneAndRemove(query, options, callback);

‘findOneAndRemove’ example

User.findOneAndRemove('username' : 'Tom', function (err, user) {
    if(err){
        throw err;
    }
    if(user){
        console.log('User found and removed');
    }else{
        console.log('No user found');
    }
});

findOneAndUpdate

Mongoose ‘findOneAndUpdate’ syntax

User.findOneAndUpdate(query, options, callback);

‘findOneAndUpdate’ example

User.findOneAndUpdate('username' : 'Tom', function (err, user) {
    if(err){
        throw err;
    }
    if(user){
        console.log('User found and updated: ' + JSON.stringify(user));
    }else{
        console.log('No user found');
    }
});

count

Mongoose ‘count’ syntax

User.find({ 'username': 'an example username' }, function (err, user) {
    if(err){
        throw err;
    }
    if(user){
        console.log('User found: ' + JSON.stringify(user));
    }else{
        console.log('No user found');
    }
});

Top