0% found this document useful (0 votes)
18 views7 pages

Practical # 2

Uploaded by

Alishba Aleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Practical # 2

Uploaded by

Alishba Aleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Department of Software Engineering

Mehran University of Engineering and Technology, Jamshoro

Course: SWE – Data Analytics and Business Intelligence


Instructor Ms Sana Faiz Practical/Lab No. 02
Date CLOs 03
Signature Assessment Score

Topic To understand basics of python


Objectives To become familiar with DDL commands in Mongo DB

Lab Discussion: Theoretical concepts and Procedural steps

Data definition Language


 Before you can write data to collection, you must provide a definition of the collection you
want to use. This definition includes information such as the collection's name, the name of
its various documents and the data type contained in those documents. To perform these
definitions, NoSQL Database provides a Data Definition Language (DDL) that you use to
form databases and collection creation statements. These statements can be used to:
 Define collections
 Modify collection definitions.
 Delete collection definitions.

The use Command


 MongoDB use DATABASE_NAME is used to create database. The command will create a
new database if it doesn't exist, otherwise it will return the existing database.
o use DATABASE_NAME

The db Command
 To check your currently selected database, use the command dB
o db

The show dbs command


 If you want to check your databases list, use the command show dbs.
o show dbs
To Show Database

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.

Parameter Type Description

Name String Name of the collection to be


created

Options Document (Optional) Specify options about


memory size and indexing
Collection
 Options parameter is optional, so you need to specify only the name of the collection.
Following is the list of options you can use

Field Type Description

(Optional) If true, enables a capped collection. Capped collection


is a fixed size collection that automatically overwrites its oldest
capped Boolean entries when it reaches its maximum size. If you specify true, you
need to specify size parameter also.

(Optional) If true, automatically create index on _id fields. Default


autoIndexI
Boolean value is false.
d

(Optional) Specifies a maximum size in bytes for a capped


collection. If capped is true, then you need to specify this field
size number also.

(Optional) Specifies the maximum number of documents allowed


in the capped collection.
max number

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.deleteMany() Deletes multiple documents in a collection.


 db.collection.distinct() Returns an array of documents that have distinct values for the
specified field.
 db.collection.drop() Removes the specified collection from the database.
 db.collection.dropIndex() Removes a specified index on a collection.
 db.collection.dropIndexes() Removes all indexes on a collection.
 db.collection.findOneAndDelete() Finds a single document and deletes it.
 db.collection.findOneAndReplace() Finds a single document and replaces it.

 db.collection.ensureIndex() Deprecated. Use db.collection.createIndex().


 db.collection.explain() Returns information on the query execution of various methods.
 db.collection.find() Performs a query on a collection or a view and returns a cursor
object.
 db.collection.findAndModify() Atomically modifies and returns a single document.
 db.collection.findOne() Performs a query and returns a single document.
 Collection Functions
 db.collection.findOneAndUpdate(): Finds a single document and updates it.
 db.collection.getIndexes(): Returns an array of documents that describe the existing indexes
on a collection.
 db.collection.getShardDistribution(): For collections in sharded clusters,
db.collection.getShardDistribution(): reports data of chunk distribution.
 db.collection.getShardVersion():Internal diagnostic method for sharded cluster.
 Collection Functions
 db.collection.insertMany() Inserts several new document in a collection.
 db.collection.isCapped() Reports if a collection is a capped collection.
 db.collection.latencyStats() Returns latency statistics for a collection.
 db.collection.mapReduce() Performs map-reduce style data aggregation.
 db.collection.reIndex() Rebuilds all existing indexes on a collection.
 db.collection.remove() Deletes documents from a collection.
 db.collection.totalIndexSize() Reports the total size used by the indexes on a collection.
Provides a wrapper around the totalIndexSize field of the collStats output.

 db.collection.renameCollection() Changes the name of a collection.


 db.collection.replaceOne() Replaces a single document in a collection.
 db.collection.save() Provides a wrapper around an insert() and update() to insert new
documents.
 db.collection.stats() Reports on the state of a collection. Provides a wrapper around the
collStats.
 db.collection.storageSize() Reports the total size used by the collection in bytes.
Provides a wrapper around the storageSize field of the collStats output.

 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 count() function

Collection’s countDocument

Collection’s estimated document Count function

Collections Datasize Function:


Class Tasks
Submission Date: --

1. Perform Each and every task provided in the lab handout.


2. Create Database and create collection of the data of your own choice and attach
screenshot of every output.
3. Use at least 5 collection functions of your own choice and attach screenshot of every
output.
4. Use any five Aggregation Pipeline Operators on the data of your own choice and attach
screenshot of every output.
5. Use any five aggregation pipeline stages on the data of your own choice and attach
screenshot of every output.

You might also like