Set Multiple Conditions in MongoDB and Fetch Value in a Range

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

163 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

851 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

208 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

260 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

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 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

200 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

526 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

523 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

Convert Array to Map of Documents with N Attributes in MongoDB

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

761 Views

For this, you can use $map. Let us first create a collection with documents −> db.demo398.insertOne({ ...    "details":[ ...       { ...          "Name":"Chris", ...          "Age":22 ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e8cedfac4d418a017856c") }Display all documents from a collection with the help of find() method −> db.demo398.find();This will produce the following output −{ "_id" : ObjectId("5e5e8cedfac4d418a017856c"), "details" : [ { "Name" : "Chris", "Age" : 22 } ] }Following is the query to convert an array ... Read More

Set MongoDB Slice with a Range

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, 423, 2434, 1323, 799874, 90]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e27151ecfb11e5c34d89914") }Display all documents from a collection with the help of find() method −> db.demo54.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e27151ecfb11e5c34d89914"),    "ListOfValues" : [       100,       2030, ... Read More

Advertisements