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
Big Data Analytics Articles - Page 147 of 167
1K+ Views
Yes, it is possible using the $project operator. Let us first create a collection with documents> db.sumTwoFieldsDemo.insertOne({"FirstValue":150, "SecondValue":350}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4bfe15e86fd1496b38cd") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":450, "SecondValue":1550}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4c1215e86fd1496b38ce") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":2560, "SecondValue":2440}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4c2715e86fd1496b38cf") }Following is the query to display all documents from a collection with the help of find() method> db.sumTwoFieldsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9b4bfe15e86fd1496b38cd"), "FirstValue" : 150, "SecondValue" : 350 } { "_id" : ObjectId("5c9b4c1215e86fd1496b38ce"), "FirstValue" : 450, "SecondValue" ... Read More
1K+ Views
Following is the query to get distinct values with sorted data in MongoDBdb.yourCollectionName.distinct("yourFieldName").sort();Let us first create a collection with documents>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10, "StudentName":"John", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e3315e86fd1496b38c3") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20, "StudentName":"Carol", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e3e15e86fd1496b38c4") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10, "StudentName":"John", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e4415e86fd1496b38c5") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":30, "StudentName":"Chris", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e5115e86fd1496b38c6") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20, "StudentName":"Carol", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e5715e86fd1496b38c7") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40, "StudentName":"Bob", "StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e6515e86fd1496b38c8") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40, "StudentName":"Bob", "Stude ... Read More
728 Views
There is a $toLower operator in MongoDB to be used as part of aggregate framework. But, we can also use the for loop to iterate over the specific field and update one by one.Let us first create a collection with documents> db.toLowerDemo.insertOne({"StudentId":101, "StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b4515e86fd1496b38bf") } > db.toLowerDemo.insertOne({"StudentId":102, "StudentName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b4b15e86fd1496b38c0") } > db.toLowerDemo.insertOne({"StudentId":103, "StudentName":"CHris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b5115e86fd1496b38c1") } > db.toLowerDemo.insertOne({"StudentId":104, "StudentName":"ROBERT"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b5a15e86fd1496b38c2") }Following is the query to display all documents from ... Read More
157 Views
You can use $push operator for this. Let us first create a collection with documents>db.twoSeparateArraysDemo.insertOne({"StudentName":"Larry", "StudentFirstGameScore":[98], "StudentSecondGameScore":[77]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b152815e86fd1496b38b8") } >db.twoSeparateArraysDemo.insertOne({"StudentName":"Mike", "StudentFirstGameScore":[58], "StudentSecondGameScore":[78]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b152d15e86fd1496b38b9") } >db.twoSeparateArraysDemo.insertOne({"StudentName":"David", "StudentFirstGameScore":[65], "StudentSecondGameScore":[67]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b153315e86fd1496b38ba") }Following is the query to display all documents from a collection with the help of find() method> db.twoSeparateArraysDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9b152815e86fd1496b38b8"), "StudentName" : "Larry", "StudentFirstGameScore" : [ 98 ], "StudentSecondGameScore" : [ ... Read More
147 Views
For conditional upserts or updates, you can use $max operator. Let us first create a collection with documents>db.conditionalUpdatesDemo.insertOne({"_id":100, "StudentFirstScore":89, "StudentSecondScore":78, "BiggestScore":89}); { "acknowledged" : true, "insertedId" : 100 } >db.conditionalUpdatesDemo.insertOne({"_id":101, "StudentFirstScore":305, "StudentSecondScore":560, "BiggestScore":1050}); { "acknowledged" : true, "insertedId" : 101 } >db.conditionalUpdatesDemo.insertOne({"_id":103, "StudentFirstScore":560, "StudentSecondScore":789, "BiggestScore":880}); { "acknowledged" : true, "insertedId" : 103 } Following is the query to display all documents from a collection with the help of find() method: > db.conditionalUpdatesDemo.find().pretty();This will produce the following output{ "_id" : 100, "StudentFirstScore" : 89, "StudentSecondScore" : 78, "BiggestScore" : 89 } { "_id" : 101, ... Read More
836 Views
Yes, you can use pull and add at the same time with $addToSet and $pull operator. Let us first create a collection with documents> db.pullAndAddToSetDemo.insertOne({StudentScores : [78, 89, 90]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9a797e15e86fd1496b38af") }Following is the query to display all documents from a collection with the help of find() method> db.pullAndAddToSetDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9a797e15e86fd1496b38af"), "StudentScores" : [ 78, 89, 90 ] }Following is the query to pull and addtoset at the same time in MongoDB> var ... Read More
1K+ Views
To find oldest/youngest post in MongoDB collection, you can use sort(). Let’s say you have a document with a field “UserPostDate” and you need to get the oldest and youngest post. For that, Let us first create a collection with documents>db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Larry@123", "UserName":"Larry", "UserPostDate":new ISODate('2019-03-27 12:00:00')}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a700f15e86fd1496b38ab") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Sam@897", "UserName":"Sam", "UserPostDate":new ISODate('2012-06-17 11:40:30')}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a703815e86fd1496b38ac") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"David@777", "UserName":"David", "UserPostDate":new ISODate('2018-01-31 10:45:35')}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a705e15e86fd1496b38ad") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Chris@909", "UserName":"Chris", "UserPostDate":new ISODate('2017-04-14 04:12:04')}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a708915e86fd1496b38ae") }Following ... Read More
182 Views
To increment a value inside a nested array, use positional operator ($). Let us first create a collection with documents> db.incrementInNestedArrayDemo.insertOne( ... { ... "StudentId":100, ... "ProjectDetails": ... [ ... {"ProjectId":567778888, ... "TeamSize":4 ... }, ... { ... "ProjectId":67888999, ... "TeamSize":2 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9a6b7b15e86fd1496b38aa") }Following is the query to display all documents from a collection with the help ... Read More
775 Views
You can use upsert i.e. whenever you insert a value and it already exist then update would be performed. If the value does not already exist then it would get inserted.Let us first create a collection with documents> db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Larry", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a633815e86fd1496b38a4") } > db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Mike", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a634a15e86fd1496b38a5") } > db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Sam", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a635015e86fd1496b38a6") }Following is the query to display all documents from a collection with the help of find() method> db.onlyInsertIfValueIsUniqueDemo.find().pretty();This will produce the following output{ "_id" ... Read More
566 Views
Following is the syntax to count the number of documents in a MongoDB collectionlet anyVariableName= db.getCollection(‘yourCollectionName’); yourVariableName.count();Let us first create a collection with documents> db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a5e2015e86fd1496b38a1") } >db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Ramit", "CustomerAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a5e3515e86fd1496b38a2") } >db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Adam", "CustomerAge":27, "CustomerCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a5e4c15e86fd1496b38a3") }Following is the query to display all documents from a collection with the help of find() method> db.countNumberOfDocumentsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9a5e2015e86fd1496b38a1"), "CustomerName" : "Bob" } { "_id" : ObjectId("5c9a5e3515e86fd1496b38a2"), "CustomerName" : "Ramit", "CustomerAge" ... Read More