title | description | created | updated |
---|---|---|---|
MongoDB |
MongoDB is a cross platform document oriented NoSQL database. |
2019-06-17 |
2019-06-17 |
MongoDB is a cross platform document oriented NoSQL database.
On Mac
brew install mongodb
On Ubuntu (18.04)
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
For other platforms you can get details here https://docs.mongodb.com/manual/administration/install-community/
Mongo Shell is easiest way to experiment with MongoDB in the beginning.
You can login to shell by running command mongo
- list all databases
show dbs
- create/ login to a database
use <database_name>
* create collection
```sh
db.createCollection('<collection_name>')
Lets take an use case, using which we can go over all MongoDB commands. Lets build a 'Car Information' application which stores all cars with its specifications.
using insert
you can either insert one document or array of documents
db.cars.insert({ "name" : "Volvo xc 60", "type" : "SUV" });
db.cars.insert(
[
{ "name" : "Volvo xc 60", "type" : "SUV" },
{ "name" : "Volvo xc 90", "type" : "SUV" },
{ "name" : "Volvo S90", "type" : "SUV" }
]
);
Inserts one document
db.cars.insert({ "name" : "Volvo xc 60", "type" : "SUV" });
Inserts multiple documents
db.cars.insertMany(
[
{ "name" : "Volvo xc 60", "type" : "SUV" },
]
);
Upsert a document. If you provide _id which is already exist then it updates the document.
db.cars.save( { _id: 3, "name" : "Volvo xc 60", "type" : "SUV"} );
Updates one or more than one document(s) in collection based on matching document and based on multi
option
Syntax:
db.cars.update(
<query>,
<update>,
<options>
);
Example:
db.cars.update(
{ type: "SUV" },
{ $set: { size : 5} },
{ multi: true}
)
Updates a single document based on matching query
Updates multiple documents based on query
Replaces entire content of document except _id field
Updates a single document. Following are some of the options
upsert
: When true inserts a document.
returnNewDocument
: when true returns new document instead original document. When upsert is true and returnNewDocument is false then null will be returned in case of new document insertion.
Returns one document matching the given query
db.collection.findOne({ _id : 123}); # finding a document with _id
db.collection.findOne({ type : "SUV"}); # return one SUV
findOne with projection (limiting the fields to return)
db.collection.findOne({ type : "SUV"}, {name: 1}); # returns _id & name fields only
Returns a cursor with selected documents
db.collection.find({ type : "SUV"}); # returns all SUVs
-
Deletes a Single document from collection
-
Deletes all documents with matching filter
We can control the consistency and isolation properties of data reads ( from replica sets & replica set shards) using readConcern
option
Read Concern Levels:
- local: data returns from the instace without guaranteing that its been written to majority of replica members Default for reads against primary & reads against secondaries
- available: similar to local, gives lowest latancy for sharded collections. Default for reads against secondaries
- majority: only if the data acknowledged by a majority of the replica set members.
- linearizable: return data after all successful majority-acknowledged writes
- snapshot: Only available for transactions on multi documents.
Using Write Concern
we can set the level of acknowledgment for a given write operation.
This will be sent to mongod and mongos (in case of sharded collections)
Following are the fields that specify write concern
{
w : <number>,
j : <boolean>,
wtimeout: <milli seconds>
}
w lavlues: 0: No acknowledgement requested. 1: This requests for an acknowledgement that is propegaded to standalone mongod or primary in replica set Note: This is the default write concern value for MongoDB Greater than 1: Requests acknowledgement from primary & given number -1 of secondaries
j option: This requests acknowledgement on whether or not the write operation written onto on-disk journal files
wtimeout: Operations will return error after specified limit, this prevent the client waiting indefinitely. On failure MongoDB does not rollback the data, data may eventually get stored
We can't drop this index
help