Search This Blog

2023/04/15

MongoDb Queries

//to delete all document
db.marks.remove({})


db.marks.insertMany([
{
"firstName": "sangram",
"lastName": "desai",
"marks": 100,
"subject": "maths"
},
{
"firstName": "sagar",
"lastName": "desai",
"marks": 90,
"subject": "marathi"
},
{
"firstName": "sachine",
"lastName": "desai",
"marks": 90,
"subject": "biology"
}
])


db.marks.insertMany([
{
"firstName": "swapnil",
"lastName": "sardeshmukh",
"marks": 100,
"subject": "maths"
},
{
"firstName": "suyog",
"lastName": "sawant",
"marks": 90,
"subject": "marathi"
},
{
"firstName": "sanjay",
"lastName": "rane",
"marks": 90,
"subject": "biology"
}
])

db.marks.insertMany([
{
"firstName": "sathish",
"lastName": "sardeshmukh",
"marks": 100,
"subject": "maths"
},
{
"firstName": "sunil",
"lastName": "sawant",
"marks": 90,
"subject": "maths"
},
{
"firstName": "sujay",
"lastName": "rane",
"marks": 90,
"subject": "biology"
}
])


db.marks.insertMany([
{
"firstName": "sneha",
"lastName": "sardeshmukh",
"marks": 30,
"subject": "maths"
},
{
"firstName": "sanket",
"lastName": "sawant",
"marks": 20,
"subject": "maths"
},
{
"firstName": "shiva",
"lastName": "rane",
"marks": 30,
"subject": "biology"
}
])

db.marks.find({},
{subject: 1,firstName: 1
}).pretty())


db.marks.find(
{marks: {$gt: 80
}
},
{subject: 1,firstName: 1
}
).pretty()



db.marks.find(
{
$and: [
{marks: {$gt: 80
}
},
{marks: {$lt: 100
}
}
]
},
{subject: 1,firstName: 1,marks: 1
}
).pretty()

or

db.marks.find(
{
marks: {
$gt: 80,
$lt: 100
},
},
{subject: 1,firstName: 1,marks: 1
}
)

db.marks.aggregate(
{
"$group": {
_id: "$subject",
count: { $sum: 1
},
average_marks: { $avg: "$marks"
}
}
}
).pretty()


db.marks.find({firstName :/an/
})


db.marks.find({firstName :/^sanj/
})


db.marks.find({firstName :/jay$/
})

db.marks.update(
{ subject: "marathi"
},
{ $inc: { marks: -10
}
},
{ multi: true
}
)

db.marks.update(
{},
{ $set: { marks: 80
}
},
{ multi: true
}
)

db.marks.update(
{ marks: {$gte : 35
}
},
{ $set: { passed: true
}
},
{ multi: true
}
)

db.marks.updateMany(
{},
[
{ $set: { status: { $switch: {
branches: [
{ case: { $gte: [
"$marks",
35
]
}, then: "passed"
}
],
default: "fails"
}
}
}
}
]
)

db.marks.replaceOne({
firstName: "sagar",
lastName: "desai"
},
{
"firstName": "sagar",
"lastName": "desai",
"marks": 90,
"subject": "marathi",
"level": "primary"
})


updateMany() : It update all documents in a collection with matching filter.

updateOne() : It update only one top most document in a collection with matching filter.

update() : By default, the update() method updates a single document. Include the option {multi : true
} to update all documents that match the query criteria. Hence we can use it as both ways.

What are some utilities for backup and restore in MongoDB?
Answer:
mongoimport
mongoexport
mongodump
mongorestore

What are the data types in MongoDB?
Answer:
Null
{
"x": null
}
Boolean
{
"x": true
}
Number
{
"x": 4
}
String
{
"x": "foobar"
}
Date
{
"x": new Date()
}
Regular expression
{
"x": /foobar/i
}
Array
{
"x": [
"a",
"b",
"c"
]
}
Embedded document
{
"x": {
"foo": "bar"
}
}
Object ID
{
"x": ObjectId()
}
Binary Data
Binary data is a string of arbitrary bytes.
Code
{
"x": function() { /* ... */}
}

db.marks.find().limit(1).skip(1)

Sort on Marks Descendin
db.marks.find({},
{}).sort({
"marks": -1
})

