Monday, February 27, 2017

#1.1 MongoDB basics

1. Installing on archLinux: Install mongodb from official repositories:

# pacman -S mongodb

2. Edit some options of the configuration file in /etc/mongodb.conf

standard file:
# See for format details
# Run mongod --help to see a list of options

bind_ip =
quiet = true
dbpath = /var/lib/mongodb
logpath = /var/log/mongodb/mongod.log
logappend = true

edited file:
   dbPath: /mongodb/data/db
   directoryPerDB: true
   destination: file
   path: "/mongodb/log/mongo.log"
   logAppend: true
   verbosity: 3
   quiet: true
      RESTInterfaceEnabled: true

Note 1: the dbpath and log path refer to /mongodb/data/db and /mongodb/log, respectively. These directories must be created and chown to user mongodb and group daemon;
Note 2: the log path points to a file mongo.log that must be created: touch /mongodb/log/mongo.log;
Note 3: by enabling the REST API, the HTTP interface is also enabled and, as a result, can increase network exposure.

3. Enable/Start the mongodb.service daemon:

# systemctl enable mongodb.service
# systemctl start mongodb.service

4. Basic configuration for non-daemon execution:

# mongod --directoryperdb --dbpath /mongodb/data/db --logpath /mongodb/log/mongo.log --logappend --rest

parameters description:
--directoryperdb -> each database will be stored in a separate directory;
--dbpath -> directory for datafiles - defaults to /data/db;
--logpath -> log file to send write to instead of stdout - has to be a file, not directory;
--logappend -> append to logpath instead of over-writing;
--rest -> turn on simple rest api;

5. To access the Database shell:
# mongo

6. Basic commands for beginners:

- list all existing databases
# show dbs

- create and switch to a new database
# use <db>

- check the current database
# db

- create a new user for the current database
# db.createUser({
roles:["readWrite", "dbAdmin"]

- create a new collection
# db.createCollection('<collection_name>');

- list all collections for the current database
# show collections

- insert a document (record) in the collection
# db.<collection_name>.insert({
first_name: "John",
last_name: "Doe"

- list documents in the collection
# db.<collection_name>.find()
# db.<collection_name>.find().pretty()

- update a document / set (add) a new field
# db.<collection_name>.update({first_name:"John"},{first_name: "John", last_name: "Doe", gender:"male"});
# db.<collection_name>.update({first_name:"John"},{$set:{gender:"male", age: 45}});

- increment function
# db.<collection_name>.update({first_name:"John"},{$inc:{age: 5}});

- unset (remove) a field in the document
# db.<collection_name>.update({first_name:"John"},{$unset:{age:1}});

- upsert option (update if found, or insert if not found)
# db.<collection_name>.update({first_name:"Mary"},{first_name: "Mary", last_name: "Poppings", gender:"female"},{upsert: true});

- rename an attribute (field)
# db.<collection_name>.update({first_name:"Mary"},{$rename:{"gender":"sex"}});

- remove a document (record)
# db.<collection_name>.remove({first_name:"Mary"})
# db.<collection_name>.remove({first_name:"Mary"},{justOne: true})

- search a specific document (record)
# db.<collection_name>.find({first_name: "John"})
# db.<collection_name>.find({$or:[{first_name: "John"},{first_name: "Mary"}]})

- search with options "lower than" or "greater than"
# db.<collection_name>.find({age:{$lt:40}})
other options: lt/lte/gt/gte

- sort documents
# db.<collection_name>.find().sort({first_name:1})
options: 1 (ascending), -1 (descending)

- count documents
# db.<collection_name>.find().count()

- limit the record count (first x documents)
# db.<collection_name>.find().limit(x)

- using forEarch and custom output
# db.<collection_name>.find().forEach(function(doc){print("Customer Name: "+doc.first_name)})


No comments:

Post a Comment