Found 1659 Articles for Big Data Analytics

How to pull even numbers from an array in MongoDB?

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

510 Views

Use $mod to get the even numbers and pull them from the array. Let us first create a collection with documents −>db.pullEvenNumbersDemo.insertOne({"AllNumbers":[101, 102, 104, 106, 108, 109, 110, 112, 14, 17, 18, 21]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd45b072cba06f46efe9eea") }Following is the query to display all documents from the collection with the help of find() method −> db.pullEvenNumbersDemo.find().pretty();This will produce the following output −{    "AllNumbers" : [       102,       104,       106,       108,       109,       110,       112,   ... Read More

Add a field to an embedded document in an array in MongoDB?

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

419 Views

You can use update() along with $ operator for this. Let us first create a collection with documents −> db.addAFieldDemo.insertOne( ...   { ... ...      "ClientName" : "Larry", ...      "ClientCountryName" : "US", ...      "ClientOtherDetails" : [ ...         { ...            "ClientProjectName":"Online Banking System" ...         } ...      ] ...   } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd44bdc2cba06f46efe9ee8") }Following is the query to display all documents from a collection with the help of find() method −>  db.addAFieldDemo.find().pretty();This ... Read More

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

Advertisements