0% found this document useful (0 votes)
14 views18 pages

Dbms

Uploaded by

17SYCMIDhumal Om
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)
14 views18 pages

Dbms

Uploaded by

17SYCMIDhumal Om
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/ 18

test> show dbs

admin 40.00 KiB

config 12.00 KiB

local 72.00 KiB

test> use db_!

switched to db db_!

db_!> drop db_!

Uncaught:

SyntaxError: Missing semicolon. (1:4)

> 1 | drop db_!

| ^

2|

db_!> use db_1

switched to db db_1

db_1> db.createCollection("Posts")

{ ok: 1 }

db_1> db.Posts.insertOne({"Name":"Om Dhumal","Role":"Developer","Age":21,"Salary":50000})

acknowledged: true,

insertedId: ObjectId('6720d3873d84db951486b01d')

db_1> db.Posts.find()

_id: ObjectId('6720d3873d84db951486b01d'),

Name: 'Om Dhumal',

Role: 'Developer',

Age: 21,

Salary: 50000
}

db_1> db.Posts.find({"Age":20})

db_1> db.Posts.find({"Age":21})

_id: ObjectId('6720d3873d84db951486b01d'),

Name: 'Om Dhumal',

Role: 'Developer',

Age: 21,

Salary: 50000

db_1> db.Posts.insertMany([{"Name":"Ankush
Khairnar","Role":"Developer","Age":21,"Salary":50000},{"Name":"Rohan Nagare","Role":"Front-End
Devloper","Age":21,"Salary":60000},{"Name":"Vighnesh ise","Role":"Back-End
Devloper","Age":21,"Salary":50000}])

acknowledged: true,

insertedIds: {

'0': ObjectId('6720d5213d84db951486b01e'),

'1': ObjectId('6720d5213d84db951486b01f'),

'2': ObjectId('6720d5213d84db951486b020')

db_1> db.Posts.find()

_id: ObjectId('6720d3873d84db951486b01d'),

Name: 'Om Dhumal',

Role: 'Developer',
Age: 21,

Salary: 50000

},

_id: ObjectId('6720d5213d84db951486b01e'),

Name: 'Ankush Khairnar',

Role: 'Developer',

Age: 21,

Salary: 50000

},

_id: ObjectId('6720d5213d84db951486b01f'),

Name: 'Rohan Nagare',

Role: 'Front-End Devloper',

Age: 21,

Salary: 60000

},

_id: ObjectId('6720d5213d84db951486b020'),

Name: 'Vighnesh ise',

Role: 'Back-End Devloper',

Age: 21,

Salary: 50000

db_1> db.Posts.find({"Salary":50000})

_id: ObjectId('6720d3873d84db951486b01d'),

Name: 'Om Dhumal',

Role: 'Developer',
Age: 21,

Salary: 50000

},

_id: ObjectId('6720d5213d84db951486b01e'),

Name: 'Ankush Khairnar',

Role: 'Developer',

Age: 21,

Salary: 50000

},

_id: ObjectId('6720d5213d84db951486b020'),

Name: 'Vighnesh ise',

Role: 'Back-End Devloper',

Age: 21,

Salary: 50000

db_1> db.Posts.find({"Salary":50000}).sort({"Age":1})

_id: ObjectId('6720d3873d84db951486b01d'),

Name: 'Om Dhumal',

Role: 'Developer',

Age: 21,

Salary: 50000

},

_id: ObjectId('6720d5213d84db951486b01e'),

Name: 'Ankush Khairnar',

Role: 'Developer',
Age: 21,

Salary: 50000

},

_id: ObjectId('6720d5213d84db951486b020'),

Name: 'Vighnesh ise',

Role: 'Back-End Devloper',

Age: 21,

Salary: 50000

db_1> db.Posts.find({"Salary":50000}).sort({"Age":1}).skip(1).limit(3)

_id: ObjectId('6720d5213d84db951486b01e'),

Name: 'Ankush Khairnar',

Role: 'Developer',

Age: 21,

Salary: 50000

},

_id: ObjectId('6720d5213d84db951486b020'),

Name: 'Vighnesh ise',

Role: 'Back-End Devloper',

Age: 21,

Salary: 50000

db_1> db.createCollection('students')

{ ok: 1 }

db_1> db.students.insertMany([
... { "_id": 1, "name": "Alice", "marks": 85 },

... { "_id": 2, "name": "Bob", "marks": 70 },

... { "_id": 3, "name": "Charlie", "marks": 90 },

... { "_id": 4, "name": "Alice", "marks": 95 },

... { "_id": 5, "name": "Bob", "marks": 80 }

... ]);

