- 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 1359 Articles for MongoDB
25K+ Views
You can select a single field in MongoDB using the following syntax:db.yourCollectionName.find({"yourFieldName":yourValue}, {"yourSingleFieldName":1, _id:0});In the above syntax "yourSingleFieldName":1, _id:0 means get all data from one field without _id.To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:> db.singleFieldDemo.insertOne({"StudentName":"David", "StudentAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eba356fd07954a489067c") } > db.singleFieldDemo.insertOne({"StudentName":"Bob", "StudentAge":18}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eba406fd07954a489067d") } > db.singleFieldDemo.insertOne({"StudentName":"Chris", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eba4c6fd07954a489067e") } > db.singleFieldDemo.insertOne({"StudentName":"Robert", "StudentAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eba586fd07954a489067f") ... Read More
1K+ Views
To remove object from an array in MongoDB, you can use $pull operator. The syntax is as follows:db.yourCollectionName.update( {'_id':ObjectId("5c6ea036a0c51185aefbd14f")}, {$pull:{"yourArrayName":{"yourArrayFieldName":yourValue}}}, false, true);To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:> db.removeObject.insertOne({"CustomerName":"Maxwell", "CustomerAge":23, ... "CustomerDetails":[ ... { ... "CustomerId":100, ... "CustomerProduct":"Product-1" ... }, ... { ... "CustomerId":150, ... "CustomerProduct":"Product-2" ... }, ... { ... "CustomerId":200, ... "CustomerProduct":"Product-3" ... } ... ] ... }); { "acknowledged" : true, "insertedId" : ObjectId("5c6ea036a0c51185aefbd14f") }Display all documents from a collection with the help of find() method. The query is ... Read More
668 Views
To remove array element in MongoDB, you can use $pull and $in operator. The syntax is as follows:db.yourCollectionName.update({}, {$pull:{yourFirstArrayName:{$in:["yourValue"]}, yourSecondArrayName:"yourValue"}}, {multi:true} );To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:>db.removeArrayElement.insertOne({"StudentName":"Larry", "StudentCoreSubject":["MongoD B", "MySQL", "SQL Server", "Java"], "StudentFavouriteTeacher":["John", "Marry", "Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ec9c46fd07954a4890688") }Display all documents from a collection with the help of find() method. The query is as follows:> db.removeArrayElement.find().pretty();The following is the output:{ "_id" : ObjectId("5c6ec9c46fd07954a4890688"), "StudentName" : "Larry", "StudentCoreSubject" : [ ... Read More
651 Views
You can use find() method to find document with array that contains a specific value. The syntax is as follows:db.yourCollectionName.find({"yourArrayFieldName":"yourValue"}, .......N).pretty();To understand the above syntax, let us create a collection with documents. The query to create a collection with documents is as follows:>db.findSpecificValue.insertOne({"StudentId":1, "StudentName":"Larry", "FavouriteSubject":["C", "C++", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6e8996140577d89182b8d0") } >db.findSpecificValue.insertOne({"StudentId":2, "StudentName":"Larry", "FavouriteSubject":["MongoDB", "MySQL", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6e89b1140577d89182b8d1") }Display all documents from a collection with the help of find() method. The query is as follows:> db.findSpecificValue.find().pretty();The following is the output:{ "_id" : ObjectId("5c6e8996140577d89182b8d0"), "StudentId" ... Read More
195 Views
You can update the in exact element array in MongoDB with the help of below statement. The syntax is as follows:{"yourArrayDocumentName.$.yourNestedArrayDocument.yourPosition":"yourValue"}});To understand the above syntax, let us create a collection with some documents. The query to create a collection with document is as follows:> db.updateExactField.insertOne({"ActorId":1, "ActorDetails":[{"ActorName":"Johnny Depp", "MovieList": ["The Tourist", "Public Enemy"]}, ... {"ActorName":"Chris Evans", "MovieList":["Captain America", "Avengers"]}]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d7f63f2db199c1278e7f1") }Now you can display documents from a collection with the help of find() method. The query is as follows:> db.updateExactField.find().pretty();The following is the output:{ "_id" : ObjectId("5c6d7f63f2db199c1278e7f1"), "ActorId" : 1, ... Read More
289 Views
Yes, you can use regexp to make a case-insensitive query in MongoDB. The syntax is as follows:db.yourCollectionName.find({"yourFieldName":/^yourvalue$/i});To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows:> db.caseInsensitiveDemo.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d7a67f2db199c1278e7ef") } > db.caseInsensitiveDemo.insertOne({"Name":"JOHN"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d7ad6f2db199c1278e7f0") }Display all documents from a collection with the help of find(). The query is as follows:> db.caseInsensitiveDemo.find();The following is the output:{ "_id" : ObjectId("5c6d7a67f2db199c1278e7ef"), "Name" : "John" } { "_id" : ObjectId("5c6d7ad6f2db199c1278e7f0"), "Name" : "JOHN" }Here is the ... Read More
834 Views
Here, $push can be used to add new documents in nested array. To understand the above $push concept, let us create a collection with nested array document. The query to create a collection with document is as follows:>db.nestedArrayDemo.insertOne({"EmployeeName":"Larry", "EmployeeSalary":9000, "EmployeeDetails": [{"EmployeeDOB":new Date('1990-01-21'), "EmployeeDepartment":"ComputerScience", "EmployeeProject": [{"Technology":"C", "Duration":6}, {"Technology":"Java", "Duration":7}]}]});The following is the output:{ "acknowledged" : true, "insertedId" : ObjectId("5c6d73090c3d5054b766a76e") }Now you can display documents from a collection with the help of find() method. The query is as follows:> db.nestedArrayDemo.find().pretty();The following is the output:{ "_id" : ObjectId("5c6d73090c3d5054b766a76e"), "EmployeeName" : "Larry", "EmployeeSalary" : 9000, "EmployeeDetails" ... Read More
549 Views
You can use length to query for documents where array size is greater than 1:db.yourCollectionName.find({$where:"this.yourArrayDocumentName.length > 1"}).pretty();To understand the above syntax, let us create a collection with some documents. The query is as follows to create a collection with documents:>db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Larry", "StudentTechnicalSubje ct":["Java", "C", "C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c4c0c3d5054b766a76a") } >db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Maxwell", "StudentTechnicalSu bject":["MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c660c3d5054b766a76b") } >db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Maxwell", "StudentTechnicalSu bject":["MySQL", "SQL Server", "PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c800c3d5054b766a76c") }Display all documents from a collection with the help of find() method. The query is as follows:> db.arrayLengthGreaterThanOne.find().pretty();The ... Read More
600 Views
To update the objects in a document’s array, you need to use update() method. To understand the update() method, let us create a collection with document. The query to create a collection with document is as follows:> db.updateObjects.insertOne({"CustomerId":1, "CustomerName":"Larry", "TotalItems":100, ... "ItemDetails":[ ... { ... "NameOfItem":"Item_1", ... "Amount":450 ... }, ... { ... "NameOfItem":"Item_2", ... "Amount":500 ... }, ... { ... "NameOfItem":"Item_3", ... "Amount":200 ... } ... ] ... } ... );The following is the output:{ "acknowledged" : true, "insertedId" : ObjectId("5c6d688b0c3d5054b766a769") }Now you can display documents from a collection with the help of find() method. The query ... Read More
299 Views
You can use aggregate and unwind the array list before applying match. To understand the above concept, let us create a collection with documents. The query to create a collection with document is as follows:> db.filterArray.insertOne( { "L": [{ "N":1 }, { "N":2 } , { "N":3 }, { "N":4 }, { "N":5 } ]});The following is visible after running the above query:{ "acknowledged" : true, "insertedId" : ObjectId("5c6d63f2734e98fc0a434aeb") }Display document from a collection with the help of find() method. The query is as follows:> db.filterArray.find().pretty();The following is the output:{ "_id" : ObjectId("5c6d63f2734e98fc0a434aeb"), "L" : [ ... Read More