Sort MongoDB Documents in Ascending Order with Aggregation

AmitDiwan
Updated on 13-May-2020 09:08:39

220 Views

Use $sort in MongoDB aggregation. Let us create a collection with documents −> db.demo652.insertOne({ ...    value:10, ...    "details" : [{ ...       "ProductName" : "Product-1", ...       "ProductQuantity" : 8, ...       "ProductPrice" : 500 ...    }, { ...          "ProductName" : "Product-2", ...          "ProductQuantity" : 7, ...          "ProductPrice" : 500 ... ...       }] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0730e3c3cd0dcff36a62") } > > db.demo652.insertOne({ ...    value:5, ...   ... Read More

Avoid MongoDB Performance Issues While Using Regex

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

454 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") } > db.demo531.insertOne({CountryName:"UK", "Name":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2b6cef4dcbee04fbbbf3") } > db.demo531.insertOne({CountryName:"AUS", "Name":"chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2b74ef4dcbee04fbbbf4") } > db.demo531.insertOne({CountryName:"US", "Name":"CHRIS"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2badef4dcbee04fbbbf5") }Display all documents from a collection with the help of find() method −> db.demo531.find();This will ... Read More

Use OrderBy in MongoDB with Possible Null Values

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

730 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" : true,    "insertedId" : ObjectId("5e8b2991ef4dcbee04fbbbed") } > db.demo530.insertOne({"Name":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2992ef4dcbee04fbbbee") } > db.demo530.insertOne({"Name":"Adam"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2995ef4dcbee04fbbbef") } > db.demo530.insertOne({"Name":null});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2999ef4dcbee04fbbbf0") } > db.demo530.insertOne({"Name":"Carol"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b299eef4dcbee04fbbbf1") }Display ... Read More

MongoDB Query to Group by ID

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

923 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});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b1d62ef4dcbee04fbbbe7") }Display all documents from a collection with the help of find() method −> db.demo529.find();This will produce the following output −{ "_id" : ObjectId("5e8b1d5bef4dcbee04fbbbe4"), "Score" : 10 } { "_id" : ObjectId("5e8b1d5fef4dcbee04fbbbe5"), "Score" : 20 } { "_id" : ObjectId("5e8b1d61ef4dcbee04fbbbe6"), "Score" : 10 ... Read More

Matching MongoDB Collection Items by ID

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

445 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" : ObjectId("5e8b00e0ef4dcbee04fbbbe2") }Display all documents from a collection with the help of find() method −> db.demo528.find();This will produce the following output −{ "_id" : ObjectId("5e8b00d2ef4dcbee04fbbbe0"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e8b00d9ef4dcbee04fbbbe1"), "Name" : "Bob", "Age" : 22 } { "_id" : ObjectId("5e8b00e0ef4dcbee04fbbbe2"), "Name" : "David", ... Read More

Group in MongoDB and Sum Price Records of Documents

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

116 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,    "insertedId" : ObjectId("5e8aff39ef4dcbee04fbbbde") } > db.demo527.insertOne({"Price":200.6});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8aff40ef4dcbee04fbbbdf") }Display all documents from a collection with the help of find() method −> db.demo527.find();This will produce the following output −{ "_id" : ObjectId("5e8aff2fef4dcbee04fbbbdc"), "Price" : 45.5 } { "_id" : ObjectId("5e8aff34ef4dcbee04fbbbdd"), "Price" : 55.5 } ... Read More

Update an Array with Push in MongoDB

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

457 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", ...          "Subject": "MySQL", ...          "ListOfMailId":[] ...       }, ...       { ...          "Name": "David", ...          "Subject": "MongoDB", ...          "ListOfMailId":[] ... ...       } ...    ] ... } ... Read More

MongoDB Query to Find Multiple Matchings Inside Array of Objects

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" : ObjectId("5e8ae5ef437efc8605595b66") } > db.demo525.insertOne({"details":[{Name:"Bob", "CountryName":"UK"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ae5f8437efc8605595b67") } > db.demo525.insertOne({"details":[{Name:"chris", "CountryName":"us"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ae602437efc8605595b68") } > db.demo525.insertOne({"details":[{Name:"Mike", "CountryName":"UK"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ae60b437efc8605595b69") }Display all documents from a collection with the help of find() method −> ... Read More

Search Date Between Two Dates in MongoDB

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,    "insertedId" : ObjectId("5e8adbf3437efc8605595b65") }Display all documents from a collection with the help of find() method −> db.demo524.find();This will produce the following output −{ "_id" : ObjectId("5e8adbe5437efc8605595b63"), "EndDate" : ISODate("2020-01-19T00:00:00Z") } { "_id" : ObjectId("5e8adbec437efc8605595b64"), "EndDate" : ISODate("2020-01-20T00:00:00Z") } { "_id" : ObjectId("5e8adbf3437efc8605595b65"), "EndDate" : ISODate("2020-12-31T00:00:00Z") }Following is the query ... Read More

Fix MongoDB Group Query Returning 0 in Float Conversion

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

144 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" : { "values" : "-0.45" } }Following is the query that does not result 0 in the conversion of float −>db.getCollection('demo523').find({}).forEach( function(d) ... { d.details.values = parseFloat( d.details.values ) ... db.getCollection('demo523').save(d)} );Display all documents from a collection with the help of find() method −> db.demo523.find();This will produce the following output −{ "_id" ... Read More

Advertisements