Index:
db.mycol.createIndex({
"title": 1
})
1 is for ascending order. To create index in descending order you need to use -1.

or multiple fields
db.mycol.createIndex({
"title": 1,
"description": -1
})
dropping Index:
db.mycol.dropIndex({
"title": 1
})

Get Index:
db.mycol.getIndexes()


db.tutorials.insertMany([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by_user: 'tutorials point',
url: 'http: //www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'
],
likes: 100
},
{
title: 'NoSQL Overview',
description: 'No sql database is very fast',
by_user: 'tutorials point',
url: 'http: //www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'
],
likes: 10
},
{
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: 'Neo4j',
url: 'http: //www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'
],
likes: 750
}
])

db.tutorials.aggregate([
{
$group: {
_id: [
"$by_user",
"$title"
],
sum_total: { $sum: 1
}
}
}
])

db.marks.aggregate([
{
$project: {
"firstName": 1,
"lastName": 1,
"marks": 1,
}
}
])
db.marks.aggregate([
{
$project: {
"marks": 0,
}
}
])


db.books.insert({
"_id": 5,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa"
},
copies: 5,
lastModified: "2016-07-28"
})

db.books.insertMany([
{
"_id": 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa"
},
copies: 5,
lastModified: "2016-07-28"
},
{
"_id": 2,
title: "Baked Goods",
isbn: "9999999999999",
author: { last: "xyz", first: "abc", middle: ""
},
copies: 2,
lastModified: "2017-07-21"
},
{
"_id": 3,
title: "Ice Cream Cakes",
isbn: "8888888888888",
author: { last: "xyz", first: "abc", middle: "mmm"
},
copies: 5,
lastModified: "2017-07-22"
}
])

db.books.aggregate( [
{ $project : {
"author.first": 0,
"lastModified": 0
}
}
] )

db.books.aggregate( [
{
$project: {
title: 1,
"author.first": 1,
"author.last": 1,
"author.middle": {
$cond: {
if: { $eq: [
"",
"$author.middle"
]
},
then: "$$REMOVE",
else: "$author.middle"
}
}
}
}
] )

db.books.aggregate( [
{ $project: { myArray: [
"$isbn",
"$author.last"
]
}
}
] )

db.books.aggregate([
{
$project: {
first: {$substr: [
"$author.first",
0,
1
]
},
middle: {$substr: [
"$author.middle",
0,
1
]
},
last: "$author.last"
}
}
])

db.books.aggregate([
{
$project: {
authorFull: {$concat: [
"$author.first",
" ",
"$author.last"
]
}
}
}
])


//rename field
db.orders.update({},
{$rename: {
"OrderDate": "order_date"
}
},
false, true);

//create new collection from existing
db.orders.aggregate([
{$out: "order_copy"
}
])

//extrat date parts
db.books.aggregate([
{
$project: {
title: 1,
by_user: 1,
year : { $substr : [
"$lastModified",
0,
4
]
},
month : { $substr : [
"$lastModified",
5,
2
]
},
day : { $substr : [
"$lastModified",
8,
2
]
},
iso_date:new Date("$lastModified")
}
},
{$out: "books_date"
}
])

//date between two dates

db.books_date.find({
iso_date: {
$gte : ISODate("1960-01-01T00:00:00Z"),
$lt : ISODate("2000-01-01T00:00:00Z")
}
})

