MongoDB Query to Display Documents with a Specific Name Irrespective of Case

AmitDiwan
Updated on 14-May-2020 09:36:49

170 Views

For this, use $regex in MongoDB. We will search for document field value with name “David”, irrespective of case. Let us create a collection with documents −> db.demo700.insertOne( { details: [ { Name:"david" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6b1551299a9f98c93ac") } > db.demo700.insertOne( { details: [ { Name:"Chris" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6b9551299a9f98c93ad") } > db.demo700.insertOne( { details: [ { Name:"DAVID" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6bf551299a9f98c93ae") } > db.demo700.insertOne( { details: [ { Name:"David" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6c4551299a9f98c93af") }Display all documents ... Read More

Multiple Atomic Updates Using MongoDB

AmitDiwan
Updated on 14-May-2020 09:32:01

220 Views

For multiple atomic updates, use update() along with $set. Let us create a collection with documents −> db.demo699.insertOne({Name:"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e370551299a9f98c93a7") } > db.demo699.insertOne({Name:"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e37a551299a9f98c93a8") } > db.demo699.insertOne({Name:"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e381551299a9f98c93a9") } > db.demo699.insertOne({Name:"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e38a551299a9f98c93aa") }Display all documents from a collection with the help of find() method −> db.demo699.find();This will produce the following output −{ "_id" : ObjectId("5ea6e370551299a9f98c93a7"), "Name" : "Chris Brown" } { "_id" : ObjectId("5ea6e37a551299a9f98c93a8"), "Name" ... Read More

Get Email ID from MongoDB Document and Display with Print

AmitDiwan
Updated on 14-May-2020 09:29:48

477 Views

For this, use forEach() along with print() to display the email-id values. Let us create a collection with documents −> db.demo690.insertOne({"UserName":"John", "UserEmailId":"John@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db31551299a9f98c939c") } > db.demo690.insertOne({"UserName":"Bob", "UserEmailId":"Bob@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db3c551299a9f98c939d") } > db.demo690.insertOne({"UserName":"David", "UserEmailId":"David@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db47551299a9f98c939e") }Display all documents from a collection with the help of find() method −> db.demo690.find();This will produce the following output −{ "_id" : ObjectId("5ea6db31551299a9f98c939c"), "UserName" : "John", "UserEmailId" : "John@gmail.com" } { "_id" : ObjectId("5ea6db3c551299a9f98c939d"), "UserName" : "Bob", "UserEmailId" : "Bob@gmail.com" } { "_id" ... Read More

Increment Only a Single Value in MongoDB Document

AmitDiwan
Updated on 14-May-2020 09:27:54

164 Views

To update only a single value and increment it in MongoDB, use $inc along with update(). Let us create a collection with documents −> db.demo698.insertOne({Score:78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8a4551299a9f98c9398") } > db.demo698.insertOne({Score:56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8a7551299a9f98c9399") } > db.demo698.insertOne({Score:65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8aa551299a9f98c939a") } > db.demo698.insertOne({Score:88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8b0551299a9f98c939b") }Display all documents from a collection with the help of find() method −> db.demo698.find();This will produce the following output −{ "_id" : ObjectId("5ea6d8a4551299a9f98c9398"), "Score" : 78 } { "_id" : ... Read More

Get Number of Records in MongoDB

AmitDiwan
Updated on 14-May-2020 09:26:07

352 Views

To get number of records, use count() in MongoDB. Let us create a collection with documents −> db.demo697.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7d1551299a9f98c9395") } > db.demo697.insertOne({Name:"Bob", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7d8551299a9f98c9396") } > db.demo697.insertOne({Name:"David", Age:24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7dd551299a9f98c9397") }Display all documents from a collection with the help of find() method −> db.demo697.find();This will produce the following output −{ "_id" : ObjectId("5ea6d7d1551299a9f98c9395"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5ea6d7d8551299a9f98c9396"), "Name" : "Bob", "Age" : 23 } { "_id" : ObjectId("5ea6d7dd551299a9f98c9397"), "Name" ... Read More

Pull an Element in Sub-of-Sub Array in MongoDB

AmitDiwan
Updated on 14-May-2020 09:25:43

446 Views

To pull an element, use $pull along with $(positional) operator. Let us create a collection with documents −> db.demo679.insertOne( ...    { ...       id:1, ...       "details": [ ...          { ...             CountryName:"US", ...             "information": [ ... ...                { "Name": "Chris", "FirstName": "Name=Chris" }, ... ...                {"Name": "Bob", "FirstName": "Name=Bob" } ...             ] ...          }, ... ... Read More

Build Escape Regular Expressions in MongoDB

AmitDiwan
Updated on 14-May-2020 09:25:04

376 Views

For this, use find() along with //i. Let us create a collection with documents −> db.demo696.insertOne({Message:"/Good/"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d664551299a9f98c9391") } > db.demo696.insertOne({Message:"(good)"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d67a551299a9f98c9392") } > db.demo696.insertOne({Message:"/Bye/"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d68b551299a9f98c9393") } > db.demo696.insertOne({Message:"(GOOD)"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d693551299a9f98c9394") }Display all documents from a collection with the help of find() method −> db.demo696.find();This will produce the following output −{ "_id" : ObjectId("5ea6d664551299a9f98c9391"), "Message" : "/Good/" } { "_id" : ObjectId("5ea6d67a551299a9f98c9392"), "Message" : "(good)" } { "_id" : ObjectId("5ea6d68b551299a9f98c9393"), ... Read More

Index Collection Using Compound Multikey Index

AmitDiwan
Updated on 14-May-2020 09:24:10

114 Views

For this, use ensureIndex(). Let us create a collection with documents −> db.demo678.ensureIndex({id:1,"details.userId":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo678.insertOne( ...    { ...       id:101, ... ...       "details" : [ ...          { ...             "userId" : "1001", ...             "userName":"Chris" ...          }, ...          { ...             "userId" : "1002", ...             "userName":"David" ...          } ...       ], ...       "otherDetails" : [ ...          { ...             CountryName:"US", ...             EmailId:["Chris@gmail.com","David@gmail.com"] ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4276904263e90dac943fc") }Display all documents from a collection with the help of find() method −> db.demo678.find();This will produce the following output −{ "_id" : ObjectId("5ea4276904263e90dac943fc"), "id" : 101, "details" : [    { "userId" : "1001", "userName" : "Chris" },    { "userId" : "1002", "userName" : "David" } ], "otherDetails" : [    { "CountryName" : "US", "EmailId" : [ "Chris@gmail.com", "David@gmail.com" ] } ] }

Work with Array Fields in MongoDB to Match All

AmitDiwan
Updated on 14-May-2020 09:22:26

208 Views

To match all in MongoDB, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements. Let us create a collection with documents −> db.demo695.insertOne({"ListOfValues":[100, 200, 500, 800]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d4c4551299a9f98c938f") } > db.demo695.insertOne({"ListOfValues":[1000, 200, 4000]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d4cf551299a9f98c9390") }Display all documents from a collection with the help of find() method −> db.demo695.find();This will produce the following output −{ "_id" : ObjectId("5ea6d4c4551299a9f98c938f"), "ListOfValues" : [ 100, 200, 500, 800 ] } { "_id" : ObjectId("5ea6d4cf551299a9f98c9390"), "ListOfValues" ... Read More

Find Documents in MongoDB Based on Matched Objects in Array

AmitDiwan
Updated on 14-May-2020 09:20:21

164 Views

Let us see an example and create a collection with documents −> db.demo694.insertOne( ...    { ...       "details" : ...       [ ...          { ...             "Name" : "Chris", ...             Age:21 ...          }, ...          { ...             "Name" : "David", ...             Age:22 ...          } ...       ] ...    } ... ); {    "acknowledged" ... Read More

Advertisements