Skip to content

Instantly share code, notes, and snippets.

@cahlan
Last active December 2, 2015 17:45
Show Gist options
  • Save cahlan/ab524b5dad979b7997ef to your computer and use it in GitHub Desktop.
Save cahlan/ab524b5dad979b7997ef to your computer and use it in GitHub Desktop.
some sample mongodb/mongoose queries
//given this schema ...
// {
// [id]
// name: String,
// email: String,
// bio: String,
// createdAt: Date,
// age: 11
// }
//order these queries from least efficient to most efficient
User.findOne({createdAt: {'$gt': new Date(2015, 02, 12)}}).exec();
User.findOne({_id: 'aasldkjflkj324'}).exec();
User.find({name: function(name) { return name.charAt(0) === 'F'; }}).exec();
User.findOne({bio: new RegExp('some phrase', 'g').exec();
User.findOne({name: new RegExp('^F'}).exec();
User.findOne({age: 12}).exec();
User.findOne({name: function(name) { return name.charAt(0) === 'F'; }}).exec();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment