authentication
https://stackoverflow.com/questions/41615574/mongodb-server-has-startup-warnings-
access-control-is-not-enabled-for-the-dat
https://www.mongodb.com/docs/v4.2/tutorial/enable-authentication/
Data Directory: /var/lib/mongo
Log Directory: /var/log/mongodb
1. To connect remote host and localsystem
method1: mongo --host 10.91.0.213 --port 27017
localsystem: mongo
2. To show list of Databases
> show dbs;
3. To create new database;
> use mydb; (If there is no database, it will create it)
4. to show which db you are connected;
> db;
5. To create collections #Whereas mycollection is table nam
> db.mycollection.insert({"Name" : "Avi"});
6. List of documents in collections #Whereas mycollection is table name
> db.mycollection.find();
{ "_id" : ObjectId("646b347b29db3d5eee20df77"), "name" : "Avi" }
7. List collections (Like list of tables in database)
> show collections;
8. Drop database (Enre you are connected to #mydb and then execute dropDatabase
command)
> db;
mydb
> db.dropDatabase();
{ "ok" : 1 }
>
9. Drop a single collections
> show collections;
emp
> db.emp.drop();
true
> show collections;
>
10. Operations Create, Read, Update and Delete Operations
Insert Operation:
db.emp.insertOne({"emp_name" : "Avi", "dept" : "IT"});
db.emp.insertMany([{"emp_name" : "emp1" , "dept" : "IT" , "salary in usd" : 10.1},
{"emp_name" : "emp2" , "dept" : "HR" , "salary in usd" :
10.2}]);
Read Operation:
db.emp.find({"dept" : "IT"});
Update Operation:
db.emp.updateOne({"salary in usd" : 15}, {$set: {"salary in usd" : "16"}});
db.emp.updateMany({"dept" : "IT"}, { $set: {"salary in usd" : "13"}});
Delete Operation:
db.emp.deleteOne({"emp_name" : "emp1"});
11. Rename collection name
> show collections;
emp
newcollection
>
> db.emp.renameCollection("names");
{ "ok" : 1 }
> show collections;
names
newcollection
>
11. import json document to db
[root@localhost ~]# mongoimport --db newdb --collection newcollection --file
/root/zips.json
output:
2023-05-23T10:04:17.770+0530 connected to: mongodb://localhost/
2023-05-23T10:04:18.933+0530 29353 document(s) imported successfully. 0
document(s) failed to import.
(Whereas "newdb" is "database name", "newcollection" is "collection name")
12. mongoexport - in human-readable format. The data is in JSON format and CSV
format
used to Single collection
Syntax: mongoexport --host <hostname> --username <user_name> --password <password>
--db <database name> --collection <collection name> --out <output file>
example: mongoexport --db newdb --collection newcollection --out
/root/mongodump/newcollection.json --jsonArray
mongoexport --db newsun --collection newcollsun --out
/root/dump/newcollsun.json
14. mongoimport - restores the documents from the JSON file into the Mongo
collection
Syntax: mongoimport --host <hostname> --username <user_name> --password <password>
--db <database name> --collection <collection name> --file <imput file>
Example: mongoimport --db newsun --collection newcollsun --file
/root/mongodump/newcollection.json --jsonArray
mongoimport --db tuesday --collection twentyfive --file
/root/dump/newcollsun.json
Error: Failed: cannot decode array into a primitive.D
Solution: will get above error while imporing json document without option "--
jsonArray", when we exported with "--jsonArray" option.
15. mongodump - backups entire server, database or collections in binary format.
mongodump only runs without any arguments
Syntax: mongodump --host hostname --port 27017
# mongodump --out /root/data/
(It will take all database dump)
# mongodump --db deika --out /root/data/ (It
will take single database dump)
# mongodump --db deika --collection one --out /root/data/ (It will
take backup of single collection of a database)
# mongodump --db deika --excludeCollection two --out /root/data/ (It backups
databse and exclude specific collection)
# mongodump --db deika --archive=/root/data/deika.archive (dump
database in archive format)
# mongodump --db=deika --gzip --out=/root/data/deika.zip (Compress
the Output)
16. mongorestore - restore binary backup created by mongodump
# mongorestore --db deika /root/data/deika
(Mention directory name completely)
# mongorestore --db deika --collection two /root/data/deika/two.bson (To restore
single collections from bson backup)
# mongorestore --gzip --nsInclude=deika.two /root/data/deika.zip/deika/two.bson.gz
(Restore single collection name from zipeed bson backup)
16.
-----------------------------------------------------------------------------------
-------------------------------
3. To take export
Syntax: mongodump -d <database name> -o <backup-folder>
IonIdea: mongodump --host 10.91.0.213 --port 27017 -d deikaa -o
/tmp/deikaa_dump_24Feb2023
4. To restore database (to import)
Syntax: mongorestore -d <database name> --dir <backup-folder>
IonIdea: mongorestore --host 10.91.0.213 --port 27017 -d deikaa --dir <backup-
folder>
mongorestore --host 10.91.0.213 --port 27017 deikaa_dump
5. To drop database
> use mydb;
> db.dropDatabase();
/home/manju/deikaaprod/deikaadb/deikaa
mongorestore --host 10.91.0.213 --port 27017 -d deikaa --dir
/home/manju/deikaaprod/deikaadb/deikaa