//
db.subject.insertMany([
{
"_id": ObjectId("5ba49f63a3b0f1fce83f3f3f"),
"name": "sangram",
"marks": 76.0,
"subject": "english"
},
{
"_id": ObjectId("5ba49f73a3b0f1fce83f3f40"),
"name": "sagar",
"marks": 77.0,
"subject": "marathi"
},
{
"_id": ObjectId("5ba49f7da3b0f1fce83f3f41"),
"name": "sachin",
"marks": 85.0,
"subject": "geography"
},
{
"_id": ObjectId("5ba49f87a3b0f1fce83f3f42"),
"name": "swapnil",
"marks": 80.0,
"subject": "geography"
},
{
"_id": ObjectId("5ba49f90a3b0f1fce83f3f43"),
"name": "saurabh",
"marks": 90.0,
"subject": "maths"
},
{
"_id": ObjectId("5ba49fa0a3b0f1fce83f3f44"),
"name": "shashank",
"marks": 79.0,
"subject": "maths"
},
{
"_id": ObjectId("5ba49fbba3b0f1fce83f3f45"),
"name": "sunil",
"marks": 84.0,
"subject": "geography"
},
{
"_id": ObjectId("5ba49fcaa3b0f1fce83f3f46"),
"name": "sujay",
"marks": 64.0,
"subject": "marathi"
},
{
"_id": ObjectId("5ba49fe4a3b0f1fce83f3f47"),
"name": "subodh",
"marks": 74.0,
"subject": "english"
},
{
"_id": ObjectId("5ba49ff6a3b0f1fce83f3f48"),
"name": "sunit",
"marks": 60.0,
"subject": "english"
},
{
"_id": ObjectId("5ba4a1fdffaf9599885ab184"),
"name": "vivek",
"marks": 85.0,
"subject": "marathi"
},
{
"_id": ObjectId("5ba4a205ffaf9599885ab185"),
"name": "vijay",
"marks": 85.0
},
{
"_id": ObjectId("5ba4a20affaf9599885ab186"),
"name": "vineet",
"marks": 85.0,
"subject": "maths"
}
])

//max marks by subject

db.subject.aggregate([
{
$group: {
_id: "$subject",
maxm: {$max: "$marks"
}
}
}
])

//max marks irrespective of subject
db.subject.aggregate([
{
$group: {
_id: null,
maxm: {$max: "$marks"
}
}
}
])

//group by having
db.subject.aggregate([
{
$group: {
_id: "$subject",
maxm: {$max: "$marks"
}
}
},
{ $match: { _id: {$eq: "maths"
}
}
}
])

//Getting max value and all records on which max value calculated
db.subject.aggregate([
{
$group: {
_id: "$subject",
max_marks: {$max: "$marks"
},
student: {$push: "$$ROOT"
}
}
},
{
$match: {
_id: {
$eq: "maths"
}
}
}
])
db.marks.remove()


db.marks.insertMany([
{
"firstName": "sangram",
"lastName": "desai",
"marks": 100,
"subject": "maths"
},
{
"firstName": "sagar",
"lastName": "desai",
"marks": 90,
"subject": "marathi"
},
{
"firstName": "sachine",
"lastName": "desai",
"marks": 90,
"subject": "biology"
}
])


db.marks.insertMany([
{
"firstName": "swapnil",
"lastName": "sardeshmukh",
"marks": 100,
"subject": "maths"
},
{
"firstName": "suyog",
"lastName": "sawant",
"marks": 90,
"subject": "marathi"
},
{
"firstName": "sanjay",
"lastName": "rane",
"marks": 90,
"subject": "biology"
}
])

db.marks.insertMany([
{
"firstName": "sathish",
"lastName": "sardeshmukh",
"marks": 100,
"subject": "maths"
},
{
"firstName": "sunil",
"lastName": "sawant",
"marks": 90,
"subject": "maths"
},
{
"firstName": "sujay",
"lastName": "rane",
"marks": 90,
"subject": "biology"
}
])


db.marks.insertMany([
{
"firstName": "sneha",
"lastName": "sardeshmukh",
"marks": 30,
"subject": "maths"
},
{
"firstName": "sanket",
"lastName": "sawant",
"marks": 20,
"subject": "maths"
},
{
"firstName": "shiva",
"lastName": "rane",
"marks": 30,
"subject": "biology"
}
])

db.marks.find({},
{subject: 1,firstName: 1
}).pretty())


db.marks.find(
{marks: {$gt: 80
}
},
{subject: 1,firstName: 1
}
).pretty()



db.marks.find(
{
$and: [
{marks: {$gt: 80
}
},
{marks: {$lt: 100
}
}
]
},
{subject: 1,firstName: 1,marks: 1
}
).pretty()

or
db.marks.find(
{
marks: {
$gt: 80,
$lt: 100
},
},
{subject: 1,firstName: 1,marks: 1
}
).pretty()

db.marks.aggregate(
{
"$group": {
_id: "$subject",
count: { $sum: 1
},
average_marks: { $avg: "$marks"
}
}
}
).pretty()


db.marks.find({firstName :/an/
})


db.marks.find({firstName :/^sanj/
})


db.marks.find({firstName :/jay$/
})

db.marks.update(
{ subject: "marathi"
},
{ $inc: { marks: -10
}
},
{ multi: true
}
)

