Found 6705 Articles for Database

MongoDB query with $all in array

AmitDiwan
Updated on 01-Apr-2020 11:52:49

376 Views

In MongoDB, $all is used to select the documents where the value of a field is an array that contains all the specified elementsLet us create a collection with documents −> db.demo163.insertOne( ...    { ...       "ClientDetails": [{ ...          "ClientName": "Chris" ... ...       }, { ...          "ClientName": "David" ... ...       } ...    ] ... ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3686d49e4f06af551997c5") } > db.demo163.insertOne( ...    { ...       "ClientDetails": [{ ...     ... Read More

Updating a MongoDB document and adding new keys only in the first document?

AmitDiwan
Updated on 01-Apr-2020 11:49:47

902 Views

This can be easily achieved using MongoDB update(). Let us create a collection with documents −> db.demo162.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3684359e4f06af551997c2") } > db.demo162.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3684389e4f06af551997c3") } > db.demo162.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36843c9e4f06af551997c4") }Display all documents from a collection with the help of find() method −> db.demo162.find();This will produce the following output −{ "_id" : ObjectId("5e3684359e4f06af551997c2"), "StudentName" : "Chris", "StudentAge" : 23 } { "_id" : ObjectId("5e3684389e4f06af551997c3"), "StudentName" : "Bob" } { "_id" : ObjectId("5e36843c9e4f06af551997c4"), "StudentName" : "David" }Here is the query to ... Read More

Search for multiple documents in MongoDB?

AmitDiwan
Updated on 01-Apr-2020 11:46:48

214 Views

