
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

984 Views
For this, use aggregate(). Let us create a collection with documents −>db.demo138.insertOne({"Id":101, "PlayerDetails":[{"PlayerName":"Chris", "PlayerScore":400}, {"PlayerName":"David", "PlayerScore":1000}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31bb9ffdf09dd6d08539a1") } >db.demo138.insertOne({"Id":102, "PlayerDetails":[{"PlayerName":"Bob", "PlayerScore":500}, {"PlayerName":"Carol", "PlayerScore":600}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31bbcefdf09dd6d08539a2") }Display all documents from a collection with the help of find() method −> db.demo138.find();This will produce the following output −{ "_id" : ObjectId("5e31bb9ffdf09dd6d08539a1"), "Id" : 101, "PlayerDetails" : [ { "PlayerName" : "Chris", "PlayerScore" : 400 }, { "PlayerName" : "David", "PlayerScore" : 1000 } ] } { "_id" : ObjectId("5e31bbcefdf09dd6d08539a2"), ... Read More

1K+ Views
For this, use aggregate() along with $filter. Let us create a collection with documents −> db.demo137.insertOne( ... { ... Name1:"Chris", ... Name2:"David", ... Detail1:[ ... {_id:"John", Name3:"Chris"}, ... {_id:"Chris", Name3:"Chris"}, ... ], ... Detail2:[{_id:"David", Name3:"Chris"}, ... {_id:"Carol", Name3:"Chris"}, ... {_id:"John", Name3:"Chris"}] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e31b4cefdf09dd6d08539a0") }Display all documents from a collection with the help of find() method −> db.demo137.find();This will ... Read More

203 Views
Let us create a collection with documents −> db.demo136.insertOne( ... { ... ... "Name":"Chris", ... "Details":[ ... { ... "Id":"101", ... "EmployeeName":"Mike", ... "EmployeeDetails":[ ... { ... "EmpId":1001, ... "Salary":1000 ... }, ... { ... ... Read More

262 Views
To update, use $set along with UPDATE. Let us create a collection with documents −>db.demo135.insertOne({"Details":[{"EmployeeId":101, "EmployeeName":"Chris", "EmployeeSalary":45000}, {"EmployeeId":102, "EmployeeName":"Chris", "EmployeeSalary":45000}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31a5ddfdf09dd6d085399c") }Display all documents from a collection with the help of find() method −> db.demo135.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e31a5ddfdf09dd6d085399c"), "Details" : [ { "EmployeeId" : 101, "EmployeeName" : "Chris", "EmployeeSalary" : 45000 }, { "EmployeeId" : 102, ... Read More

2K+ Views
To update, use update() along with $set. Let us create a collection with documents −>db.demo134.insertOne({"EmployeeId":101, "EmployeeDetails":[{"EmployeeName":"Chris", "EmployeeAge":27}, {"EmployeeName":"Bob", "EmployeeAge":28}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e319b2f68e7f832db1a7f7c") } >db.demo134.insertOne({"EmployeeId":102, "EmployeeDetails":[{"EmployeeName":"David", "EmployeeAge":24}, {"EmployeeName":"Carol", "EmployeeAge":29}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e319b4468e7f832db1a7f7d") }Display all documents from a collection with the help of find() method −> db.demo134.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e319b2f68e7f832db1a7f7c"), "EmployeeId" : 101, "EmployeeDetails" : [ { "EmployeeName" : "Chris", "EmployeeAge" : 27 }, ... Read More

2K+ Views
To group, use $week and $month in MongoDB. Let us create a collection with documents −> db.demo133.insertOne({"Rank":18, "DueDate":new ISODate("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e31980968e7f832db1a7f78") } > db.demo133.insertOne({"Rank":12, "DueDate":new ISODate("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e31982568e7f832db1a7f79") } > db.demo133.insertOne({"Rank":12, "DueDate":new ISODate("2020-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5e31986568e7f832db1a7f7a") } > db.demo133.insertOne({"Rank":20, "DueDate":new ISODate("2020-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5e31986c68e7f832db1a7f7b") }Display all documents from a collection with the help of find() method −> db.demo133.find();This will produce the following output −{ "_id" : ObjectId("5e31980968e7f832db1a7f78"), "Rank" : 18, "DueDate" : ISODate("2020-01-10T00:00:00Z") } ... Read More

240 Views
To group across two columns, use $lookup. Let us create a collection with documents −> db.demo132.insertOne({"CountryName1":"US", "CountryName2":"UK", Value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5e31950468e7f832db1a7f75") } > db.demo132.insertOne({"CountryName1":"UK", "CountryName2":"AUS", Value:10}); { "acknowledged" : true, "insertedId" : ObjectId("5e31951d68e7f832db1a7f76") } > db.demo132.insertOne({"CountryName1":"AUS", "CountryName2":"US", Value:40}); { "acknowledged" : true, "insertedId" : ObjectId("5e31952c68e7f832db1a7f77") }Display all documents from a collection with the help of find() method −> db.demo132.find();This will produce the following output −{ "_id" : ObjectId("5e31950468e7f832db1a7f75"), "CountryName1" : "US", "CountryName2" : "UK", "Value" : 50 } { "_id" : ObjectId("5e31951d68e7f832db1a7f76"), "CountryName1" : "UK", "CountryName2" : "AUS", "Value" : ... Read More

1K+ Views
For this, use aggregate() in MongoDB. Let us first create a collection with documents −> db.demo131.insertOne( ... { ... "_id": 101, ... "Details": [ ... { ... "PlayerScore": 500, ... "PlayerName": "Chris" ... }, ... { ... "PlayerScore": 400, ... "PlayerName": "David" ... } ... ] ... } ... ... Read More

225 Views
Let us first create a collection with documents −> db.demo130.insertOne( ... { ... ... "PlayerDetails":[{Score:56}, {Score:78}, {Score:89}, {Score:97}] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e3065bf68e7f832db1a7f6d") } > db.demo130.insertOne( ... { ... ... "PlayerDetails":[{Score:45}, {Score:56}] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e3065c068e7f832db1a7f6e") }Display all documents from a collection with the help of find() method −> db.demo130.find();This will produce the following output −{ "_id" : ObjectId("5e3065bf68e7f832db1a7f6d"), "PlayerDetails" : [ { "Score" : 56 }, { "Score" : 78 }, { "Score" : 89 }, ... Read More

165 Views
To get distinct first words in a string, use split(). Let us first create a collection with documents −> db.demo129.insertOne({"Words":"This is the MySQL", "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e305d6368e7f832db1a7f6b") } > db.demo129.insertOne({"Words":"MongoDB is NOSQL database", "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e305d7b68e7f832db1a7f6c") }Display all documents from a collection with the help of find() method −> db.demo129.find();This will produce the following output −{ "_id" : ObjectId("5e305d6368e7f832db1a7f6b"), "Words" : "This is the MySQL", "CountryName" : "US" } { "_id" : ObjectId("5e305d7b68e7f832db1a7f6c"), "Words" : "MongoDB is NOSQL database", "CountryName" : "US" }Following is the query to ... Read More