Experiment 16: MongoDB CRUD Operations Experiment 17: B MongoDB Queries

Write MongoDB queries to perform CRUD operations on document using insert(), find(), update(), remove()


Write MongoDB queries to work with records using find(), limit(), sort(), createIndex(), aggregate() 



use AIDS_B 

command creates AIDS_B database. In AIDS_B database to create collection names "register" 

db.register.insertOne({Name:'abc',RegdNo:10001,Year:4,Semester:1,branch:'AIDS'})  

it is implicit way


test> use AIDS_B

switched to db AIDS_B

AIDS_B> db.register.insertOne({Name:'abc',RegdNo:10001,Year:4,Semester:1,branch:'AIDS'})

{

  acknowledged: true,

  insertedId: ObjectId('690588f3b25c5cb31d63b112')

}

AIDS_B> db.register.insertOne({Name:'def',RegdNo:10002,Year:4,Semester:1,branch:'AIDS'})

{

  acknowledged: true,

  insertedId: ObjectId('6905891eb25c5cb31d63b113')

}

AIDS_B> db.register.insertOne({Name:'ghi',RegdNo:10003,Year:4,Semester:1,branch:'AIDS'})

{

  acknowledged: true,

  insertedId: ObjectId('69058941b25c5cb31d63b114')

}

AIDS_B> db.register.insertOne({Name:'jkl',RegdNo:10004,Year:4,Semester:1,branch:'IT'})

{

  acknowledged: true,

  insertedId: ObjectId('6905895ab25c5cb31d63b115')

}

AIDS_B> db.register.insertOne({Name:'mno',RegdNo:10005,Year:4,Semester:1,branch:'IT'})

{

  acknowledged: true,

  insertedId: ObjectId('69058969b25c5cb31d63b116')

}

AIDS_B> db.register.insertOne({Name:'pqr',RegdNo:10006,Year:4,Semester:1,branch:'IT'})

{

  acknowledged: true,

  insertedId: ObjectId('6905897fb25c5cb31d63b117')

}

AIDS_B> db.register.find()

[

  {

    _id: ObjectId('690588f3b25c5cb31d63b112'),

    Name: 'abc',

    RegdNo: 10001,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('6905891eb25c5cb31d63b113'),

    Name: 'def',

    RegdNo: 10002,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('69058941b25c5cb31d63b114'),

    Name: 'ghi',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('6905895ab25c5cb31d63b115'),

    Name: 'jkl',

    RegdNo: 10004,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  },

  {

    _id: ObjectId('69058969b25c5cb31d63b116'),

    Name: 'mno',

    RegdNo: 10005,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  },

  {

    _id: ObjectId('6905897fb25c5cb31d63b117'),

    Name: 'pqr',

    RegdNo: 10006,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  }

]

AIDS_B> db.register.update({Name:'pqr'},{$set:{Semester:2}})

DeprecationWarning: Collection.update() is deprecated. Use updateOne, updateMany, or bulkWrite.

{

  acknowledged: true,

  insertedId: null,

  matchedCount: 1,

  modifiedCount: 1,

  upsertedCount: 0

}

AIDS_B> db.register.find({Name:'pqr'})

[

  {

    _id: ObjectId('6905897fb25c5cb31d63b117'),

    Name: 'pqr',

    RegdNo: 10006,

    Year: 4,

    Semester: 2,

    branch: 'IT'

  }

]

AIDS_B> db.register.delete({Name:'abc'})

TypeError: db.register.delete is not a function

AIDS_B> db.register.deleteOne({Name:'abc'})

{ acknowledged: true, deletedCount: 1 }

AIDS_B> db.register.find({Name:'abc'})

AIDS_B> db.register.find().count()

5

AIDS_B> db.register.find().limit(3)

[

  {

    _id: ObjectId('6905891eb25c5cb31d63b113'),

    Name: 'def',

    RegdNo: 10002,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('69058941b25c5cb31d63b114'),

    Name: 'ghi',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('6905895ab25c5cb31d63b115'),

    Name: 'jkl',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  }

]

AIDS_B> db.register.find().limit(2)

[

  {

    _id: ObjectId('6905891eb25c5cb31d63b113'),

    Name: 'def',

    RegdNo: 10002,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('69058941b25c5cb31d63b114'),

    Name: 'ghi',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  }

]

AIDS_B> db.register.find().sort()

