Practical # 2
Practical # 2
The db Command
To check your currently selected database, use the command dB
o db
In MongoDB default database is test. If you didn't create any database, then collections will
be stored in test database.
Drop Database
MongoDB db.dropDatabase() command is used to drop a existing database.
o db.dropDatabase()
Create Collection
MongoDB db.createCollection(name, options) is used to create collection.
o db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and is
used to specify configuration of collection.
Capped Collections
Capped collections are collections which can store data in the same order it is inserted.
It is very fixed size, high-performance and "auto-FIFO age-Out". That is, when the allotted
space is fully utilized, newly added objects (documents) will replace the older ones in the
same order it is inserted.
Since data is stored in the natural order, that is the order it is inserted, while retrieving data,
no ordering is required, unless you want to reverse the order.
New objects can be inserted into a capped collection.
Existing objects can be updated.
But you can't remove an individual object from the capped collection. Using drop command,
you have to remove all the documents. After the drop, you have to recreate the capped
collection.
Presently, the maximum size for a capped collection is 1e9(i.e. 1X109) for 32-bit machines.
For 64 bit machines, there is no theoretical limit. Practically, it can be extended till your
system resources permit.
Capped collections can be used for logging, caching and auto archiving.
Drop Collection
MongoDB's db.collection_name.drop() is used to drop a collection from the database.
Drop() method will return true, if the selected collection is dropped successfully, otherwise it
will return false.
Collection Functions
db.collection.aggregate():Provides access to the aggregation pipeline.
db.collection.bulkWrite():Provides bulk write operation functionality.
db.collection.count(): Wraps count to return a count of the number of documents in a
collection or a view.
db.collection.countDocuments():Wraps the $group aggregation stage with a $sum expression
to return a count of the number of documents in a collection or a view.
db.collection.estimatedDocumentCount() Wraps count to return an approximate count
of the documents in a collection or a view.
db.collection.createIndex(): Builds an index on a collection.
db.collection.createIndexes(): Builds one or more indexes on a collection.
db.collection.dataSize():Returns the size of the collection. Wraps the size field in the output
of the collStats.
db.collection.deleteOne():Deletes a single document in a collection.
db.collection.insert(): Creates a new document in a collection.
db.collection.insertOne(): Inserts a new document in a collection.
db.collection.totalSize() Reports the total size of a collection, including the size of all
documents and all indexes on a collection.
db.collection.update() Modifies a document in a collection.
db.collection.updateOne()Modifies a single document in a collection.
db.collection.updateMany() Modifies multiple documents in a collection.
db.collection.watch() Establishes a Change Stream on a collection.
db.collection.validate() Performs diagnostic operations on a collection.
Data in Collection
Aggregation
Aggregation operations group values from multiple documents together, and can perform a
variety of operations on the grouped data to return a single result.
o db.collection.aggregate(pipeline, options)
Collection’s countDocument