AmitDiwan has Published 10744 Articles

How to update a MongoDB document without overwriting the existing one?

AmitDiwan

AmitDiwan

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

853 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" : ... Read More

How to query MongoDB with a LIMIT?

AmitDiwan

AmitDiwan

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

209 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") ... Read More

How to efficiently run complex queries on MongoDB unindexed fields?

AmitDiwan

AmitDiwan

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

261 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, ... Read More

MongoDB query to update only a single item from voting (up and down) records?

AmitDiwan

AmitDiwan

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

132 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 ... Read More

MongoDB query to remove a specific document

AmitDiwan

AmitDiwan

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

202 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" : ... Read More

How to push an array in MongoDB?

AmitDiwan

AmitDiwan

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

527 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, ... Read More

How to copy attributes in MongoDB?

AmitDiwan

AmitDiwan

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

524 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 ... Read More

MongoDB query to convert an array to a map of documents with n attributes?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:36:17

764 Views

For this, you can use $map. Let us first create a collection with documents −> db.demo398.insertOne({ ...    "details":[ ...       { ...          "Name":"Chris", ...          "Age":22 ...       } ...    ] ... } ... ); {   ... Read More

Set MongoDB $slice with a range?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:35:28

253 Views

To set slice along with a range, use the $slice operator with parameters. These parameters are to be set for beginning position of the elements to be fetched and the 2nd parameter is for range. Let us create a collection with documents −> db.demo54.insertOne({"ListOfValues":[100, 2030, 5353, 7364, 635, 535, 524, ... Read More

How do I index “or” in MongoDB for indexing multiple fields?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:33:44

292 Views

To index multiple fields, use ensureIndex() for a combination. With ensureIndex(), we can create an index and even pass multiple fields. Let us create a collection with documents −> db.demo53.ensureIndex({"StudentFirstName":1, "StudentAge":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > ... Read More

Advertisements