Skip to content

Instantly share code, notes, and snippets.

@jbutko
Last active January 15, 2016 13:24
Show Gist options
  • Save jbutko/d19a1e91db8817f2ff7a to your computer and use it in GitHub Desktop.
Save jbutko/d19a1e91db8817f2ff7a to your computer and use it in GitHub Desktop.
#JS, #MongoDB Cheat Sheet
/**
* mongodb cheat sheet
*/
// create db
use tasks
// list dbs
show dbs
// create tasks collection and insert new task into collection
a = {val: 'test'};
db.tasks.save(a);
// find object with id eq to ...
db.tasks.find({'_id': "830957ae-4cb2-474b-aa48-306b5b62ada5"})
// update attribute on object with _id...
db.tasks.update({_id: "830957ae-4cb2-474b-aa48-306b5b62ada5"}, {$set: {'data.attributes.active': true}})
// find objects by nested property
db.tasks.find( { 'data.attributes.userId':"2" });
// number of results
db.tasks.find().length();
// date range lookup ?
db.tasks.find( { 'data.attributes.created': {$gte: ISODate("2015-05-22T14:56:29.000Z"), $lt: ISODate("2015-05-22T14:56:29.000Z")} })
// delete collection
db.tasks.drop();
// find task that fall under project with ID A or ID B
db.tasks.find({$or: [{'data.attributes.projectId': '12'}, {'data.attributes.projectId': '4564564'}]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment