AmitDiwan has Published 10744 Articles

MongoDB query to match documents with array values greater than a specific value

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:37:15

851 Views

You can use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo701.insertOne({"ListOfValues":[100, 200, 300]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e8cf551299a9f98c93b0") } > db.demo701.insertOne({"ListOfValues":[500, 700, 1000]}); ... Read More

MongoDB query to display documents with a specific name irrespective of case

AmitDiwan

AmitDiwan

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

141 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: [ { ... Read More

Multiple atomic updates using MongoDB?

AmitDiwan

AmitDiwan

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

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

How do I get email-id from a MongoDB document and display with print()

AmitDiwan

AmitDiwan

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

462 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"}); {   ... Read More

Increment only a single value in MongoDB document?

AmitDiwan

AmitDiwan

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

150 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}); { ... Read More

Get number of records in MongoDB?

AmitDiwan

AmitDiwan

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

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

Pull an element in sub of sub-array in MongoDB?

AmitDiwan

AmitDiwan

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

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

Build (escape) regexp in MongoDB?

AmitDiwan

AmitDiwan

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

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

How to index my collection to use a compound multikey index?

AmitDiwan

AmitDiwan

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

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

How do I work with array fields in MongoDB to match all?

AmitDiwan

AmitDiwan

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

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

Advertisements