- 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
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
2K+ Views
You can use update() function to insert records in MongoDB if it does not exist. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.insertIfNotExistsDemo.insertOne({"StudentName":"Mike", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c7eec7b559dd2396bcfbfc2") } > db.insertIfNotExistsDemo.insertOne({"StudentName":"Sam", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c7eec97559dd2396bcfbfc3") }Display all documents from a collection with the help of find() method. The query is as follows −> db.insertIfNotExistsDemo.find().pretty(); The following is the output: { "_id" : ObjectId("5c7eec7b559dd2396bcfbfc2"), "StudentName" : "Mike", "StudentAge" : ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
250 Views
The syntax is as follows to rename a field for all documents. Here, we have used $renameLdb.yourCollectionName.update({}, {$rename:{"yourOldFieldName":"yourNewFieldName"}}, false, true);To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.renameFieldDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ee6c7559dd2396bcfbfbb") } > db.renameFieldDemo.insertOne({"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ee6cb559dd2396bcfbfbc") } > db.renameFieldDemo.insertOne({"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ee6cf559dd2396bcfbfbd") } > db.renameFieldDemo.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ee6d3559dd2396bcfbfbe") } > db.renameFieldDemo.insertOne({"StudentName":"Maxwell"}); { "acknowledged" : true, "insertedId" ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
133 Views
You can use the aggregate framework to get a particular element from the MongoDB array. To understand the concept, let us create a collection with the document. The query to create a collection with the document is as follows −> db.getParticularElement.insertOne({"InstructorName":"Larry", "InstructorTechnicalSubject":["Java", "C", "C++", "MongoDB", "MySQL", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ee027559dd2396bcfbfb1") }Display all documents from a collection with the help of find() method. The query is as follows −> db.getParticularElement.find().pretty();The following is the output −{ "_id" : ObjectId("5c7ee027559dd2396bcfbfb1"), "InstructorName" : "Larry", "InstructorTechnicalSubject" : [ "Java", ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
816 Views
To get the storage size of a database in MongoDB, use the stats() method.At first, check the current database with the help of the following query −> db;The following is the output −testHere is the query to get the storage size of the database in MongoDB −> db.stats()The following is the output displaying the stats including the storage size −The following is the output −{ "db" : "test", "collections" : 114, "views" : 0, "objects" : 391, "avgObjSize" : 83.0076726342711, "dataSize" : 32456, "storageSize" : 3211264, "numExtents" : 0, "indexes" : 120, "indexSize" : 2494464, "fsUsedSize" : 126172377088, "fsTotalSize" : 199229435904, "ok" : 1 }
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
1K+ Views
The following is the syntax to create MongoDB stored procedure −db.system.js.save ( { _id:"yourStoredProcedueName", value:function(argument1,....N) { statement1, . . N } } );Now implement the above syntax. The query to create a stored procedure is as follows −> db.system.js.save ( { _id:"addTwoValue", value:function(a,b) { return a+b } } );The following is the output −WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "addTwoValue" })Now you can call the stored procedure using eval(). The query is as follows −> db.eval("return addTwoValue(100,25)"); 125
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
564 Views
You can use positional operator $ for this. To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.incrementValueInNestedArrayDemo.insertOne( ... {"UniqueId":1, ... "StudentDetails": ... [ ... { ... "StudentId":101, ... "StudentMarks":97 ... }, ... { ... "StudentId":103, ... "StudentMarks":99 ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
456 Views
To find value in the array with multiple criteria, for example, you can use $elemMatch along with $gt and $lt. The syntax is as follows −db.yourCollectionName.find({yourFieldName:{$elemMatch:{$gt:yourNegativeValue, $lt:yourPo sitiveValue}}}).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findValueInArrayWithMultipleCriteriaDemo.insertOne({"StudentName":"Larry", "StudentMarks":[-150, 150]}); { "acknowledged" : true, "insertedId" : ObjectId("5c77daf6fc4e719b197a12f5") } > db.findValueInArrayWithMultipleCriteriaDemo.insertOne({"StudentName":"Mike", "StudentMarks":[19]}); { "acknowledged" : true, "insertedId" : ObjectId("5c77db09fc4e719b197a12f6") }Display all documents from a collection with the help of find() method. The query is as follows −> db.findValueInArrayWithMultipleCriteriaDemo.find().pretty();The following is the ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
2K+ Views
You can use $regex operator to check if a field contains a string in MongoDB. The syntax is as follows −db.yourCollectionName.findOne({"yourFieldName":{$regex:".*yourValue.*"}});To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.checkFieldContainsStringDemo.insertOne({"Id":1, "Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d762fc4e719b197a12ed") } > db.checkFieldContainsStringDemo.insertOne({"Id":2, "Name":"Johnson"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d76bfc4e719b197a12ee") } > db.checkFieldContainsStringDemo.insertOne({"Id":3, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d774fc4e719b197a12ef") } > db.checkFieldContainsStringDemo.insertOne({"Id":4, "Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d77cfc4e719b197a12f0") } > db.checkFieldContainsStringDemo.insertOne({"Id":5, "Name":"Sam"}); ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
2K+ Views
To find the string which has a length greater than a particular value in MongoDB, use the $where operator. The syntax is as follows −db.yourCollectionName.find({$where:'this.yourStringFieldName.length > yourIntegerValue'}).pretty();To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.stringFieldLengthDemo.insertOne({"UserId":1, "UserName":"Adam Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb4b2386c62d05142a78") } > db.stringFieldLengthDemo.insertOne({"UserId":2, "UserName":"Carol Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb562386c62d05142a79") } > db.stringFieldLengthDemo.insertOne({"UserId":3, "UserName":"James Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb5b2386c62d05142a7a") } > db.stringFieldLengthDemo.insertOne({"UserId":4, "UserName":"John Smith"}); { "acknowledged" ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
30K+ Views
To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.searchArrayDemo.insertOne({"EmployeeFirstName":"Adam", "EmployeeLastName":"Smith", "EmployeeDateOfBirth":new ISODate("1992-01-31 13:45:10"), ... "EmployeeSkills":["Spring and Hibernate Framework", "Machine Learning"], ... "EmployeeDetails":[ ... { ... "EmployeePerformanceArea":"Java", ... "Year":2001 ... }, ... { ... ... Read More