Found 1349 Articles for MongoDB

Upsert many documents in MongoDB

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,    "insertedId" : ObjectId("5e74ee57bbc41e36cc3cae6e") } > db.demo425.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e74ee5cbbc41e36cc3cae6f") }Display all documents from a collection with the help of find() method −> db.demo425.find();This will produce the following output −{ "_id" : ObjectId("5e74ee4fbbc41e36cc3cae6c"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e74ee56bbc41e36cc3cae6d"), ... Read More

Extract a MongoDB document with a specific string

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

195 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, ...             Name:"Chris" ...          }, ...          { ...             id:11, ...             Name:"David" ...          } ...       ] ...    } ... ) WriteResult({ ... Read More

How to speed up $group phase in aggregation?

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

123 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 help of find() method −> db.demo423.find();This will produce the following output −{ "_id" : ObjectId("5e73a60e9822da45b30346e6"), "Information" : [ 101, 110, 87, 110, 98, 115, 101, 115, 89, 115 ] }Following is the query to speed up $group phase in aggregation −> db.demo423.aggregate([ ...    { ...       $project: ... Read More

MongoDB query for exact match on multiple document fields

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

621 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}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e73a4149822da45b30346e3") } > db.demo422.insertOne({"Name":"Sam", "Marks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e73a41e9822da45b30346e4") } > db.demo422.insertOne({"Name":"David", "Marks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e73a4239822da45b30346e5") }Display all documents from a collection with the help of find() method −> db.demo422.find();This will produce ... Read More

How to insert Date in MongoDB?

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

974 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 −> db.demo421.find();This will produce the following output −{ "_id" : ObjectId("5e73a2ec9822da45b30346de"), "DueDate" : ISODate("2020-03-19T16:50:52.746Z") } { "_id" : ObjectId("5e73a2fb9822da45b30346df"), "DueDate" : ISODate("2020-01-15T00:00:00Z") } { "_id" : ObjectId("5e73a3079822da45b30346e0"), "DueDate" : ISODate("2018-12-31T00:00:00Z") }Read More

Combining unique items from arrays in MongoDB?

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

420 Views

To combine unique items from array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo420.insert( ...    { ... ...       "details" : [ ...          { ...             "Value1":10, ...             "Value2":20, ...             "Value3":30 ...          } ...       ] ...    } ... ) WriteResult({ "nInserted" : 1 }) > db.demo420.insert( ...    { ... ...       "Info" : [ ...          { ... Read More

Update object in array with a specific key in MongoDB

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

683 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" : "Product-1", "ProductPrice" : 500 }, { "ProductName" : "Product-2", "ProductPrice" : 600 } ] }Following is the query to update object in array with a specific key −> db.demo419.update({'ProductInformation.ProductName' : "Product-1" }, { $set : { 'ProductInformation.$.ProductPrice' : 1250}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }Display ... Read More

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

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

637 Views

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

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

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

810 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 following output −{ "_id" : ObjectId("5e723ebbb912067e57771ae4"), "EmployeeName" : "Chris", "EmployeeSalary" : 500 } { "_id" : ObjectId("5e723ed7b912067e57771ae5"), "EmployeeName" : "Mike", "EmployeeSalary" : 1000 }Following is the query to update salary field value with 10 percent of each employee in employee collection −> db.demo417.update({ "EmployeeName": { $in: ["Chris", "Mike"] } }, ... Read More

Convert Collection to Capped in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:35:55

170 Views

To convert collection to capped, use runCommand(). It provides a helper to run specified database commands. Let us first create a collection with documents −> db.demo416.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a7bb912067e57771adf") } > db.demo416.insertOne({"StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a81b912067e57771ae0") } > db.demo416.insertOne({"StudentName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a84b912067e57771ae1") }Display all documents from a collection with the help of find() method −> db.demo416.find();This will produce the following output −{ "_id" : ObjectId("5e723a7bb912067e57771adf"), "StudentName" : "David" } { "_id" : ObjectId("5e723a81b912067e57771ae0"), "StudentName" : "Mike" } { "_id" : ObjectId("5e723a84b912067e57771ae1"), "StudentName" : ... Read More

Advertisements