Found 1349 Articles for MongoDB

Delete partial data in MongoDB?

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

94 Views

You can use map() for this. Let us first create a collection with documents −> db.deleteDemo.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd550492cba06f46efe9f06") } > db.deleteDemo.insertOne({"Name":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd5504d2cba06f46efe9f07") } > db.deleteDemo.insertOne({"Name":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd550512cba06f46efe9f08") } > db.deleteDemo.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd5505d2cba06f46efe9f09") } > db.deleteDemo.insertOne({"Name":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd550682cba06f46efe9f0a") } > db.deleteDemo.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd5506f2cba06f46efe9f0b") } > db.deleteDemo.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd550752cba06f46efe9f0c") } > db.deleteDemo.insertOne({"Name":"Bob"}); ... Read More

Selecting only a single field from MongoDB?

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

397 Views

You can use $and operator. Let us first create a collection with documents −>db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"John", "StudentAge":23, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd547142cba06f46efe9f02") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"Carol", "StudentAge":21, "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd5471f2cba06f46efe9f03") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"David", "StudentAge":24, "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd5472c2cba06f46efe9f04") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"Robert", "StudentAge":26, "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd548382cba06f46efe9f05") }Following is the query to display all documents from a collection with the help of find() method −> db.selectingASingleFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd547142cba06f46efe9f02"),    "StudentFirstName" : "John",    "StudentAge" : ... Read More

MongoDB divide aggregation operator?

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

215 Views

You can use aggregate framework for this. Let us first create a collection with documents −>db.aggregationOperatorDemo.insertOne({"FirstValue":392883,"SecondValue":10000000000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd541452cba06f46efe9f01") }Following is the query to display all documents from a collection with the help of find() method −> db.aggregationOperatorDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd541452cba06f46efe9f01"),    "FirstValue" : 392883,    "SecondValue" : 10000000000 }Following is the query to use divide aggregation operator −> db.aggregationOperatorDemo.aggregate([ ...   { "$project": { "Value": { "$divide": ["$FirstValue", "$SecondValue"] } } } ... ]);This will produce the following output −{ "_id" : ObjectId("5cd541452cba06f46efe9f01"), "Value" : 0.0000392883 }

Search an array of hashes in MongoDB?

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

159 Views

Let us first create a collection with documents −> db.searchAnArrayDemo.insertOne({_id:1, "TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 1 } > db.searchAnArrayDemo.insertOne({_id:2, "TechnicalDetails":[{"Language":"MySQL"}]}); { "acknowledged" : true, "insertedId" : 2 } > db.searchAnArrayDemo.insertOne({_id:3, "TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 3 } > db.searchAnArrayDemo.insertOne({_id:4, "TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 4 } > db.searchAnArrayDemo.insertOne({_id:5, "TechnicalDetails":[{"Language":"Java"}]}); { "acknowledged" : true, "insertedId" : 5 }Following is the query to display all documents from a collection with the help of find() method −> db.searchAnArrayDemo.find().pretty();This will produce the following output −{ "_id" : 1, "TechnicalDetails" : [ { "Language" : "MongoDB" } ] } ... Read More

Want to update inner field in a MongoDB

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

119 Views

To update the inner field, use the below syntax −db.yourCollectionName.update({"_id" : yourObjectId}, {$set : {"yourOuterFieldName.yourInnerFieldName" :yourValue}});Let us first create a collection with documents −> db.updateDocumentDemo.insertOne( ...   { ... ...      "StudentDetails" : { ...         "StudentFirstName" : "Adam", ...         "StudentLastName" : "Samith" ...      }, ...      "StudentOtherDetails" : { ...         "StudentFavouriteSubject" : "MySQL", ...         "StudentScore" : 45 ...      } ...   } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd50bb32cba06f46efe9efe") }Following is the query to ... Read More

How does MongoDB Update() method work to set records of entire field?

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

56 Views

You can use $set operator along with update(). Let us first create a collection with documents −> db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd506fe2cba06f46efe9efa") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd507022cba06f46efe9efb") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd507022cba06f46efe9efc") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd507032cba06f46efe9efd") }Following is the query to display all documents from a collection with the help of find() method −> db.workingOfUpdateMethod.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd506fe2cba06f46efe9efa"), "ClientCountryName" ... Read More

Query Array for 'true' value at index n in MongoDB?

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

98 Views

You can use dot(.) notation for this. Let us first create a collection with documents −>db.containsTrueValueDemo.insertOne({"IsMarried":[true, false, true, true, true, true, false, true, false, false, true]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd5039c2cba06f46efe9ef5") }Following is the query to display all documents from a collection with the help of find() method −> db.containsTrueValueDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd5039c2cba06f46efe9ef5"),    "IsMarried" : [       true,       false,       true,       true,       true,       true,       false,       true, ... Read More

How to find MongoDB documents in a collection with a filter on multiple combined fields?

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

270 Views

You can use $or operator along with find() for this. Let us first create a collection with documents −> db.findDocumentWithFilterDemo.insertOne({"ClientName":"Robert", "IsMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd4fd1e2cba06f46efe9ef1") } > db.findDocumentWithFilterDemo.insertOne({"ClientName":"Chris", "IsMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd4fd322cba06f46efe9ef2") } > db.findDocumentWithFilterDemo.insertOne({"ClientName":"David", "IsMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd4fd3b2cba06f46efe9ef3") } > db.findDocumentWithFilterDemo.insertOne({"ClientName":"Carol", "IsMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd4fd452cba06f46efe9ef4") }Following is the query to display all documents from a collection with the help of find() method −> db.findDocumentWithFilterDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd4fd1e2cba06f46efe9ef1"),    "ClientName" ... Read More

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

Advertisements