acknowledged: true,

insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }

db_1> db.students.find()

{ _id: 1, name: 'Alice', marks: 85 },

{ _id: 2, name: 'Bob', marks: 70 },

{ _id: 3, name: 'Charlie', marks: 90 },

{ _id: 4, name: 'Alice', marks: 95 },

{ _id: 5, name: 'Bob', marks: 80 }

db_1> var mapFunction=function() {emit this.name,this.marks};

Uncaught:

SyntaxError: Missing semicolon. (1:32)

> 1 | var mapFunction=function() {emit this.name,this.marks};

| ^

2|

db_1> var mapFunction=function() emit (this.name,this.marks);

Uncaught:

SyntaxError: Unexpected token, expected "{" (1:27)

> 1 | var mapFunction=function() emit (this.name,this.marks);


| ^

2|

db_1> var mapFunction=function(){ emit (this.name,this.marks)};

db_1> var reduceFunction=function(key,values){

... var total=0;

... var count=values.length();

... for( var i=0;i<count;i++){

... total+=values[i]

... }

... return {total:total,count:count};

... };

db_1> var finalizeFunction = function(key, reducedValue) {

... return reducedValue.total / reducedValue.count; // Calculate the average

... };

db_1> db.students.mapReduce(

... mapFunction,

... reduceFunction,

... {

... out: "average_marks", // Output collection for results

... finalize: finalizeFunction // Finalize function to calculate averages

... }

... );

DeprecationWarning: Collection.mapReduce() is deprecated. Use an aggregation instead.

See https://docs.mongodb.com/manual/core/map-reduce for details.

Uncaught:

MongoServerError[JSInterpreterFailure]: MapReduce internal error :: caused by :: TypeError:


values.length is not a function :
@:3:18

db_1> db.average_marks.find().pretty();

db_1> db.students.aggregate([

... {

... $group: {

... _id: "$name", // Group by student name

... averageMarks: { $avg: "$marks" } // Calculate average marks

... }

... }

... ]);

{ _id: 'Alice', averageMarks: 90 },

{ _id: 'Charlie', averageMarks: 90 },

{ _id: 'Bob', averageMarks: 75 }

db_1> db.students.aggregate([ { $group: { _id: "$_id", /* Group by student name*/ averageMarks:


{ $avg: "$marks" } /* Calculate average marks*/ } }] );

{ _id: 2, averageMarks: 70 },

{ _id: 3, averageMarks: 90 },

{ _id: 1, averageMarks: 85 },

{ _id: 4, averageMarks: 95 },

{ _id: 5, averageMarks: 80 }

db_1> db.students.aggregate([ { $group: { _id: "$_name", /* Group by student name*/


averageMarks: { $avg: "$marks" } /* Calculate average marks*/ } }] );

[ { _id: null, averageMarks: 84 } ]

db_1> db.students.aggregate([ { $group: { _id: "$name", /* Group by student name*/ averageMarks:


{ $avg: "$marks" } /* Calculate average marks*/ } }] );

{ _id: 'Alice', averageMarks: 90 },


{ _id: 'Bob', averageMarks: 75 },

{ _id: 'Charlie', averageMarks: 90 }

db_1> db.createCollection('products')

{ ok: 1 }

db_1> db.orders.insertMany([

... { _id: 1, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-01"), price: 25, items: [ { sku:
"oranges", qty: 5, price: 2.5 }, { sku: "apples", qty:

5, price: 2.5 } ], status: "A" },

... { _id: 2, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-08"), price: 70, items: [ { sku:
"oranges", qty: 8, price: 2.5 }, { sku: "chocolates", qty: 5, price: 10 } ], status: "A" },

... { _id: 3, cust_id: "Busby Bee", ord_date: new Date("2020-03-08"), price: 50, items: [ { sku:
"oranges", qty: 10, price: 2.5 }, { sku: "pears", qty: 10, price: 2.5 } ], status: "A" },

... { _id: 4, cust_id: "Busby Bee", ord_date: new Date("2020-03-18"), price: 25, items: [ { sku:
"oranges", qty: 10, price: 2.5 } ], status: "A" },

... { _id: 5, cust_id: "Busby Bee", ord_date: new Date("2020-03-19"), price: 50, items: [ { sku:
"chocolates", qty: 5, price: 10 } ], status: "A"},

