- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 1349 Articles for MongoDB
1K+ Views
To add MD5 hash value, use hex_md5(). Let us first create a collection with documents −>db.addMd5HashValueDemo.insertOne({"UserName":"Adam", "UserPassword":"Adam123456"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6a4c66d78f205348bc619") } >db.addMd5HashValueDemo.insertOne({"UserName":"Chris", "UserPassword":"Chris_121#"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6a4e46d78f205348bc61a") }Following is the query to display all documents from a collection with the help of find() method −> db.addMd5HashValueDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd6a4c66d78f205348bc619"), "UserName" : "Adam", "UserPassword" : "Adam123456" } { "_id" : ObjectId("5cd6a4e46d78f205348bc61a"), "UserName" : "Chris", "UserPassword" : "Chris_121#" }Following is the query to add md5 hash value to mongo collection −> db.addMd5HashValueDemo.find().forEach( function(documentPass){ documentPass.Value ... Read More
1K+ Views
To get a list of values, use $group aggregation along with $push operator. Let us first create a collection with documents −> db.groupByDemo.insertOne({"UserName":"John", "Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f0457806ebf1256f136") } > db.groupByDemo.insertOne({"UserName":"Larry", "Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f0657806ebf1256f137") } > db.groupByDemo.insertOne({"UserName":"John", "Subject":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f0d57806ebf1256f138") } > db.groupByDemo.insertOne({"UserName":"John", "Subject":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f1357806ebf1256f139") } > db.groupByDemo.insertOne({"UserName":"Larry", "Subject":"SQL Server"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f1c57806ebf1256f13a") }Following is the query to display all documents from a collection with the help ... Read More
75 Views
For this, use the $all operator. Let us first create a collection with documents −>db.findDocumentExactlySameInArrayDemo.insertOne({"TechnicalSubjects":["C++", "Java", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69a5f57806ebf1256f12e") } >db.findDocumentExactlySameInArrayDemo.insertOne({"TechnicalSubjects":["MySQL", "Java", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69ac057806ebf1256f12f") } >db.findDocumentExactlySameInArrayDemo.insertOne({"TechnicalSubjects":["C#", "Python", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69ad457806ebf1256f130") } >db.findDocumentExactlySameInArrayDemo.insertOne({"TechnicalSubjects":["MySQL", "C", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69adf57806ebf1256f131") }Following is the query to display all documents from a collection with the help of find() method −> db.findDocumentExactlySameInArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd69a5f57806ebf1256f12e"), "TechnicalSubjects" : [ ... Read More
1K+ Views
Use $max and $min operator along with aggregate framework to get the maximum and minimum value. Let us first create a collection with documents −> db.maxAndMinDemo.insertOne({"Value":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698a357806ebf1256f129") } > db.maxAndMinDemo.insertOne({"Value":97}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698af57806ebf1256f12a") } > db.maxAndMinDemo.insertOne({"Value":69}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b357806ebf1256f12b") } > db.maxAndMinDemo.insertOne({"Value":96}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b657806ebf1256f12c") } > db.maxAndMinDemo.insertOne({"Value":99}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b957806ebf1256f12d") }Following is the query to display all documents from a collection with the help of find() method ... Read More
207 Views
You can use $addToSet operator for this. Let us first create a collection with documents −> db.insertDataIntoArrayDemo.insertOne( { "UserDetails":[ { "UserId" :"user121", "userGroupMessage":[] }, { "UserId" :"user221", "userGroupMessage":["Cool", "Good Morning"] } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd694e157806ebf1256f128") }Following is the query to display all documents from ... Read More
67 Views
To get MySQL LIKE statement, use the REGEX in MongoDB. Let us first create a collection with documents −> db.likeInMongoDBDemo.insertOne({"Name" : "Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6922857806ebf1256f123") } > db.likeInMongoDBDemo.insertOne({"Name" : "John" }); { "acknowledged" : true, "insertedId" : ObjectId("5cd6923157806ebf1256f124") } > db.likeInMongoDBDemo.insertOne({"Name" : "Scott"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6924557806ebf1256f125") } > db.likeInMongoDBDemo.insertOne({"Name" : "Sean"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6924f57806ebf1256f126") } > db.likeInMongoDBDemo.insertOne({"Name" : "Samuel"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6925857806ebf1256f127") }Following is the query to display all documents from a collection with ... Read More
579 Views
To merge, use $setUnion operator. Let us first create a collection with documents −> db.mergeTwoArrayFieldDemo.insertOne({"NaturalNumbers":[1,2,3,8,10,20,30],"WholeNumbers":[0,1,2,63,78,20,45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68e4057806ebf1256f11d") }Following is the query to display all documents from a collection with the help of find() method −> db.mergeTwoArrayFieldDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd68e4057806ebf1256f11d"), "NaturalNumbers" : [ 1, 2, 3, 8, 10, 20, 30 ], "WholeNumbers" : [ 0, 1, 2, 63, 78, 20, 45 ] }Following is the query to merge two array field in MongoDB.> db.mergeTwoArrayFieldDemo.aggregate([ { "$project": { "MergedArray": { "$setUnion": [ "$NaturalNumbers", "$WholeNumbers" ] } }} ]);This will produce the following output −{ "_id" : ObjectId("5cd68e4057806ebf1256f11d"), "MergedArray" : [ 0, 1, 2, 3, 8, 10, 20, 30, 45, 63, 78 ] }
81 Views
Let us first create a collection with documents −> db.findByObjectIdDemo.insertOne({"ClientName":"Larry", "ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cd657806ebf1256f11a") } > db.findByObjectIdDemo.insertOne({"ClientName":"Chris", "ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cdc57806ebf1256f11b") } > db.findByObjectIdDemo.insertOne({"ClientName":"David", "ClientAge":38, "isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cf657806ebf1256f11c") }Following is the query to display all documents from a collection with the help of find() method −> db.findByObjectIdDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd68cd657806ebf1256f11a"), "ClientName" : "Larry", "ClientAge" : 23 } { "_id" : ObjectId("5cd68cdc57806ebf1256f11b"), "ClientName" : "Chris", "ClientAge" : 26 } { ... Read More
254 Views
Let’s say you have saved the Login date of users. Now, you want the users who logged in between specific dates i.e. login date. For this, use $gte and $lt operator along with count(). Let us first create a collection with documents −> db.findDataByDateDemo.insertOne({"UserName":"John", "UserLoginDate":new ISODate("2019-01-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8cd7bf3115999ed511ed") } > db.findDataByDateDemo.insertOne({"UserName":"Larry", "UserLoginDate":new ISODate("2019-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8ce7bf3115999ed511ee") } > db.findDataByDateDemo.insertOne({"UserName":"Sam", "UserLoginDate":new ISODate("2019-05-02")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8cf3bf3115999ed511ef") } > db.findDataByDateDemo.insertOne({"UserName":"David", "UserLoginDate":new ISODate("2019-05-16")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8d00bf3115999ed511f0") } > db.findDataByDateDemo.insertOne({"UserName":"Carol", ... Read More
273 Views
You can use $group operator with _id: null. Following is the syntax −db.yourCollectionName.aggregate([{$group: {_id:null, "anyFieldName": {$avg:"$yourFieldName"} } }]);Let us first create a collection with documents −> db.caculateTheAverageValueDemo.insertOne({"Population":100}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a197924bb85b3f4895f") } > db.caculateTheAverageValueDemo.insertOne({"Population":500}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a1c7924bb85b3f48960") } > db.caculateTheAverageValueDemo.insertOne({"Population":200}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a237924bb85b3f48961") } > db.caculateTheAverageValueDemo.insertOne({"Population":100}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a297924bb85b3f48962") } > db.caculateTheAverageValueDemo.insertOne({"Population":100}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a2e7924bb85b3f48963") }Following is the query to display all documents from a collection with the help of find() ... Read More