To search for multiple documents in MongoDB, use $in. Let us create a collection with documents −> db.demo161.insertOne({"ClientId":101, "ClientName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3577cafdf09dd6d0853a09") } > db.demo161.insertOne({"ClientId":102, "ClientName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3577d0fdf09dd6d0853a0a") } > db.demo161.insertOne({"ClientId":103, "ClientName":"David", "ClientAge":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3577dffdf09dd6d0853a0b") } > db.demo161.insertOne({"ClientId":104, "ClientName":"Carol", "ClientAge":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3577eefdf09dd6d0853a0c") }Display all documents from a collection with the help of find() method −> db.demo161.find();This will produce the following output −{ "_id" : ObjectId("5e3577cafdf09dd6d0853a09"), "ClientId" : 101, "ClientName" : "Chris" } { ... Read More

GroupBy Date in MongoDB to count duplicate date records

AmitDiwan
Updated on 01-Apr-2020 11:44:06

265 Views

To count duplicate date records in MongoDB, use aggregate() and $group. Let us create a collection with documents −> db.demo160.insertOne({"DueDate":new ISODate()}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357525fdf09dd6d0853a04") } > db.demo160.insertOne({"DueDate":new ISODate("2019-01-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357532fdf09dd6d0853a05") } > db.demo160.insertOne({"DueDate":new ISODate()}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357534fdf09dd6d0853a06") } > db.demo160.insertOne({"DueDate":new ISODate("2019-01-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357538fdf09dd6d0853a07") } > db.demo160.insertOne({"DueDate":new ISODate("2020-04-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e357542fdf09dd6d0853a08") }Display all documents from a collection with the help of find() method −> db.demo160.find();This will produce the following ... Read More

Calculate average value in MongoDB

AmitDiwan
Updated on 01-Apr-2020 11:40:59

497 Views

To calculate average value in MongoDB, use aggregate() along with $avg. Let us create a collection with documents −> db.demo159.insertOne({"Score":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3557b2fdf09dd6d0853a01") } > db.demo159.insertOne({"Score":70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3557b6fdf09dd6d0853a02") } > db.demo159.insertOne({"Score":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3557c4fdf09dd6d0853a03") }Display all documents from a collection with the help of find() method −> db.demo159.find();This will produce the following output −{ "_id" : ObjectId("5e3557b2fdf09dd6d0853a01"), "Score" : 50 } { "_id" : ObjectId("5e3557b6fdf09dd6d0853a02"), "Score" : 70 } { "_id" : ObjectId("5e3557c4fdf09dd6d0853a03"), "Score" : 60 }Following is the query to ... Read More

Get execution stats in MongoDB for a collection

AmitDiwan
Updated on 01-Apr-2020 11:39:19

395 Views

To get stats, use explain() in MongoDB. Let us create a collection with documents −> db.demo157.insertOne({"Status":"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e354fdffdf09dd6d08539fc") } > db.demo157.insertOne({"Status":"InActive"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e354fe3fdf09dd6d08539fd") }Display all documents from a collection with the help of find() method −> db.demo157.find();This will produce the following output −{ "_id" : ObjectId("5e354fdffdf09dd6d08539fc"), "Status" : "Active" } { "_id" : ObjectId("5e354fe3fdf09dd6d08539fd"), "Status" : "InActive" }Following is how to implement explain() in MongoDB −> db.demo157.find({Status: { $in: ['Active', 'InActive'] }}).explain("executionStats");This will produce the following output −{    "queryPlanner" : {       "plannerVersion" ... Read More

How to sort by the difference in array contents in MongoDB?

AmitDiwan
Updated on 01-Apr-2020 11:33:27

192 Views

To sort by difference, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo155.insertOne({"Scores":[{"Value":45}, {"Value":50}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e354584fdf09dd6d08539e3") } > db.demo155.insertOne({"Scores":[{"Value":60}, {"Value":10}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e35458efdf09dd6d08539e4") } > db.demo155.insertOne({"Scores":[{"Value":100}, {"Value":95}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e354599fdf09dd6d08539e5") }Display all documents from a collection with the help of find() method −> db.demo155.find();This will produce the following output −{ "_id" : ObjectId("5e354584fdf09dd6d08539e3"), "Scores" : [ { "Value" : 45 }, { "Value" : 50 } ] } { "_id" : ObjectId("5e35458efdf09dd6d08539e4"), "Scores" : [ { "Value" ... Read More

Query a nested field within an array with MongoDB

AmitDiwan
Updated on 01-Apr-2020 11:06:59

317 Views

To query a nested field within an array, use $elemMatch in MongoDB. Let us create a collection with documents −> db.demo153.insertOne({"ClientDetails":[{"ClientName":"Chris", "ClientProject":"Online Banking System"}, {"ClientName":"David", "ClientProject":"Online School Management"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e351957fdf09dd6d08539df") } > db.demo153.insertOne({"ClientDetails":[{"ClientName":"Carol", "ClientProject":"Online Book System"}, {"ClientName":"Mike", "ClientProject":"Game Development"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3519c9fdf09dd6d08539e0") }Display all documents from a collection with the help of find() method −> db.demo153.find();This will produce the following output −{    "_id" : ObjectId("5e351957fdf09dd6d08539df"), "ClientDetails" : [       { "ClientName" : "Chris", "ClientProject" : "Online Banking System" },       { "ClientName" ... Read More

MongoDB query to update an array using FindAndUpdate()?

AmitDiwan
Updated on 01-Apr-2020 11:04:38

372 Views

To update an array, instead use findAndModify() in MongoDB. Let us create a collection with documents −> db.demo152.insertOne({"id":102, "Name":["Chris", "David"], Score:45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3515bcfdf09dd6d08539dd") } > db.demo152.insertOne({"id":103, "Name":["Mike", "Carol"], Score:65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3515cafdf09dd6d08539de") }Display all documents from a collection with the help of find() method −> db.demo152.find();This will produce the following output −{ "_id" : ObjectId("5e3515bcfdf09dd6d08539dd"), "id" : 102, "Name" : [ "Chris", "David" ], "Score" : 45 } { "_id" : ObjectId("5e3515cafdf09dd6d08539de"), "id" : 103, "Name" : [ "Mike", "Carol" ], "Score" : 65 }Following is the ... Read More

MongoDB projection result as an array of selected items?

AmitDiwan
Updated on 01-Apr-2020 11:02:17

166 Views

Let us create a collection with documents −> db.demo151.insertOne({"ListOfNames":["Chris", "David", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3513b6fdf09dd6d08539da") } > db.demo151.insertOne({"ListOfNames":["Mike", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3513c4fdf09dd6d08539db") } > db.demo151.insertOne({"ListOfNames":["John", "David", "Chris"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3513dcfdf09dd6d08539dc") }Display all documents from a collection with the help of find() method −> db.demo151.find();This will produce the following output −{ "_id" : ObjectId("5e3513b6fdf09dd6d08539da"), "ListOfNames" : [ "Chris", "David", "Mike" ] } { "_id" : ObjectId("5e3513c4fdf09dd6d08539db"), "ListOfNames" : [ "Mike", "Bob" ] } { "_id" : ObjectId("5e3513dcfdf09dd6d08539dc"), "ListOfNames" : [ "John", "David", "Chris" ] ... Read More

Advertisements