... { _id: 6, cust_id: "Cam Elot", ord_date: new Date("2020-03-19"), price: 35, items: [ { sku:
"carrots", qty: 10, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "A" },

... { _id: 7, cust_id: "Cam Elot", ord_date: new Date("2020-03-20"), price: 25, items: [ { sku:
"oranges", qty: 10, price: 2.5 } ], status: "A" },

... { _id: 8, cust_id: "Don Quis", ord_date: new Date("2020-03-20"), price: 75, items: [ { sku:
"chocolates", qty: 5, price: 10 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "A" },

... { _id: 9, cust_id: "Don Quis", ord_date: new Date("2020-03-20"), price: 55, items: [ { sku:
"carrots", qty: 5, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 }, { sku: "oranges", qty: 10, price:
2.5 } ], status: "A" },

... { _id: 10, cust_id: "Don Quis", ord_date: new Date("2020-03-23"), price: 25, items: [ { sku:
"oranges", qty: 10, price: 2.5 } ], status: "A" }

... ])

acknowledged: true,

insertedIds: {

'0': 1,

'1': 2,

'2': 3,
'3': 4,

'4': 5,

'5': 6,

'6': 7,

'7': 8,

'8': 9,

'9': 10

db_1>

db_1> db.products.find()

db_1> db.orders.find()

_id: 1,

cust_id: 'Ant O. Knee',

ord_date: ISODate('2020-03-01T00:00:00.000Z'),

price: 25,

items: [

{ sku: 'oranges', qty: 5, price: 2.5 },

{ sku: 'apples', qty: 5, price: 2.5 }

],

status: 'A'

},

_id: 2,

cust_id: 'Ant O. Knee',

ord_date: ISODate('2020-03-08T00:00:00.000Z'),

price: 70,
items: [

{ sku: 'oranges', qty: 8, price: 2.5 },

{ sku: 'chocolates', qty: 5, price: 10 }

],

status: 'A'

},

_id: 3,

cust_id: 'Busby Bee',

ord_date: ISODate('2020-03-08T00:00:00.000Z'),

price: 50,

items: [

{ sku: 'oranges', qty: 10, price: 2.5 },

{ sku: 'pears', qty: 10, price: 2.5 }

],

status: 'A'

},

_id: 4,

cust_id: 'Busby Bee',

ord_date: ISODate('2020-03-18T00:00:00.000Z'),

price: 25,

items: [ { sku: 'oranges', qty: 10, price: 2.5 } ],

status: 'A'

},

_id: 5,

cust_id: 'Busby Bee',

ord_date: ISODate('2020-03-19T00:00:00.000Z'),

price: 50,

items: [ { sku: 'chocolates', qty: 5, price: 10 } ],


status: 'A'

},

_id: 6,

cust_id: 'Cam Elot',

ord_date: ISODate('2020-03-19T00:00:00.000Z'),

price: 35,

items: [

{ sku: 'carrots', qty: 10, price: 1 },

{ sku: 'apples', qty: 10, price: 2.5 }

],

status: 'A'

},

_id: 7,

cust_id: 'Cam Elot',

ord_date: ISODate('2020-03-20T00:00:00.000Z'),

price: 25,

items: [ { sku: 'oranges', qty: 10, price: 2.5 } ],

status: 'A'

},

_id: 8,

cust_id: 'Don Quis',

ord_date: ISODate('2020-03-20T00:00:00.000Z'),

price: 75,

items: [

{ sku: 'chocolates', qty: 5, price: 10 },

{ sku: 'apples', qty: 10, price: 2.5 }

],

status: 'A'
},

_id: 9,

cust_id: 'Don Quis',

ord_date: ISODate('2020-03-20T00:00:00.000Z'),

price: 55,

items: [

{ sku: 'carrots', qty: 5, price: 1 },

{ sku: 'apples', qty: 10, price: 2.5 },

{ sku: 'oranges', qty: 10, price: 2.5 }

],

status: 'A'

},

_id: 10,

cust_id: 'Don Quis',

ord_date: ISODate('2020-03-23T00:00:00.000Z'),

price: 25,

items: [ { sku: 'oranges', qty: 10, price: 2.5 } ],

status: 'A'

db_1> var mapFunction=function(){ emit(this.cust_id,this.price)};

db_1> var reduceFunction1 = function(keyCustId, valuesPrices) {

... return Array.sum(valuesPrices);

... };

db_1> db.orders.mapReduce(

... mapFunction1,

... reduceFunction1,
... { out: "map_reduce_example" }

... )

ReferenceError: mapFunction1 is not defined

db_1> db.orders.mapReduce( mapFunction, reduceFunction1, { out: "map_reduce_example" } )

{ result: 'map_reduce_example', ok: 1 }

db_1> db.map_reduce_example.find().sort( { _id: 1 } )

{ _id: 'Ant O. Knee', value: 95 },

