- 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 1659 Articles for Big Data Analytics
572 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
84 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
98 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
647 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
905 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
115 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
630 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
453 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
1K+ Views
Following is the syntax to return only a single property _id in MongoDBdb.yourCollectionName.find({}, {"_id": 1}).pretty();Let us first create a collection with documents> db.singlePropertyIdDemo.insertOne({"_id":101, "UserName":"Larry", "UserAge":21}); { "acknowledged" : true, "insertedId" : 101 } > db.singlePropertyIdDemo.insertOne({"_id":102, "UserName":"Mike", "UserAge":26}); { "acknowledged" : true, "insertedId" : 102 } > db.singlePropertyIdDemo.insertOne({"_id":103, "UserName":"Chris", "UserAge":24}); { "acknowledged" : true, "insertedId" : 103 } > db.singlePropertyIdDemo.insertOne({"_id":104, "UserName":"Robert", "UserAge":23}); { "acknowledged" : true, "insertedId" : 104 } > db.singlePropertyIdDemo.insertOne({"_id":105, "UserName":"John", "UserAge":27}); { "acknowledged" : true, "insertedId" : 105 }Following is the query to display all documents from a collection with the help of find() method> db.singlePropertyIdDemo.find().pretty();This will produce ... Read More
2K+ Views
Yes, it is possible to rename using aggregation. Let us first create a collection with documents> db.renameIdDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a1760353decbc2fc927c5") } > db.renameIdDemo.insertOne({"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a1765353decbc2fc927c6") } > db.renameIdDemo.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a176b353decbc2fc927c7") }Following is the query to display all documents from a collection with the help of find() method> db.renameIdDemo.find();This will produce the following output{ "_id" : ObjectId("5c9a1760353decbc2fc927c5"), "StudentName" : "Chris" } { "_id" : ObjectId("5c9a1765353decbc2fc927c6"), "StudentName" : "Robert" } { "_id" : ObjectId("5c9a176b353decbc2fc927c7"), "StudentName" : "David" }Following is the query to ... Read More