Lab#11 Mongodb Basic CRUD Commands
Lab#11 Mongodb Basic CRUD Commands
Basic Commands
1. Version check
The foremost command is to check the installed version of the MongoDB server and
Mongo Shell. Run this command on the terminal on Linux or CMD prompt on
windows.
mongod --version
This simple command help to create a new database if it doesn’t exist or help to
switch to the existing Database. In MongoDB “test” is default database hence users
use “test” DB once Mongo Shell is logged in.
use DB_Name
show dbs
> db
GeekFlare
Copy
The given command helps the user to drop the required database. Run the
command on MongoDB client. Please make sure to select the Database before
running the drop command. Otherwise, it will drop the default “test” Database.
db.dropDatabase()
Let's first list out all the database, switch to one of them and then drop it
8. Create Collection
Syntax: db.createCollection(Name,Options)
Example:
9. Drop Collection
It returns true for successful drop and false in case of any error or if DB doesn’t exist.
Syntax: collectionName.drop()
Example:
Example:
> db.geekFlareCollection.insertOne( {
code: "P123", Qty: 200, status: "Active"
});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ed309725429283aee2e134d")
}
Copy
Syntax:
Example:
db.geekFlareCollection.insertMany([
... { code: "P1", Qty: 100, status: "Active"},
... { code: "P2", Qty: 200, status: "Active"},
... { code: "P3", Qty: 0, status: "Dective"}
... ]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5edf7b4e18b2c26b9dfe8cac"),
ObjectId("5edf7b4e18b2c26b9dfe8cad"),
ObjectId("5edf7b4e18b2c26b9dfe8cae")
]
}
> db.geekFlareCollection.find()
{ "_id" : ObjectId("5edf546fdfa12b33b7cb75b8"), "product" :
"bottles", "Qty" : 100 }
{ "_id" : ObjectId("5edf546fdfa12b33b7cb75b9"), "product" : "bread",
"Qty" : 20 }
{ "_id" : ObjectId("5edf546fdfa12b33b7cb75ba"), "product" :
"yogurt", "Qty" : 30 }
{ "_id" : ObjectId("5edf7b4e18b2c26b9dfe8cac"), "code" : "P1", "Qty"
: 100, "status" : "Active" }
{ "_id" : ObjectId("5edf7b4e18b2c26b9dfe8cad"), "code" : "P2", "Qty"
: 200, "status" : "Active" }
{ "_id" : ObjectId("5edf7b4e18b2c26b9dfe8cae"), "code" : "P3", "Qty"
: 0, "status" : "Dective" }
>
To search for the document stored in a collection find() method can be used. The
below command will be used to retrieve all the documents from the collection.
Syntax: collectionName.find()
Example:
> db.geekFlareCollection.find()
{ "_id" : ObjectId("5ed31186b6f2c2bb1edb86ce"), "code" : "P1", "Qty"
: 200, "status" : "Active" }
{ "_id" : ObjectId("5ed31186b6f2c2bb1edb86cf"), "code" : "P2", "Qty"
: 200, "status" : "Active" }
{ "_id" : ObjectId("5ed31186b6f2c2bb1edb86d0"), "code" : "P3", "Qty"
: 200, "status" : "Active" }
{ "_id" : ObjectId("5ed3159eb6f2c2bb1edb86d1"), "code" : "P4", "Qty"
: 100, "status" : "Inactive" }
Copy
Syntax: collectionName.find({ condition })
Example:
Syntax: collectionName.findOne()
Example:
> db.geekFlareCollection.findOne();
{
"_id" : ObjectId("5ed31186b6f2c2bb1edb86ce"),
"code" : "P1",
"Qty" : 200,
"status" : "Inactive"
}
Copy
Syntax: collectionName.find().pretty()
Example:
Syntax: collectionName.update({KeyToUpdate},{Set Command})
Example:
> db.geekFlareCollection.find()
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfb"), "product" :
"bottles", "Qty" : 100 }
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfc"), "product" : "bread",
"Qty" : 20 }
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfd"), "product" :
"yogurt", "Qty" : 30 }
>
> db.geekFlareCollection.update({"product" : "bottles"},{$set :
{"Qty": 10}} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.geekFlareCollection.find()
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfb"), "product" :
"bottles", "Qty" : 10 }
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfc"), "product" : "bread",
"Qty" : 20 }
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfd"), "product" :
"yogurt", "Qty" : 30 }
>
> db.geekFlareCollection.find()
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfb"), "product" :
"bottles", "Qty" : 10 }
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfc"), "product" : "bread",
"Qty" : 20 }
{ "_id" : ObjectId("5edf3f67d6bfbd8125f58cfd"), "product" :
"yogurt", "Qty" : 30 }
Syntax: collectionName.deleteOne({DeletionCondition})
Example:
Syntax: collectionName.deleteMany({DeletionCondition})
Example:
Syntax: collectionName.remove({DeletionCondition},1)
Example:
Syntax: collectionName.distinct(field)
Example:
> db.geekFlareCollection.distinct("product")
[ "Cheese", "Snacks2", "Snacks3", "bread", "ketchup" ]
Copy
To get distinct records from one field while specifying the query.
Syntax: collectionName.distinct(field,query)
Example:
> db.geekFlareCollection.distinct('product',{"Qty":20})
[ "Snacks3", "bread" ]
Copy
Syntax: collectionName.renameCollection(newCollectionName)
Example:
>db.geekFlareCollection.renameCollection('geekFlareCol')
{ "ok" : 1 }
> show collections
geekFlareCol