AmitDiwan has Published 10744 Articles

Match ID and fetch documents with $eq in MongoDB in case of array?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:57:13

182 Views

Use $eq operator along with find() to match ID and fetch documents. The $eq specifies equality condition. It matches documents where the value of a field equals the specified value.Let us create a collection with documents −> db.demo426.insert({"Ids":["110", "120", "101"]}); WriteResult({ "nInserted" : 1 }) > db.demo426.insert({"Ids":["100", "201", "401"]}); WriteResult({ ... Read More

Upsert many documents in MongoDB

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:55:28

1K+ Views

To upsert many documents, use UPSERT() with UPDATE(). Let us create a collection with documents −> db.demo425.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e74ee4fbbc41e36cc3cae6c") } > db.demo425.insertOne({"Name":"David", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e74ee56bbc41e36cc3cae6d") } > db.demo425.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,   ... Read More

Extract a MongoDB document with a specific string

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:52:59

278 Views

To extract a MongoDB document with a specific string, use $match in MongoDB. Let us create a collection with documents −> db.demo424.insert( ...    { ... ...       "Information" : [ ...          { ...             id:10, ...     ... Read More

How to speed up $group phase in aggregation?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:50:57

174 Views

To speed up the $group phase, use $group along with aggregation. Let us see an example and create a collection with documents −> db.demo423.insertOne({"Information":[101, 110, 87, 110, 98, 115, 101, 115, 89, 115]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e73a60e9822da45b30346e6") }Display all documents from a collection with the ... Read More

MongoDB query for exact match on multiple document fields

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:49:41

782 Views

For exact match, set the values to be matched inside MongoDB $in(). Let us first create a collection with documents −> db.demo422.insertOne({"Name":"Chris", "Marks":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e73a4059822da45b30346e1") } > db.demo422.insertOne({"Name":"Chris", "Marks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e73a40a9822da45b30346e2") } > db.demo422.insertOne({"Name":"David", "Marks":78}); { ... Read More

How to insert Date in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:47:42

1K+ Views

To insert date in MongoDB, use Date(). Let us create a collection with documents −> db.demo421.insert({"DueDate":new Date(Date.now())}); WriteResult({ "nInserted" : 1 }) > db.demo421.insert({"DueDate":new Date("2020-01-15")}); WriteResult({ "nInserted" : 1 }) > db.demo421.insert({"DueDate":new Date("2018-12-31")}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> ... Read More

Combining unique items from arrays in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:46:05

496 Views

To combine unique items from array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo420.insert( ...    { ... ...       "details" : [ ...          { ...             "Value1":10, ...           ... Read More

Update object in array with a specific key in MongoDB

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:42:15

778 Views

Let us first create a collection with documents −>db.demo419.insertOne({"ProductInformation":[{"ProductName":"Product-1", "ProductPrice":500}, {"ProductName":"Product-2", "ProductPrice":600}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e724762b912067e57771ae8") }Display all documents from a collection with the help of find() method −> db.demo419.find();This will produce the following output −{ "_id" : ObjectId("5e724762b912067e57771ae8"), "ProductInformation" : [ { "ProductName" : ... Read More

MongoDB query to filter object where all elements from nested array match the condition

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:40:35

775 Views

For this, use aggregate(). Let us first create a collection with documents −> db.demo418.insertOne( ...    { ...       "details":[ ...          { ...             "CountryName":"US", ...             "Marks":45 ...          }, ... Read More

Update salary field value with 10 percent of each employee in MongoDB

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:38:00

1K+ Views

Let us first create a collection with documents −> db.demo417.insertOne({"EmployeeName":"Chris", "EmployeeSalary":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723ebbb912067e57771ae4") } > db.demo417.insertOne({"EmployeeName":"Mike", "EmployeeSalary":1000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723ed7b912067e57771ae5") }Display all documents from a collection with the help of find() method −> db.demo417.find();This will produce the ... Read More

Advertisements