Found 1349 Articles for MongoDB

Calculate average of ratings in array and then include the field to original document in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

281 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

Match element in array of MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

220 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

How to get a specific object from array of objects inside specific MongoDB document?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

363 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

Sum MongoDB Sub-documents field?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

136 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

Query to retrieve multiple items in an array in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

112 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

How to search for documents based on the value of adding two properties in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

57 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

Insert MongoDB document field only when it's missing?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

238 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

Find all documents that have two specific id's in an array of objects in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

75 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

MongoDB pull with positional operator?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

92 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

Search a sub-field on MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

235 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

Advertisements