
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6705 Articles for Database

502 Views
To calculate average in MongoDB, use $avg. Let us create a collection with documents −> db.demo80.insertOne({"Details":{"Price":10.5}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43271bf0181ecc42297") } > db.demo80.insertOne({"Details":{"Price":50.3}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43871bf0181ecc42298") } > db.demo80.insertOne({"Details":{"Price":100.10}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43f71bf0181ecc42299") }Display all documents from a collection with the help of find() method −> db.demo80.find();This will produce the following output −{ "_id" : ObjectId("5e2bf43271bf0181ecc42297"), "Details" : { "Price" : 10.5 } } { "_id" : ObjectId("5e2bf43871bf0181ecc42298"), "Details" : { "Price" : 50.3 } } { "_id" : ObjectId("5e2bf43f71bf0181ecc42299"), "Details" : { "Price" ... Read More

111 Views
Let us create a collection with documents −> db.demo78.insertOne({"Name1":"Chris", "Name2":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd99c71bf0181ecc4228f") } > db.demo78.insertOne({"Name1":"Bob", "Name2":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd9ac71bf0181ecc42290") } > db.demo78.insertOne({"Name1":"David", "Name2":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd9b671bf0181ecc42291") } > db.demo78.insertOne({"Name1":"Jace", "Name2":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd9bf71bf0181ecc42292") }Display all documents from a collection with the help of find() method −> db.demo78.find();This will produce the following output −{ "_id" : ObjectId("5e2bd99c71bf0181ecc4228f"), "Name1" : "Chris", "Name2" : "Mike" } { "_id" : ObjectId("5e2bd9ac71bf0181ecc42290"), "Name1" : "Bob", "Name2" : "Carol" } { ... Read More

334 Views
To update MongoDB object, use UPDATE(). Let us create a collection with documents −> db.demo77.insertOne({"Details" : { "Score" : 78 } }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd6f371bf0181ecc4228a") }Display all documents from a collection with the help of find() method −> db.demo77.find();This will produce the following output −{ "_id" : ObjectId("5e2bd6f371bf0181ecc4228a"), "Details" : { "Score" : 78 } }Following is the query to update MongoDB object −> db.demo77.update({'Details.Score':78}, {$set:{'Details.Score':89}}, {multi:true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method −> db.demo77.find();This will produce the ... Read More

419 Views
To implement multilevel $group, use MongoDB aggregate. Let us create a collection with documents −> db.demo76.insertOne({ Name:"Chris", "Age" : 21, "CountryName" : 'US' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42281") } > db.demo76.insertOne({ Name:"Chris", "Age" : 21, "CountryName" : 'US' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42282") } > db.demo76.insertOne({ Name:"Chris", "Age" : 23, "CountryName" : 'UK' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42283") } > db.demo76.insertOne({ Name:"Chris", "Age" : 23, "CountryName" : 'UK' }); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd3e571bf0181ecc42284") } > db.demo76.insertOne({ Name:"Chris", "Age" : 21, ... Read More

227 Views
To return only a specific number of elements, use aggregate() and $slice. Let us create a collection with documents −> db.demo75.insertOne({"Name":["Sam", "Mike", "Carol", "David", "Bob", "John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bcd7671bf0181ecc42278") }Display all documents from a collection with the help of find() method −> db.demo75.find();This will produce the following output −{ "_id" : ObjectId("5e2bcd7671bf0181ecc42278"), "Name" : [ "Sam", "Mike", "Carol", "David", "Bob", "John" ] }Following is the slice query in MongoDB −> db.demo75.aggregate([ { $project: { Name: { $slice: [ "$Name", 4 ] } } } ]);This will produce the following output −{ "_id" : ObjectId("5e2bcd7671bf0181ecc42278"), ... Read More

537 Views
Let us create a collection with documents −> db.demo74.insertOne( ... { ... StudentName: "Chris", ... StudentDetails: [{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... }, { ... "Subject": "MongoDB", ... "isRegular": "InActive" ... }, { ... "Subject": "MongoDB", ... "isRegular": "InActive" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e29c6b671bf0181ecc4226f") } > db.demo74.insertOne({ ... name: "document2", ... data: [{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... }, { ... "Subject": "MongoDB", ... Read More

1K+ Views
To get the first and last document in MongoDB, use aggregate() along with $first and $last respectively. Let us create a collection with documents −> db.demo73.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29c41b71bf0181ecc4226c") } . > db.demo73.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29c41e71bf0181ecc4226d") } > db.demo73.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29c42271bf0181ecc4226e") }Display all documents from a collection with the help of find() method −> db.demo73.find();This will produce the following output −{ "_id" : ObjectId("5e29c41b71bf0181ecc4226c"), "Name" : "Chris" } { "_id" : ObjectId("5e29c41e71bf0181ecc4226d"), "Name" : "Bob" } { "_id" : ObjectId("5e29c42271bf0181ecc4226e"), "Name" ... Read More

670 Views
For this, use $let along with $expr. Here, $let is used to define temporary variable. The $expr is used for aggregation expressions. Let us create a collection with documents −> db.demo72.insertOne( ... { ... StudentDetails:[ ... { ... Name: "Chris", ... Age: 21 ... }, ... { ... Name: "David", ... Age: 23 ... }, ... { ... Name: "Bob", ... ... Read More

1K+ Views
To calculate sum of string in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo71.insertOne({"Price":"20"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29af210912fae76b13d76e") } > db.demo71.insertOne({"Price":"50"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29af240912fae76b13d76f") } > db.demo71.insertOne({"Price":"20"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29af270912fae76b13d770") } > db.demo71.insertOne({"Price":"10"}); { "acknowledged" : true, "insertedId" : ObjectId("5e29af2d0912fae76b13d771") }Display all documents from a collection with the help of find() method −> db.demo71.find();This will produce the following output −{ "_id" : ObjectId("5e29af210912fae76b13d76e"), "Price" : "20" } { "_id" : ObjectId("5e29af240912fae76b13d76f"), "Price" : "50" } { "_id" ... Read More

119 Views
Concatenate deep sub-lists using aggregate() along with $unwind. Let us create a collection with documents −> db.demo70.insertOne( ... { ... ... "first" : [ ... { ... "details" : { ... "second" : [ ... { ... "StudentDetails" : { ... "Score" : 10 ... ... Read More