Small But Very Useful And Frequently Used Commond
Step1: Remove lock file.
sudo rm /var/lib/mongodb/mongod.lock
sudo rm-rf mongodb/mongod.lock
Step2: Repair mongodb.
mongod --repair
Step3: Start mongodb.
sudo mongod -dbpath=mongodb
sudo start mongodb
or
sudo service mongodb start
Step4: Check status of mongodb.
sudo status mongodb
or
sudo service mongodb status
Step5: Start mongo console.
mongo
Basic and Important MongoDB Commands
Connect to a Database
To connect to the database server, which runs as mongod, and begin using the mongo shell to select a logical database within the database instance and access the help text in the mongo shell.
Commond : mongod
Task Perform: Connect to DB.
-
Show all DB
Commond : show dbs
Task Perform: Show all DB
Create New DB
Commond : use databaseName
Task Perform: Create new DB
Insert New Document
Insert the j and k documents into the testData collection with the following sequence of operations:
Commond : db.testData.insert( j )
db.testData.insert( j )
Task Perform: Insert new Document
Find Data using mongoDB
Confirm that the documents exist in the testData collectionModifies an existing document or documents in a collection. The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update perameter.
By default, the update() method updates a single document. Set the Multi Parameter to update all documents that match the query criteria. by issuing a query on the collection using the find() method:
Commond : db.testData.find()
Task Perform : Find all the collection data
-
Update Data using mongoDB
Modifies an existing document or documents in a collection. The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update perameter.
By default, the update() method updates a single document. Set the Multi Parameter to update all documents that match the query criteria.
The update() method has the following form:
Commond :
db.collection.update(
,
,
{
upsert: ,
multi: ,
writeConcern:
}
)
NOTE :
upsert
Optional. If set to true, creates a new document when no document matches the query criteria. The default value is false, which does not insert a new document when no match is found.
multi
Optional. If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document. The default value is false.
writeConcern
Optional. A document expressing the write concern . Omit to use the default write concern.
Example
db.collection.update( { "_id.authorID": 1 },
{ "name": "Robert Frost" },
{ upsert: true }
)
$setOnInsert
Commond :
db.collection.update(
<query>,
{ $setOnInsert: { <field1>: <value1>, ... } },
{ upsert: true }
)
If upsert : true , then $setOnInsert assigns the specified values to the fields in the document. If the update operation does not result in an insert, $setOnInsert does nothing.
We can specify the upsert option for either the db.collection.update( ) or db.collection.findAndModify() methods.
-
$set
If the field does not exist, $set will add a new field with the specified value.If there is same field then $set will modify it.
Example
Consider a collection products with the following document:
{
_id: 100,
sku: "abc123",
quantity: 250,
instock: true,
reorder: false,
details: { model: "14Q2", make: "xyz" },
tags: [ "apparel", "clothing" ],
ratings: [ { by: "ijk", rating: 4 } ]
}
db.products.update(
{ _id: 100 },
{ $set:
{
quantity: 500,
details: { model: "14Q3", make: "xyz" },
tags: [ "coats", "outerwear", "clothing" ]
}
}
)
Here we can see we have product collection with same id and we have to modify our quantity, detail and tag field so by using $set we can perform such task.
-
Delete Record using MangoDB
Commond: db.collection.remove(,)
Example:
db.products.remove( { qty: { $gt: 20 } } )
The following operation removes all the documents from the collection products where qty is greater than 20
-
$Unset
The $unset operator deletes a particular field. Consider the following syntax:
Commond: { $unset: { : "", ... } }
Example:
db.products.update(
{ sku: "unknown" },
{ $unset: { quantity: "", instock: "" } }
)
Refrences
http://docs.mongodb.org/manual/reference/method/db.collection.update
http://docs.mongodb.org/v2.4/reference/method/db.collection.update/
0 Comment(s)