MongoDB Query to Find Matching Documents Given an Array with Values

AmitDiwan
Updated on 13-May-2020 06:56:14

215 Views

For specific documents, use MongoDB $in. Let us create a collection with documents −> db.demo511.insertOne({"ListOfProject":["Library Management System", "Hospital Management System"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e88473a987b6e0e9d18f585") } > db.demo511.insertOne({"ListOfProject":["Online Web Tracking", "Library Management System"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e884751987b6e0e9d18f586") } > db.demo511.insertOne({"ListOfProject":["Online Shopping Cart", "Hospital Management System"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e884776987b6e0e9d18f587") }Display all documents from a collection with the help of find() method −> db.demo511.find();This will produce the following output −{ "_id" : ObjectId("5e88473a987b6e0e9d18f585"), "ListOfProject" : [ "Library Management System", "Hospital Management System" ] } { "_id" : ... Read More

Match and Group Array Elements with Max Value in MongoDB Aggregation

AmitDiwan
Updated on 13-May-2020 06:54:21

621 Views

For this, use $group along with $max in MongoDB. Let us create a collection with documents −> db.demo510.insertOne( ... { ...    details:[ ...       { ...          Name:"Chris", ...          Score:56 ...       }, ...       { ...          Name:"David", ...          Score:45 ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8845fa987b6e0e9d18f582") } > db.demo510.insertOne( ... { ...    details:[ ...       { ...       ... Read More

Implement MongoDB addToSet for Array in Array and Append a Value

AmitDiwan
Updated on 13-May-2020 06:48:29

390 Views

For this, use update() along with $addToSet. The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array. Let us create a collection with documents −> db.demo509.insertOne( ... { ... ...    "value1" : [ ...       { ...          "value2" : [ ...             76, ...             14, ...             56 ...          ] ...       }, ...       { ... Read More

Does MongoDB Track Index Usage in Queries?

AmitDiwan
Updated on 13-May-2020 06:38:55

141 Views

Yes, you can track how many times each index is used in a query using MongoDB $indexStats. Following is the query to create an index in MongoDB −> db.demo508.createIndex({"FirstName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Let us create a collection with documents −> db.demo508.insertOne({"FirstName":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e883818987b6e0e9d18f578") } > db.demo508.insertOne({"FirstName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88381b987b6e0e9d18f579") } > db.demo508.insertOne({"FirstName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e88381f987b6e0e9d18f57a") }Display all documents from a collection with the help of find() method −> db.demo508.find();This will ... Read More

Increment Value of an Array Element with Array Object in MongoDB

AmitDiwan
Updated on 13-May-2020 06:36:04

763 Views

To increment the value of an array object, use $inc. Let us create a collection with documents −>db.demo506.insertOne({"details":[{id:1, Quantity:4}, {id:2, Quantity:3}, {id:3, Quantity:2}, {id:4, Qua ntity:7}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e882ed6987b6e0e9d18f576") }Display all documents from a collection with the help of find() method −> db.demo506.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e882ed6987b6e0e9d18f576"),    "details" : [       {          "id" : 1,          "Quantity" : 4       },       {          "id" : 2,         ... Read More

Filter Fields and Fetch Specific Subject Name Value in MongoDB

AmitDiwan
Updated on 13-May-2020 06:35:54

1K+ Views

To filter and fetch, use projection along with MongoDB $filter and $match. Let us create a collection with documents −> db.demo507.insertOne( ... { ... ...    "Information": ...    [ ...       {"Name":"John", "SubjectName":"MySQL"}, ...       {"Name":"Bob", "SubjectName":"MongoDB"}, ...       {"Name":"Chris", "SubjectName":"MySQL"}, ...       {"Name":"David", "SubjectName":"C++"} ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8836d3987b6e0e9d18f577") }Display all documents from a collection with the help of find() method −> db.demo507.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e8836d3987b6e0e9d18f577"),    "Information" : [     ... Read More

MongoDB Query to Sort Nested Array

AmitDiwan
Updated on 13-May-2020 06:29:56

2K+ Views

To sort nested array in MongoDB, use $sort. Let us create a collection with documents −> db.demo505.insertOne( ... { ...    "details": [ ...    { ...       Name:"Chris", ...       "Score":58 ...    }, { ... ...          Name:"Bob", ...          "Score":45 ...       }, { ... ...             Name:"John", ...             "Score":68 ...       }, { ... ...          Name:"David", ...          "Score":46 ...       } ... Read More

Find User by Name with MongoDB

AmitDiwan
Updated on 13-May-2020 06:27:09

336 Views

To find the user by name in MongoDB, use find(). Let us create a collection with documents −> db.demo504.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8823ee987b6e0e9d18f570") } > db.demo504.insertOne({"Name":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8823f2987b6e0e9d18f571") } > db.demo504.insertOne({"Name":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8823f5987b6e0e9d18f572") }Display all documents from a collection with the help of find() method −> db.demo504.find();This will produce the following output −{ "_id" : ObjectId("5e8823ee987b6e0e9d18f570"), "Name" : "Chris" } { "_id" : ObjectId("5e8823f2987b6e0e9d18f571"), "Name" : "John" } { "_id" : ObjectId("5e8823f5987b6e0e9d18f572"), "Name" : "David" }Following is the query to find a user by ... Read More

MongoDB Slice Array in Populated Field

AmitDiwan
Updated on 13-May-2020 06:25:44

283 Views

To slice array, use a $slice operator in MongoDB. Let us create a collection with documents −> db.demo503.insertOne({_id:1, Name:"John", Subject:["MySQL", "Java", "C"]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo503.insertOne({_id:2, Name:"David", Subject:["MongoDB", "C++", "Python"]}); { "acknowledged" : true, "insertedId" : 2 }Display all documents from a collection with the help of find() method −> db.demo503.find().pretty();This will produce the following output −{ "_id" : 1, "Name" : "John", "Subject" : [ "MySQL", "Java", "C" ] } {    "_id" : 2,    "Name" : "David",    "Subject" : [       "MongoDB",       "C++",     ... Read More

MongoDB Query to Find on Field Combination of Firstname and Lastname

AmitDiwan
Updated on 13-May-2020 06:23:42

719 Views

For combination, use $concat and check the equality using $eq. Let us create a collection with documents −> db.demo502.insertOne({"FirstName":"John", "LastName":"Smith"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e875534987b6e0e9d18f56d") } > db.demo502.insertOne({"FirstName":"David", "LastName":"Miller"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e87553e987b6e0e9d18f56e") } > db.demo502.insertOne({"FirstName":"John", "LastName":"Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e875543987b6e0e9d18f56f") }Display all documents from a collection with the help of find() method −> db.demo502.find();This will produce the following output −{ "_id" : ObjectId("5e875534987b6e0e9d18f56d"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5e87553e987b6e0e9d18f56e"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5e875543987b6e0e9d18f56f"), "FirstName" : "John", ... Read More

Advertisements