
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 1349 Articles for MongoDB

325 Views
Use aggregate framework along with $ifNull operator for this. The $concatArrays in aggregation is used to concatenate arrays. Let us first create a collection with documents −>db.concatenateArraysDemo.insertOne({"FirstSemesterSubjects": ["MongoDB", "MySQL", "Java"], "SecondSemesterSubjects":["C", "C++", ]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd687707924bb85b3f4895c") } > db.concatenateArraysDemo.insertOne({"FirstSemesterSubjects":["C#", "Ruby", "Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd687927924bb85b3f4895d") } >db.concatenateArraysDemo.insertOne({"FirstSemesterSubjects":["HTML", "CSS", "Javascript"], "SecondSemesterSubjects":["CSS", "Javascript"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd687bb7924bb85b3f4895e") }Following is the query to display all documents from a collection with the help of find() method −> db.concatenateArraysDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd687707924bb85b3f4895c"), ... Read More

2K+ Views
You can use $in operator to check if a value is in an array or not. Let us first create a collection with documents −> db.valueInArrayDemo.insertOne({"UserName":"John", "UserMessage":["Hi", "Hello", "Bye"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd684cf7924bb85b3f48959") } > db.valueInArrayDemo.insertOne({"UserName":"Larry", "UserMessage":["Thank You", "Amazing", "Nice"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd684d27924bb85b3f4895a") } >db.valueInArrayDemo.insertOne({"UserName":"Carol", "UserMessage":["Awesome", "Bye", "Cool"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd684d87924bb85b3f4895b") }Following is the query to display all documents from a collection with the help of find() method −> db.valueInArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd684cf7924bb85b3f48959"), "UserName" : "John", "UserMessage" : [ "Hi", "Hello", ... Read More

351 Views
To push elements to an existing array, use $addToSet operator along with update(). Let us first create a collection with documents −> db.pushElements.insertOne({"Comments":["Good", "Awesome", "Nice"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd682597924bb85b3f48953") }Following is the query to display all documents from a collection with the help of find() method −> db.pushElements.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd682597924bb85b3f48953"), "Comments" : [ "Good", "Awesome", "Nice" ] }Following is the query to push elements to an existing array in MongoDB −> db.pushElements.update( {_id:ObjectId("5cd682597924bb85b3f48953")}, { ... Read More

2K+ Views
For this, work with the concept of forEach(). Let us first create a collection with documents −> db.printDocuementValueDemo.insertOne({"InstructorName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6804f7924bb85b3f48950") } > db.printDocuementValueDemo.insertOne({"InstructorName":"Sam Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd680577924bb85b3f48951") } > db.printDocuementValueDemo.insertOne({"InstructorName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd680637924bb85b3f48952") }Following is the query to display all documents from a collection with the help of find() method −> db.printDocuementValueDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd6804f7924bb85b3f48950"), "InstructorName" : "John Smith" } { "_id" : ObjectId("5cd680577924bb85b3f48951"), "InstructorName" : "Sam Williams" } ... Read More

634 Views
For this, you need to write some code using forEach(). Let us first create a collection with documents −> db.removingWhiteSpaceDemo.insertOne({"Title":" Introduction to java "}); { "acknowledged" : true, "insertedId" : ObjectId("5cd66f387924bb85b3f4894c") }Following is the query to display all documents from a collection with the help of find() method −> db.removingWhiteSpaceDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd66f387924bb85b3f4894c"), "Title" : " Introduction to java " }Following is the query to remove white spaces (leading and trailing) from string value −> db.removingWhiteSpaceDemo.find({}, {"Title": 1 }).forEach(function(myDocument) { myDocument.Title = myDocument.Title.trim(); db.removingWhiteSpaceDemo.update( { "_id": myDocument._id ... Read More

591 Views
You can use $literal operator along with aggregate framework. Let us first create a collection with documents −> db.fieldWithStaticValue.insertOne({"Name":"Larry", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6554c7924bb85b3f48948") } > db.fieldWithStaticValue.insertOne({"Name":"Chris", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd655567924bb85b3f48949") } > db.fieldWithStaticValue.insertOne({"Name":"David", "Age":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cd655607924bb85b3f4894a") }Following is the query to display all documents from a collection with the help of find() method −> db.fieldWithStaticValue.find();This will produce the following output −{ "_id" : ObjectId("5cd6554c7924bb85b3f48948"), "Name" : "Larry", "Age" : 24 } { "_id" : ObjectId("5cd655567924bb85b3f48949"), "Name" : "Chris", "Age" : 23 ... Read More

169 Views
You can use $in operator. Let us first create a collection with documents −> db.tagCountDemo.insertOne({"ListOfNames":["John", "Sam", "Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b387924bb85b3f48944") } > db.tagCountDemo.insertOne({"ListOfNames":["Bob", "David", "John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b4b7924bb85b3f48945") } > db.tagCountDemo.insertOne({"ListOfNames":["Mike", "Robert", "Chris"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b5d7924bb85b3f48946") } > db.tagCountDemo.insertOne({"ListOfNames":["James", "Carol", "Jace"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b717924bb85b3f48947") }Following is the query to display all documents from a collection with the help of find() method −> db.tagCountDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd64b387924bb85b3f48944"), "ListOfNames" : ... Read More

324 Views
To sort by sub-document match, you can use aggregate framework. Let us first create a collection with documents −> db.sortBySubDocumentsDemo.insertOne( { "StudentName": "Chris", "StudentDetails": [ { "Age":21, "StudentScore":91 }, { "Age":22, "StudentScore":99 }, { "Age":21, "StudentScore":93 ... Read More

323 Views
You can use $lte operator along with Date() for this. Let us first create a collection with documents. Here, we have set the date 2019-05-11, which is the current date −> db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-05-11")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563b17924bb85b3f4893b") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-01-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563bf7924bb85b3f4893c") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-05-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563ca7924bb85b3f4893d") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563e77924bb85b3f4893e") }Following is the query to display all documents from a collection with the help of find() method −> db.getDocumentsExpiredDemo.find().pretty();This ... Read More

393 Views
To update array in MongoDB document by variable index, use the below syntax. Here, yourIndexValue in the index value, where yourIndexVariableName is the variable name for index −var yourIndexVariableName= yourIndexValue, anyVariableName= { "$set": {} }; yourVariableName["$set"]["yourFieldName."+yourIndexVariableName] = "yourValue"; db.yourCollectionName.update({ "_id": yourObjectId}, yourVariableName);Let us first create a collection with documents −> db.updateByVariableDemo.insertOne({"StudentSubjects":["MySQL", "Java", "SQL Server", "PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd553c37924bb85b3f4893a") }Following is the query to display all documents from a collection with the help of find() method −> db.updateByVariableDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd553c37924bb85b3f4893a"), "StudentSubjects" : [ ... Read More