Skip to content

Instantly share code, notes, and snippets.

@maksymx
Forked from veysiertekin/db.create.user.js
Created February 10, 2016 08:21
Show Gist options
  • Save maksymx/2cec392c1eb06afe5ea0 to your computer and use it in GitHub Desktop.
Save maksymx/2cec392c1eb06afe5ea0 to your computer and use it in GitHub Desktop.
Mongo DB - usefull tricks
// select db
use test;
// create a user with db role(s) (http://docs.mongodb.org/manual/reference/built-in-roles/)
db.createUser( { user: "test", pwd: "changeit", roles: [ { role: "dbOwner", db: "test" } ] } );
// save (create)
// http://docs.mongodb.org/manual/reference/method/db.collection.save/
db.people.save({name: "Bob", surname: "Smith", age: 15 });
// find (read/retrieve)
// http://docs.mongodb.org/manual/reference/method/db.collection.find/
db.people.find({name: "Bob"});
db.people.find({age : 15});
db.people.find({age : { $lt : 18 }});
db.people.find({age : { $gt : 10 }});
db.people.find({surname : { $regex : "^S" }});
// update (update)
// http://docs.mongodb.org/manual/reference/method/db.collection.update/
db.people.update({ name: "Bob" }, { name : "James"});
db.people.update({ name: "Bob" }, { name : "James"}, {multi : true});
// remove (delete)
// http://docs.mongodb.org/manual/reference/method/db.collection.remove/
db.people.remove({ age : { $gte : 10, $lte : 20} });
#!/bin/sh
# create dump
# http://docs.mongodb.org/manual/reference/program/mongodump/
mongodump --host localhost --port 27017 --username user --password pass --out /home/user/Desktop/my.dump.archive
# restore dump
# http://docs.mongodb.org/manual/reference/program/mongorestore/
mongorestore --host localhost --port 27017 --username user --password pass /home/user/Desktop/my.dump.archive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment