commands
backup MongoDB data
- running on the same machine and on the default port of 27017
- backup data is dumped on dump directory.
mongodump -u <username> -p <password> -h <hostname> --port <port> -d <database> --out <outputfile>
restore MongoDB data
- restore MongoDB data from specified directory
mongorestore -u <username> -p <password> -h <hostname> --port <port> -d <database> <restore data directory path>
start mongod daemon with specified configuration file
mongod --config /usr/local/etc/mongod.conf
execute mongodb command from javascript file
mongo <database host ip or fqdn>/<database name> <javascript file path>
add a new field all documents in a collection
db.<collection name>.update({},{ '$set': {"<field.field": ""} }, false, true)
find minimum value from nested object
document example (sample collection)
{
"_id": ObjectId("xxxxxxxxxx"),
"pair": {
"x": 12,
"y": 34
}
}
{
...
}
find minimum value of x
db.sample.aggregate([
{
$group: {
_id: {},
minX: {
$min: "$pair.x"
}
}
}
])
find minimum value of x and minimum value of y
db.sample.aggregate([{
$group: {
_id: {},
minX: {
$min: "$pair.x"
},
minY: {
$min: "$pair.y"
}
}
}])
GridFS
remove file by name
mongofiles -v -d <database name> delete <filename>