
Everything about creating documents in MongoDB
mongod --dbpath <path>
to change path to databasemongod -- logpath <path>
to change path to log filemongodb config file
- Create config file named mongod.cfg
- Add the following to it
storage:
dbPath: <path>
systemLog:
destoination: file
path: <file path>
- mongod -f <path to config file>
mongod
For running MongoDB server
mongo
For running MongoDB shell
CRUD Operations
Create
insertOne()
db.collection.insertOne({field: "value"});insertMany()
db.collection.insertOne([{field: "value"}, {field: "value"}]);Ordered Insert in MongoDB
db.hobbies.insertMany([{_id: "yoga", name: "Yoga"}, {_id: "cooking", name: "Cooking"}, {_id: "hiking", name: "Hiking"}], {ordered: true})
{ordered: true} is the default behaviour of insert statements
db.hobbies.insertMany([{_id: "yoga", name: "Yoga"}, {_id: "cooking", name: "Cooking"}, {_id: "hiking", name: "Hiking"}], {ordered: true})
Unordered Insert in MongoDB
If you want mongodb to continue trying to insert other documents even after one or more failing due to any reason, you must set ordered to false. See example below:
db.hobbies.insertMany([{_id: "yoga", name: "Yoga"}, {_id: "cooking", name: "Cooking"}, {_id: "hiking", name: "Hiking"}], {ordered: false})
db.hobbies.insertMany([{_id: "yoga", name: "Yoga"}, {_id: "cooking", name: "Cooking"}, {_id: "hiking", name: "Hiking"}], {ordered: false})
WriteConcern
Setting Acknowledge to false
Setting acknowledge to false increases speed but the down side is that you don't get acknowledgement as well as _id if data is inserted successfully. What one can do is fetch data to check.
Example: Run the following statements in mongo shell
db.hobbies.insertOne({name: "Dancing"}, {writeConcern: {w: 0}})
db.hobbies.find().pretty()
Setting Acknowledge to false
This can be explicitly be done but is also the default behavior.
Example: Run the following statements in mongo shell
db.hobbies.insertOne({name: "Trekking"}, {writeConcern: {w: 1}})
db.hobbies.find().pretty()
Adding to Journal before Inserting to DB
Setting Journal to true
Setting journal to true slows down operation as data is first added to journal and from there it is added to db.
db.hobbies.insertOne({name: "Disco Dancing"}, {writeConcern: {w: 1, j: true}})
db.hobbies.find().pretty()
Setting Journal to false
This is the default behavior. So one need not explicitly mention but one can. Setting journal to false directly adds to db.
db.hobbies.insertOne({name: "Listening Music"}, {writeConcern: {w: 1, j: false}})
db.hobbies.find().pretty()
Set Timeout for Create Operation
Setting wtimeout milliseconds
Setting journal to true slows down operation as data is first added to journal and from there it is added to db. The following example sets timeout to 200 milliseconds.
db.hobbies.insertOne({name: "Playing Video Games"}, {writeConcern: {w: 1, j: true, wtimeout: 200}})
db.hobbies.find().pretty()
Not setting wtimeout
This is the default behavior. So one need not explicitly mention. The below example will wait for response even if it takes very long for acknowledgement.
db.hobbies.insertOne({name: "Playing Drums"}, {writeConcern: {w: 1, j: true}})
db.hobbies.find().pretty()
That is all about creating documents in MongoDB
Importing Data from JSON File
One can import data from json files using the following command in terminal.
mongoimport data.json -d <database name> -c <collection name> --jsonArray --drop
Comments
Post a Comment