db.marks.update(
{},
{ $set: { marks: 80
}
},
{ multi: true
}
)

db.marks.update(
{ marks: {$gte : 35
}
},
{ $set: { passed: true
}
},
{ multi: true
}
)

db.marks.updateMany(
{},
[
{ $set: { status: { $switch: {
branches: [
{ case: { $gte: [
"$marks",
35
]
}, then: "passed"
}
],
default: "fails"
}
}
}
}
]
)

db.marks.replaceOne({
firstName: "sagar",
lastName: "desai"
},
{
"firstName": "sagar",
"lastName": "desai",
"marks": 90,
"subject": "marathi",
"level": "primary"
})


updateMany() : It update all documents in a collection with matching filter.

updateOne() : It update only one top most document in a collection with matching filter.

update() : By default, the update() method updates a single document. Include the option {multi : true
} to update all documents that match the query criteria. Hence we can use it as both ways.

What are some utilities for backup and restore in MongoDB?
Answer:
mongoimport
mongoexport
mongodump
mongorestore

What are the data types in MongoDB?
Answer:
Null
{
"x": null
}
Boolean
{
"x": true
}
Number
{
"x": 4
}
String
{
"x": "foobar"
}
Date
{
"x": new Date()
}
Regular expression
{
"x": /foobar/i
}
Array
{
"x": [
"a",
"b",
"c"
]
}
Embedded document
{
"x": {
"foo": "bar"
}
}
Object ID
{
"x": ObjectId()
}
Binary Data
Binary data is a string of arbitrary bytes.
Code
{
"x": function() { /* ... */}
}

db.marks.find().limit(1).skip(1)

Sort on Marks Descendin
db.marks.find({},
{}).sort({
"marks": -1
})

Index:
db.mycol.createIndex({
"title": 1
})
1 is for ascending order. To create index in descending order you need to use -1.

or multiple fields
db.mycol.createIndex({
"title": 1,
"description": -1
})
dropping Index:
db.mycol.dropIndex({
"title": 1
})

Get Index:
db.mycol.getIndexes()


db.tutorials.insertMany([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by_user: 'tutorials point',
url: 'http: //www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'
],
likes: 100
},
{
title: 'NoSQL Overview',
description: 'No sql database is very fast',
by_user: 'tutorials point',
url: 'http: //www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'
],
likes: 10
},
{
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: 'Neo4j',
url: 'http: //www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'
],
likes: 750
}
])

db.tutorials.aggregate([
{
$group: {
_id: [
"$by_user",
"$title"
],
sum_total: { $sum: 1
}
}
}
])

db.marks.aggregate([
{
$project: {
"firstName": 1,
"lastName": 1,
"marks": 1,
}
}
])
db.marks.aggregate([
{
$project: {
"marks": 0,
}
}
])


db.books.insert({
"_id": 5,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa"
},
copies: 5,
lastModified: "2016-07-28"
})

db.books.insertMany([
{
"_id": 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa"
},
copies: 5,
lastModified: "2016-07-28"
},
{
"_id": 2,
title: "Baked Goods",
isbn: "9999999999999",
author: { last: "xyz", first: "abc", middle: ""
},
copies: 2,
lastModified: "2017-07-21"
},
{
"_id": 3,
title: "Ice Cream Cakes",
isbn: "8888888888888",
author: { last: "xyz", first: "abc", middle: "mmm"
},
copies: 5,
lastModified: "2017-07-22"
}
])

db.books.aggregate( [
{ $project : {
"author.first": 0,
"lastModified": 0
}
}
] )

db.books.aggregate( [
{
$project: {
title: 1,
"author.first": 1,
"author.last": 1,
"author.middle": {
$cond: {
if: { $eq: [
"",
"$author.middle"
]
},
then: "$$REMOVE",
else: "$author.middle"
}
}
}
}
] )

db.books.aggregate( [
{ $project: { myArray: [
"$isbn",
"$author.last"
]
}
}
] )

db.books.aggregate([
{
$project: {
first: {$substr: [
"$author.first",
0,
1
]
},
middle: {$substr: [
"$author.middle",
0,
1
]
},
last: "$author.last"
}
}
])

db.books.aggregate([
{
$project: {
authorFull: {$concat: [
"$author.first",
" ",
"$author.last"
]
}
}
}
])


