In this Article, I will show How to start quick development with MongoDB.
How to start/stop MongoDB server. How to start MongoDB Shell.
How create and use own collection.
What is MongoDB?
1) MongoDB is an open-source document database that provides
high performance, high availability, and automatic scaling.
2) MongoDB documents are similar to JSON objects.
Simple Steps to be follow?
1) Download latest version from mongodb
2) Extracts to your prefer location, example c:\mongodb
3) Create a folder in C:\data\db
How to Start MongoDB Server :
Go to C:\mongodb\bin> mongod.exe is the command to start MongoDB server.
How to start a mongoDB command shell :
C:\mongodb\bin>mongo.exe is the command to start MongoDB command shell,
Basic MongoDB commands :
1) > show dbs – To list of existing databases.
2) > use
3) > show collections – To list all existing collection in connected database.
Basic Data operations :
Basic Data operations in MongoDB on collections are insert, select,
update and delete.
The document in MongoDB is similar to JSON object.
Sample Order Document is,
{ "address" : { "street" : "2 Avenue", "zipcode" : "10075", "building" : "1480", "coord" : [ -73.9557413, 40.7720266 ], }, "status" : "Ordered", "Items" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "item_id" : "Item101", "count" : 11 }, { "date" : ISODate("2014-01-16T00:00:00Z"), "item_id" : "Item102", "count" : 17 } ], "customer_name" : "Rick GoldMan", "order_id" : "41704620" }
Insert Data :
Insert a document into a collection named orders.
The operation will create the collection if the
collection does not currently exist.
Query Data :
The find() method returns query results in a cursor, which is an iterable
object that yields documents.
To return all documents in a collection, call the find()
method without a criteria document.
db.collection.find() – To get all documents.
> db.orders.find()
db.collection.count() – To get count of documents in specified collection.
> db.orders.count()
Syntax is,
{ <field>: { <any-operator>: <value> } }
ex: { <field>: { $eq: <value> } }
$eq – Matches values that are equal to a specified value.
$gt – Matches values that are greater than a specified value.
$gte – Matches values that are greater than or equal to a specified value.
$lt – Matches values that are less than a specified value.
$lte – Matches values that are less than or equal to a specified value.
$ne – Matches all values that are not equal to a specified value.
$in – Matches any of the values specified in an array.
$nin – Matches none of the values specified in an array.
Find all the orders placed by customer “Rick GoldMan”
> db.orders.find({“customer_name” : “Rick GoldMan” })
Find all the orders should dispatch to zipcode “10075”
> db.orders.find( { “address.zipcode”: “10075” } )
Find all the orders, placed for Item with item_id “Item101”
> db.orders.find( { “Items.item_id”: “Item101” } )
Find all the orders, where Item count is greater than 30 and 10
> db.orders.find( { “Items.count”: { $gt: 30 } } )
> db.orders.find( { “Items.count”: { $gt: 10 } } )
Logical Operators :
Logical operators Logical AND and Logical OR,
Find all the orders placed by customer “Rick GoldMan” and
should dispatch to zipcode “10075”
> db.orders.find( { “customer_name”: “Rick GoldMan”, “address.zipcode”: “10075” } )
Find all the orders placed by customer “Rick GoldMan” or
should dispatch to zipcode “10075”
> db.orders.find(
{ $or: [ { “customer_name”: “Rick GoldMan” }, { “address.zipcode”: “10075” } ] }
)
How to sort the results :
sort() is the method to sort the results based on some field.
Syntax is,
db.<collection_nams>.find().sort( { “<field_name>”: 1/-1, “<field_name>”: 1/-1 … } )
1 for ascending and -1 for descending
Sort by customer_name in ascending order,
db.orders.find().sort( { “customer_name”: 1 } )
Sort by customer_name in decending order,
db.orders.find().sort( { “customer_name”: -1 } )
Sort by customer_name in ascending order first and then sort by address.zipcode in ascending order,
db.orders.find().sort( { “customer_name”: 1, “address.zipcode”: 1 } )
Update Data :
The following operation updates the first document with name equal to “Rick GoldMan”,
The $set operator to update the order field value from ordred to executed.
db.orders.update(
{ “customer_name” : “Rick GoldMan” },
{
$set: { “status”: “Executed” }
}
)
The following updates the street field in the embedded address document.
db.orders.update(
{ “order_id” : “41704620” },
{ $set: { “address.street”: “East 31st Street” } }
)
By default, the update() method updates a single document.
To update multiple documents, use the multi option in the update() method.
db.orders.update(
{ “order_id” : “41704620” },
{ $set: { “address.street”: “East 31st Street” } },
{ multi: true}
)
To update multiple field values,
db.orders.update(
{ “customer_name” : “Rick GoldMan” },
{
$set: { “status”: “Executed”, “address.street”: “East 31st Street” }
}
)
Remove Data :
To removes all documents that match the specified condition.
db.orders.remove( { “customer_name”: “Rick GoldMan” } )
Use the justOne option to limit the remove operation to only
one of the matching documents.
db.orders.remove( { “customer_name”: “Rick GoldMan”, { justOne: true } )
To remove all the documents,
db.orders.remove( { } )
The remove all operation only removes the documents from the collection.
To remove all the collection and any indexes for the collection, remain.
db.orders.drop()
Upon successful drop of the collection, the operation returns true.
*** Venkat – Happy leaning ****