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
MongoDB Articles - Page 95 of 135
377 Views
You can use $avg operator along with aggregate framework. Let us first create a collection with documents −> db.averageOfRatingsInArrayDemo.insertOne( ... { ... "StudentDetails":[ ... { ... "StudentId":1, ... "StudentScore":45 ... }, ... { ... "StudentId":2, ... "StudentScore":58 ... }, ... { ... "StudentId":3, ... ... Read More
328 Views
You can use $or operator along with limit(1) to match element in array. Let us first create a collection with documents −> db.matchElementInArrayDemo.insertOne( ... { ... "StudentName" : "Chris" , ... "StudentOtherDetails" : ... [ ... {"StudentCountryName" : "US" , "StudentSkills" : "MongoDB"}, ... {"StudentCountryName" : "UK" , "StudentSkills" : "Java"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd423282cba06f46efe9ee2") } > db.matchElementInArrayDemo.insertOne( ... { ... "StudentName" : "Chris" , ... ... Read More
462 Views
To get specific object from array of objects, use positional operator($). Let us first create a collection with documents −> db.getASpecificObjectDemo.insertOne( ... { ... _id :1, f ... "CustomerName" : "Larry", ... "CustomerDetails" : { ... "CustomerPurchaseDescription": [{ ... id :100, ... "ProductName" : "Product-1", ... "Amount":10000 ... }, { ... id :101, ... "ProductName" : ... Read More
233 Views
You can use aggregate framework for this. Let us first create a collection with documents −> db.summingSubDocumentDemo.insertOne( ... { "_id" :101, "CustomerDetails" : { "CustomerPurchase" : { "CustomerPurchaseAmount" : 2000 } } }); { "acknowledged" : true, "insertedId" : 101 } > db.summingSubDocumentDemo.insertOne( { "_id" :102, "CustomerDetails" : { "CustomerPurchase" : { "CustomerPurchaseAmount" : 3000 } } }); { "acknowledged" : true, "insertedId" : 102 } > db.summingSubDocumentDemo.insertOne( { "_id" :103, "CustomerDetails" : { "CustomerPurchase" : { "CustomerPurchaseAmount" : 5000 } } }); { "acknowledged" : true, "insertedId" : 103 }Following is the query to display all documents ... Read More
196 Views
To retrieve multiple items in an array, use aggregate framework. Let us first create a collection with documents −> db.retrieveMultipleDemo.insertOne( ... { ... "UserDetails": ... [ ... { "_id": "101", "UserName":"John", "UserAge": 23 }, ... { "_id": "102", "UserName":"Carol", "UserAge": 21 }, ... { "_id": "103", "UserName":"David", "UserAge": 23}, ... { "_id": "104", "UserName":"Sam", "UserAge": 25} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd40c85edc6604c74817cf0") }Following is the query to ... Read More
139 Views
You can use aggregate framework for this. Here, we will get the sum and then match it to search for documents less than a particular number. Let us first create a collection with documents −> db.searchDocumentsDemo.insertOne({"Value1":100, "Value2":560}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe1eedc6604c74817ce9") } > db.searchDocumentsDemo.insertOne({"Value1":300, "Value2":150}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe29edc6604c74817cea") } > db.searchDocumentsDemo.insertOne({"Value1":400, "Value2":200}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe30edc6604c74817ceb") } > db.searchDocumentsDemo.insertOne({"Value1":190, "Value2":210}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe45edc6604c74817cec") }Following is the query to display all documents from a collection with the help of ... Read More
351 Views
Let us first create a collection with documents −>db.missingDocumentDemo.insertOne({"StudentFirstName":"Adam", "StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fb1eedc6604c74817ce6") } >db.missingDocumentDemo.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fb29edc6604c74817ce7") } >db.missingDocumentDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fb40edc6604c74817ce8") }Following is the query to display all documents from a collection with the help of find() method −> db.missingDocumentDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd3fb1eedc6604c74817ce6"), "StudentFirstName" : "Adam", "StudentLastName" : "Smith" } { "_id" : ObjectId("5cd3fb29edc6604c74817ce7"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor" } { "_id" : ... Read More
152 Views
You can use $and operator for this. Let us first create a collection with documents −> db.twoSpecificIdsDemo.insertOne( ... { ... PlayerId:1, ... "PlayerDetails": [{ ... id: 100, ... "PlayerName":"Chris" ... }, { ... id: 101, ... "PlayerName":"Sam" ... }, { ... id: 102, ... "PlayerName":"Robert" ... }, { ... id: 103, ... "PlayerName":"Carol" ... }] ... Read More
210 Views
Use $pull operator along with positional operator($) in MongoDB. Let us first create a collection with documents −> db.pullWithPositionalOperatorDemo.insertOne( ... { ... _id: 100, ... "StudentDetails": [ ... { ... "StudentId": "STU-1", ... "StudentFavouriteSubject": ["MongoDB", "Java"] ... }, ... { ... "StudentId": "STU-2", ... "StudentFavouriteSubject": ["PHP", "MySQL"] ... } ... ] ... } ... ); { ... Read More
323 Views
To search a sub-filed in MongoDB, you can use double quotes along with dot notation. Let us first create a collection with documents −> db.searchSubFieldDemo.insertOne( ... { ... "UserDetails": ... {"UserEmailId":"John123@gmail.com", "UserAge":21} ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd3d909edc6604c74817ce2") } > db.searchSubFieldDemo.insertOne( { "UserDetails": {"UserEmailId":"Carol@yahoo.com", "UserAge":26} } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd3d9a4edc6604c74817ce3") }Following is the query to display all documents from a collection with the help of find() method −> db.searchSubFieldDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd3d909edc6604c74817ce2"), "UserDetails" ... Read More