[

  {

    _id: ObjectId('6905891eb25c5cb31d63b113'),

    Name: 'def',

    RegdNo: 10002,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('69058941b25c5cb31d63b114'),

    Name: 'ghi',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('6905895ab25c5cb31d63b115'),

    Name: 'jkl',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  },

  {

    _id: ObjectId('69058969b25c5cb31d63b116'),

    Name: 'mno',

    RegdNo: 10004,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  },

  {

    _id: ObjectId('6905897fb25c5cb31d63b117'),

    Name: 'pqr',

    RegdNo: 10006,

    Year: 4,

    Semester: 2,

    branch: 'IT'

  }

]

AIDS_B> db.register.find().sort({branch:1})

[

  {

    _id: ObjectId('6905891eb25c5cb31d63b113'),

    Name: 'def',

    RegdNo: 10002,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('69058941b25c5cb31d63b114'),

    Name: 'ghi',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('6905895ab25c5cb31d63b115'),

    Name: 'jkl',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  },

  {

    _id: ObjectId('69058969b25c5cb31d63b116'),

    Name: 'mno',

    RegdNo: 10004,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  },

  {

    _id: ObjectId('6905897fb25c5cb31d63b117'),

    Name: 'pqr',

    RegdNo: 10006,

    Year: 4,

    Semester: 2,

    branch: 'IT'

  }

]

AIDS_B> db.register.find().sort({branch:-1})

[

  {

    _id: ObjectId('6905895ab25c5cb31d63b115'),

    Name: 'jkl',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  },

  {

    _id: ObjectId('69058969b25c5cb31d63b116'),

    Name: 'mno',

    RegdNo: 10004,

    Year: 4,

    Semester: 1,

    branch: 'IT'

  },

  {

    _id: ObjectId('6905897fb25c5cb31d63b117'),

    Name: 'pqr',

    RegdNo: 10006,

    Year: 4,

    Semester: 2,

    branch: 'IT'

  },

  {

    _id: ObjectId('6905891eb25c5cb31d63b113'),

    Name: 'def',

    RegdNo: 10002,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  },

  {

    _id: ObjectId('69058941b25c5cb31d63b114'),

    Name: 'ghi',

    RegdNo: 10003,

    Year: 4,

    Semester: 1,

    branch: 'AIDS'

  }

]

AIDS_B>

AIDS_B>


sort 1 ascending

sort -1 descending


**********************************************************

  ******************************************************

      ***********************************************


Aggregations:


THEORY:

Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods.

Syntax 

db.collection.aggregate(pipeline, options)

Parameters

Parameter Details

1.      pipeline array(A sequence of data aggregation operations or stages)

2.      options document(optional, available only if pipeline present as an array)

  

PROGRAM:

 

Insert following documents in db.register collection


{Name:'abc',RegdNo:10001,Year:4,Semester:1,branch:'AIDS'}

|

|

3 documents

{Name:'jkl',RegdNo:20001,Year:4,Semester:1,branch:'IT'}

|

|

4 doucments


Count

How do you get the number of AIDS and IT students? One way to do it is by using count() 

function as below.

>db.register.find({branch:'AIDS'}).count()

O/p: 3

>db.register.find({branch:'IT'}).count()

O/p: 4

But what if you do not know the possible values of "branch" upfront. Here Aggregation framework comes to play.

>db.register.aggregate( [ { $group: { _id:'$branch', count: { $sum: 1 } } }])

O/p: :[ { _id: 'AIDS', count: 3 }, { _id: 'IT', count: 4 } ]


Index Management in MongoDB


THEORY:

Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every document of a collection to select those documents that match the query statement. This scan is highly inefficient and require MongoDB to process a large volume of data.

Indexes are special data structures, that store a small portion of the data set in an easy-to-traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in the index.

 

PROCEDURE:

 

The createIndex() Method

To create an index, you need to use createIndex() method of MongoDB.

Syntax

The basic syntax of createIndex() method is as follows

>db.COLLECTION_NAME.createIndex({name:1})

Here key is the name of the field on which you want to create index and 1 is for ascending order. To create index in descending order you need to use -1.

 

In createIndex() method you can pass multiple fields, to create index on multiple fields.

 

The dropIndex() method

You can drop a particular index using the dropIndex() method of MongoDB.

Syntax

The basic syntax of DropIndex() method is as follows().

>db.COLLECTION_NAME.dropIndex({name:1})

 

PROGRAM:

 

Example of CreateIndex


AIDS>db.register.createIndex({RegdNo:1})


AIDS>db.register.createIndex({Name:-1})


use >db.col.getIndexes() to list out all Indexes in the collection

 

Example of dropIndex

 

>db.register.dropIndex({"Name":1})



 







Comments

Popular posts from this blog

Experiment 1: Node.js

Experiment 10: ReactJS – Render HTML, JSX, Components – function & Class

Experiment 6: ExpressJS – Routing, HTTP Methods, Middleware