//rename field
db.orders.update({},
{$rename: {
"OrderDate": "order_date"
}
},
false, true);

//create new collection from existing
db.orders.aggregate([
{$out: "order_copy"
}
])

//extrat date parts
db.books.aggregate([
{
$project: {
title: 1,
by_user: 1,
year : { $substr : [
"$lastModified",
0,
4
]
},
month : { $substr : [
"$lastModified",
5,
2
]
},
day : { $substr : [
"$lastModified",
8,
2
]
},
iso_date:new Date("$lastModified")
}
},
{$out: "books_date"
}
])

//date between two dates

db.books_date.find({
iso_date: {
$gte : ISODate("1960-01-01T00:00:00Z"),
$lt : ISODate("2000-01-01T00:00:00Z")
}
})

//
db.subject.insertMany([
{
"_id": ObjectId("5ba49f63a3b0f1fce83f3f3f"),
"name": "sangram",
"marks": 76.0,
"subject": "english"
},
{
"_id": ObjectId("5ba49f73a3b0f1fce83f3f40"),
"name": "sagar",
"marks": 77.0,
"subject": "marathi"
},
{
"_id": ObjectId("5ba49f7da3b0f1fce83f3f41"),
"name": "sachin",
"marks": 85.0,
"subject": "geography"
},
{
"_id": ObjectId("5ba49f87a3b0f1fce83f3f42"),
"name": "swapnil",
"marks": 80.0,
"subject": "geography"
},
{
"_id": ObjectId("5ba49f90a3b0f1fce83f3f43"),
"name": "saurabh",
"marks": 90.0,
"subject": "maths"
},
{
"_id": ObjectId("5ba49fa0a3b0f1fce83f3f44"),
"name": "shashank",
"marks": 79.0,
"subject": "maths"
},
{
"_id": ObjectId("5ba49fbba3b0f1fce83f3f45"),
"name": "sunil",
"marks": 84.0,
"subject": "geography"
},
{
"_id": ObjectId("5ba49fcaa3b0f1fce83f3f46"),
"name": "sujay",
"marks": 64.0,
"subject": "marathi"
},
{
"_id": ObjectId("5ba49fe4a3b0f1fce83f3f47"),
"name": "subodh",
"marks": 74.0,
"subject": "english"
},
{
"_id": ObjectId("5ba49ff6a3b0f1fce83f3f48"),
"name": "sunit",
"marks": 60.0,
"subject": "english"
},
{
"_id": ObjectId("5ba4a1fdffaf9599885ab184"),
"name": "vivek",
"marks": 85.0,
"subject": "marathi"
},
{
"_id": ObjectId("5ba4a205ffaf9599885ab185"),
"name": "vijay",
"marks": 85.0
},
{
"_id": ObjectId("5ba4a20affaf9599885ab186"),
"name": "vineet",
"marks": 85.0,
"subject": "maths"
}
])

//max marks by subject

db.subject.aggregate([
{
$group: {
_id: "$subject",
maxm: {$max: "$marks"
}
}
}
])

//max marks irrespective of subject
db.subject.aggregate([
{
$group: {
_id: null,
maxm: {$max: "$marks"
}
}
}
])

//group by having
db.subject.aggregate([
{
$group: {
_id: "$subject",
maxm: {$max: "$marks"
}
}
},
{ $match: { _id: {$eq: "maths"
}
}
}
])

//Getting max value and all records on which max value calculated
db.subject.aggregate([
{
$group: {
_id: "$subject",
max_marks: {$max: "$marks"
},
student: {$push: "$$ROOT"
}
}
},
{
$match: {
_id: {
$eq: "maths"
}
}
}
])

or

db.subject.aggregate([
{
$group: {
_id: "$subject",
max_marks: {$max: "$marks"},
student: {$push: "$$ROOT"}
}
}
]).pretty()
or

db.subject.aggregate([
{
$group: {
_id: "$subject",
max_marks: {$max: "$marks"},
student: {$push: "$$ROOT"}
}
}
]).pretty()

db.subject.aggregate([
{
$group: {
_id: "$subject",
avg: {$avg: "$marks"
},
students:{
$push:"$$ROOT"
}
}
}
]).pretty()

//using javascript function get odd marks
db.subject.find({
$where:function(){
return this.marks %2
}
})

No comments:

Post a Comment