Conditional Update in MongoDB

AmitDiwan
Updated on 03-Apr-2020 12:44:30

960 Views

Use update() for conditional update in MongoDB. Let us first create a collection with documents −> db.demo402.insertOne({id:101, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e61214efac4d418a0178585") } > db.demo402.insertOne({id:102, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e612150fac4d418a0178586") } > db.demo402.insertOne({id:103, "Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e612152fac4d418a0178587") }Display all documents from a collection with the help of find() method −> db.demo402.find();This will produce the following output −{ "_id" : ObjectId("5e61214efac4d418a0178585"), "id" : 101, "Name" : "Chris" } { "_id" : ObjectId("5e612150fac4d418a0178586"), "id" : 102, "Name" : "David" } { "_id" : ObjectId("5e612152fac4d418a0178587"), "id" : ... Read More

MongoDB Query to Get Date Records in a Range

AmitDiwan
Updated on 03-Apr-2020 12:43:41

391 Views

To get date records in a range, use $gt along with $lt. Let us create a collection with documents −> db.demo60.insertOne({"ArrivalDate":new ISODate("2019-01-11 12:30:10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2863fecfb11e5c34d89927") } > db.demo60.insertOne({"ArrivalDate":new ISODate("2019-10-12 03:10:00")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28641acfb11e5c34d89928") } > db.demo60.insertOne({"ArrivalDate":new ISODate("2019-01-14 05:11:20")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28642acfb11e5c34d89929") }Display all documents from a collection with the help of find() method −> db.demo60.find();This will produce the following output −{ "_id" : ObjectId("5e2863fecfb11e5c34d89927"), "ArrivalDate" : ISODate("2019-01-11T12:30:10Z") } { "_id" : ObjectId("5e28641acfb11e5c34d89928"), "ArrivalDate" : ISODate("2019-10-12T03:10:00Z") } { "_id" : ObjectId("5e28642acfb11e5c34d89929"), "ArrivalDate" ... Read More

Set Multiple Conditions in MongoDB and Fetch Value in a Range

AmitDiwan
Updated on 03-Apr-2020 12:42:28

168 Views

Let us create a collection with documents −> db.demo59.insertOne({"Values":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e286286cfb11e5c34d89923") } > db.demo59.insertOne({"Values":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e286288cfb11e5c34d89924") } > db.demo59.insertOne({"Values":58}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28628ccfb11e5c34d89925") } > db.demo59.insertOne({"Values":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28628fcfb11e5c34d89926") }Display all documents from a collection with the help of find() method −> db.demo59.find();This will produce the following output −{ "_id" : ObjectId("5e286286cfb11e5c34d89923"), "Values" : 50 } { "_id" : ObjectId("5e286288cfb11e5c34d89924"), "Values" : 10 } { "_id" : ObjectId("5e28628ccfb11e5c34d89925"), "Values" : 58 } { "_id" : ... Read More

Update MongoDB Document Without Overwriting Existing One

AmitDiwan
Updated on 03-Apr-2020 12:42:07

863 Views

To update only a field value, use update() along with $set. This won’t overwrite the existing one. Let us first create a collection with documents −> db.demo401.insertOne( ...    { ...       "_id" : 1001, ...       "Name" : "Chris", ...       "SubjectName" : "MongoDB", ...       "Score" : 45 ...    } ... ); { "acknowledged" : true, "insertedId" : 1001 }Display all documents from a collection with the help of find() method −> db.demo401.find();This will produce the following output −{ "_id" : 1001, "Name" : "Chris", "SubjectName" : "MongoDB", "Score" ... Read More

Query MongoDB with a Limit

AmitDiwan
Updated on 03-Apr-2020 12:41:18

214 Views

To query MongoDB with limit, use LIMIT() method. Let us create a collection with documents −> db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f8fcfb11e5c34d8991f") } > db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f93cfb11e5c34d89920") } > db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f94cfb11e5c34d89921") } > db.demo58.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f99cfb11e5c34d89922") }Display all documents from a collection with the help of find() method −> db.demo58.find();This will produce the following output −{ "_id" : ObjectId("5e285f8fcfb11e5c34d8991f"), "Name" : "David" } { "_id" : ObjectId("5e285f93cfb11e5c34d89920"), "Name" : "David" } { "_id" : ... Read More

Efficiently Run Complex Queries on MongoDB Unindexed Fields

AmitDiwan
Updated on 03-Apr-2020 12:40:09

266 Views

Create an index to efficiently run complex queries. Let us first create a collection with documents −> db.demo400.insertOne({SubjectName:"Java Spring"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610720fac4d418a0178572") } > db.demo400.insertOne({SubjectName:"Spring Hibernate"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e61072dfac4d418a0178573") } > db.demo400.insertOne({SubjectName:"Java Hibernate"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610736fac4d418a0178574") } > db.demo400.createIndex({SubjectName:"text"}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Display all documents from a collection with the help of find() method −> db.demo400.find();This will produce the following output −{ "_id" : ObjectId("5e610720fac4d418a0178572"), "SubjectName" : "Java ... Read More

Update a Single Item in MongoDB Voting Records

AmitDiwan
Updated on 03-Apr-2020 12:39:49

137 Views

Let us first create a collection with documents −> db.demo57.insertOne({"Votes":{"VoterName":"Chris", "TotalVote":50}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285bb8cfb11e5c34d8991a") } > db.demo57.insertOne({"Votes":{"VoterName":"David", "TotalVote":101}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285bc3cfb11e5c34d8991b") }Display all documents from a collection with the help of find() method −> db.demo57.find();This will produce the following output −{ "_id" : ObjectId("5e285bb8cfb11e5c34d8991a"), "Votes" : { "VoterName" : "Chris", "TotalVote" : 50 } } { "_id" : ObjectId("5e285bc3cfb11e5c34d8991b"), "Votes" : { "VoterName" : "David", "TotalVote" : 101 } }Here is the query to update only a single item (TotalVote) −> db.demo57.update({"Votes.VoterName":"David" }, { $inc : { "Votes.TotalVote" ... Read More

MongoDB Query to Remove a Specific Document

AmitDiwan
Updated on 03-Apr-2020 12:39:02

209 Views

To remove a specific document, use remove() in MongoDB. Let us create a collection with documents −> db.demo56.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e0bcfb11e5c34d89917") } > db.demo56.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e10cfb11e5c34d89918") } > db.demo56.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e13cfb11e5c34d89919") }Display all documents from a collection with the help of find() method −> db.demo56.find();This will produce the following output −{ "_id" : ObjectId("5e272e0bcfb11e5c34d89917"), "Name" : "Chris" } { "_id" : ObjectId("5e272e10cfb11e5c34d89918"), "Name" : "David" } { "_id" : ObjectId("5e272e13cfb11e5c34d89919"), "Name" : "Bob" }Following is the query to remove a ... Read More

Push an Array in MongoDB

AmitDiwan
Updated on 03-Apr-2020 12:38:25

534 Views

To push an array, use $push in MongoDB. Let us first create a collection with documents −> db.demo399.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610339fac4d418a017856d") } > db.demo399.insertOne({Name:"David", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610341fac4d418a017856e") } > db.demo399.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610355fac4d418a017856f") } > db.demo399.insertOne({Name:"Bob", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e61035efac4d418a0178570") } > db.demo399.insertOne({Name:"David", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610364fac4d418a0178571") }Display all documents from a collection with the help of find() method −> db.demo399.find();This will produce the following output −{ ... Read More

Copy Attributes in MongoDB

AmitDiwan
Updated on 03-Apr-2020 12:37:39

531 Views

To copy the value of one attribute to another, use $set along with update(). Let us create a collection with documents −> db.demo55.insertOne({"ShippingDate":'', "date":new ISODate("2019-01-21")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2716dfcfb11e5c34d89915") } > db.demo55.insertOne({"ShippingDate":'', "date":new ISODate("2020-05-12")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2716ebcfb11e5c34d89916") }Display all documents from a collection with the help of find() method −> db.demo55.find();This will produce the following output −{ "_id" : ObjectId("5e2716dfcfb11e5c34d89915"), "ShippingDate" : "", "date" : ISODate("2019-01-21T00:00:00Z") } { "_id" : ObjectId("5e2716ebcfb11e5c34d89916"), "ShippingDate" : "", "date" : ISODate("2020-05-12T00:00:00Z") }Following is the query to copy attributes in MongoDB −> db.demo55.find({}).forEach(function(c){ ... ... Read More

Advertisements