-
-
Save maksymx/2cec392c1eb06afe5ea0 to your computer and use it in GitHub Desktop.
Mongo DB - usefull tricks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" } ] } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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} }); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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