{ _id: 'Busby Bee', value: 125 },

{ _id: 'Cam Elot', value: 60 },

{ _id: 'Don Quis', value: 155 }

db_1> db.createCollection("Student")

{ ok: 1 }

db_1> db.students.insertMany([

... { "_id": 1, "name": "Alice", "marks": 85 },

... { "_id": 2, "name": "Bob", "marks": 70 },

... { "_id": 3, "name": "Charlie", "marks": 90 },

... { "_id": 4, "name": "Alice", "marks": 95 },

... { "_id": 5, "name": "Bob", "marks": 80 }

...

db_1> db.createCollection("student")

{ ok: 1 }

db_1> db.createCollection("students")

{ ok: 1 }

db_1> db.students.insertMany([

... { "_id": 1, "name": "Alice", "marks": 85 },

... { "_id": 2, "name": "Bob", "marks": 70 },

... { "_id": 3, "name": "Charlie", "marks": 90 },

... { "_id": 4, "name": "Alice", "marks": 95 },

... { "_id": 5, "name": "Bob", "marks": 80 }


... ]);

Uncaught:

MongoBulkWriteError: E11000 duplicate key error collection: db_1.students index: _id_ dup key:
{ _id: 1 }

Result: BulkWriteResult {

insertedCount: 0,

matchedCount: 0,

modifiedCount: 0,

deletedCount: 0,

upsertedCount: 0,

upsertedIds: {},

insertedIds: {}

Write Errors: [

WriteError {

err: {

index: 0,

code: 11000,

errmsg: 'E11000 duplicate key error collection: db_1.students index: _id_ dup key: { _id: 1 }',

errInfo: undefined,

op: { _id: 1, name: 'Alice', marks: 85 }

db_1> db.students.drop();

true

db_1> db.createCollection("students")

{ ok: 1 }

db_1> db.students.insertMany([ { "_id": 1, "name": "Alice", "marks": 85 }, { "_id": 2, "name": "Bob",


"marks": 70 }, { "_id": 3, "name": "Charlie", "marks":

90 }, { "_id": 4, "name": "Alice", "marks": 95 }, { "_id": 5, "name": "Bob", "marks": 80 }] );

{
acknowledged: true,

insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }

db_1> db.students.find()

{ _id: 1, name: 'Alice', marks: 85 },

{ _id: 2, name: 'Bob', marks: 70 },

{ _id: 3, name: 'Charlie', marks: 90 },

{ _id: 4, name: 'Alice', marks: 95 },

{ _id: 5, name: 'Bob', marks: 80 }

db_1> db.students.aggregate([

... {$group

...

db_1> db.students.aggregate([{

... $group: { _id:"$name", average:{$avg:"makrks"}}

... $group: { _id:"$name", average:{$avg:"makrks

db_1> db.students.aggregate([{

... $group: { _id:"$name", average:{$avg:"marks"}

... }

... }

... )];

Uncaught:

SyntaxError: Unexpected token, expected "," (5:0)

3|}

4|}

> 5 | )];

|^

6|
db_1> db.students.aggregate([

... {

... $group: {

... _id: "$name",

... average: { $avg: "$marks" }

... }

... }

... ]);

{ _id: 'Bob', average: 75 },

{ _id: 'Alice', average: 90 },

{ _id: 'Charlie', average: 90 }

db_1> db.students.aggregate([

... {

... $group:

... {

... id:"$name",average:{$avg:"$marks"}

... }

... }

... ]);

MongoServerError[Location40234]: The field 'id' must be an accumulator object

db_1> db.students.aggregate([ { $group: { _id: "$name", average: { $avg: "$marks" } } }] );

{ _id: 'Bob', average: 75 },

{ _id: 'Alice', average: 90 },

{ _id: 'Charlie', average: 90 }

db_1> db.students.createIndex({ name: 1 });

name_1

db_1> db.students.find({ name: "Alice" });


[

{ _id: 1, name: 'Alice', marks: 85 },

{ _id: 4, name: 'Alice', marks: 95 }

db_1> db.students.getIndexes();

{ v: 2, key: { _id: 1 }, name: '_id_' },

{ v: 2, key: { name: 1 }, name: 'name_1' }

db_1> db.students.createIndex({ name: 1, marks: -1 });

name_1_marks_-1

db_1> db.students.getIndexes();

{ v: 2, key: { _id: 1 }, name: '_id_' },

{ v: 2, key: { name: 1 }, name: 'name_1' },

{ v: 2, key: { name: 1, marks: -1 }, name: 'name_1_marks_-1' }

db_1>

You might also like