AmitDiwan has Published 10744 Articles

Avoid MongoDB performance issues while using regex

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:44:51

450 Views

To avoid performance issues in MongoDB, use the concept of index. Let us create a collection with documents −> db.demo531.createIndex({"CountryName":"text", "Name":"text"});{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo531.insertOne({CountryName:"US", "Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2b60ef4dcbee04fbbbf2") } ... Read More

How to use ORDERBY in MongoDB if there are possible null values?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:43:43

719 Views

If there are null values also, then implement ORDERBY using sort().Note − Since, starting in MongoDB v3.2, the $orderby operator deprecated in the mongo shell. Use cursor.sort() instead.Let us create a collection with documents −> db.demo530.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2990ef4dcbee04fbbbec") } > db.demo530.insertOne({"Name":null});{    "acknowledged" : ... Read More

MongoDB query to group by _id

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:41:39

918 Views

To group by _id in MongoDB, use $group. Let us create a collection with documents −> db.demo529.insertOne({"Score":10});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b1d5bef4dcbee04fbbbe4") } > db.demo529.insertOne({"Score":20});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b1d5fef4dcbee04fbbbe5") } > db.demo529.insertOne({"Score":10});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b1d61ef4dcbee04fbbbe6") } > db.demo529.insertOne({"Score":10});{ ... Read More

Matching MongoDB collection items by id?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:38:24

443 Views

To match collection items by id, use $in in MongoDB. Let us create a collection with documents −> db.demo528.insertOne({"Name":"Chris", Age:21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b00d2ef4dcbee04fbbbe0") } > db.demo528.insertOne({"Name":"Bob", Age:22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b00d9ef4dcbee04fbbbe1") } > db.demo528.insertOne({"Name":"David", Age:20});{    "acknowledged" : true,    "insertedId" ... Read More

Perform Group in MongoDB and sum the price record of documents

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:37:01

109 Views

For this, use $group and within that, we need to work with $sum to add. Let us create a collection with documents −> db.demo527.insertOne({"Price":45.5});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8aff2fef4dcbee04fbbbdc") } > db.demo527.insertOne({"Price":55.5});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8aff34ef4dcbee04fbbbdd") } > db.demo527.insertOne({"Price":100.4});{    "acknowledged" : true, ... Read More

Updating an array with $push in MongoDB

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:35:21

452 Views

To update an array with $push, use updateOne() in MongoDB. Let us create a collection with documents −> db.demo526.insertOne( ... { ... ...    "CountryName": "US", ...    "TeacherName": "Bob", ...    "StudentInformation": [ ...       { ...          "Name": "Chris", ...       ... Read More

MongoDB query to find multiple matchings inside array of objects?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:32:37

1K+ Views

For this, use $and along with $regex. The $and performs a logical AND operation on an array of one or more expressions and selects the documents that satisfy all the expressions in the array.Let us create a collection with documents −> db.demo525.insertOne({"details":[{Name:"Chris", "CountryName":"US"}]});{    "acknowledged" : true,    "insertedId" : ... Read More

How to search date between two dates in MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:30:11

2K+ Views

To search date between two dates in MongoDB, use $gte and $lt. Let us create a collection with documents −> db.demo524.insertOne({"EndDate":new ISODate("2020-01-19")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8adbe5437efc8605595b63") } > db.demo524.insertOne({"EndDate":new ISODate("2020-01-20")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8adbec437efc8605595b64") } > db.demo524.insertOne({"EndDate":new ISODate("2020-12-31")});{    "acknowledged" : true, ... Read More

Why does my MongoDB group query return always 0 in float conversion? How to fix it?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:26:57

137 Views

For float conversion, use parseFloat() in MongoDB. Let us create a collection with documents −> db.demo523.insertOne({"details":{values:"-0.45"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e89b7efb3fbf26334ef611f") }Display all documents from a collection with the help of find() method −> db.demo523.find();This will produce the following output −{ "_id" : ObjectId("5e89b7efb3fbf26334ef611f"), "details" : { ... Read More

Get distinct pair of objects with all subdocuments in MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:24:34

360 Views

To get a distinct pair of objects, use $group. Let us create a collection with documents −> db.demo522.insertOne({"Name":"John", "Score":45});{    "acknowledged" : true,    "insertedId" : ObjectId("5e89b646b3fbf26334ef611b") } > db.demo522.insertOne({"Name":"Bob", "Score":67});{    "acknowledged" : true,    "insertedId" : ObjectId("5e89b64eb3fbf26334ef611c") } > db.demo522.insertOne({"Name":"John", "Score":55});{    "acknowledged" : true,    "insertedId" : ... Read More

Advertisements