LoginSignup
1
1

More than 3 years have passed since last update.

mongodb

Last updated at Posted at 2016-05-01

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>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1