Found 6705 Articles for Database

MongoDB GroupBy to set status

AmitDiwan
Updated on 31-Mar-2020 12:55:01

129 Views

For this, you can use aggregate() in MongoDB. Let us create a collection with documents −> db.demo149.insertOne({"Status":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350386fdf09dd6d08539c4") } > db.demo149.insertOne({"Status":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350388fdf09dd6d08539c5") } > db.demo149.insertOne({"Status":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e35038afdf09dd6d08539c6") }Display all documents from a collection with the help of find() method −> db.demo149.find();This will produce the following output −{ "_id" : ObjectId("5e350386fdf09dd6d08539c4"), "Status" : 40 } { "_id" : ObjectId("5e350388fdf09dd6d08539c5"), "Status" : 40 } { "_id" : ObjectId("5e35038afdf09dd6d08539c6"), "Status" : 50 }Here is the query to MongoDB group by ... Read More

Find records in MongoDB that does NOT match a condition?

AmitDiwan
Updated on 31-Mar-2020 12:51:00

678 Views

To find records that does not match a condition, use $ne. Let us create a collection with documents −> db.demo148.insertOne({"Message":"Hello"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fb37fdf09dd6d08539c0") } > db.demo148.insertOne({"Message":"Good"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fb3efdf09dd6d08539c1") } > db.demo148.insertOne({"Message":"Bye"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fb42fdf09dd6d08539c2") }Display all documents from a collection with the help of find() method −> db.demo148.find();This will produce the following output −{ "_id" : ObjectId("5e32fb37fdf09dd6d08539c0"), "Message" : "Hello" } { "_id" : ObjectId("5e32fb3efdf09dd6d08539c1"), "Message" : "Good" } { "_id" : ObjectId("5e32fb42fdf09dd6d08539c2"), "Message" : "Bye" }Following is the query to ... Read More

How to query a document in MongoDB comparing fields from an array?

AmitDiwan
Updated on 31-Mar-2020 12:49:06

357 Views

To compare fields from an array, use $gt and $lt. Let us create a collection with documents −> db.demo147.insertOne({"Details":[{"Score":45}, {"Score":46}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fa21fdf09dd6d08539be") } > db.demo147.insertOne({"Details":[{"Score":65}, {"Score":86}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32fa40fdf09dd6d08539bf") }Display all documents from a collection with the help of find() method −> db.demo147.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e32fa21fdf09dd6d08539be"),    "Details" : [       {          "Score" : 45       },       {          "Score" : 46       } ... Read More

Removing an array element from a MongoDB collection

AmitDiwan
Updated on 31-Mar-2020 12:47:11

194 Views

To remove an array element, simply use $pull along with update(). Let us create a collection with documents −> db.demo146.insertOne({"ListOfEmployeeNames":["Chris", "David", "Bob", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f54ffdf09dd6d08539bd") }Display all documents from a collection with the help of find() method −> db.demo146.find();This will produce the following output −{ "_id" : ObjectId("5e32f54ffdf09dd6d08539bd"), "ListOfEmployeeNames" : [ "Chris", "David", "Bob", "Mike" ] }Following is the query to remove an array element from MongoDB −> db.demo146.update({}, { "$pull": { "ListOfEmployeeNames": "Bob" } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with ... Read More

Alternative of MongoDB operator $eq to get similar result

AmitDiwan
Updated on 31-Mar-2020 12:45:12

220 Views

For writing an equality, you can simply use find() along with match value. Let us create a collection with documents −> db.demo145.insertOne({"ListOfNames":["Chris", "David", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f37bfdf09dd6d08539bb") } > db.demo145.insertOne({"ListOfNames":["Bob", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f384fdf09dd6d08539bc") }Display all documents from a collection with the help of find() method −> db.demo145.find();This will produce the following output −{ "_id" : ObjectId("5e32f37bfdf09dd6d08539bb"), "ListOfNames" : [ "Chris", "David", "Mike" ] } { "_id" : ObjectId("5e32f384fdf09dd6d08539bc"), "ListOfNames" : [ "Bob", "John" ] }Following is the query to implement MongoDB operator $eq −> db.demo145.find({"ListOfNames":"John"});This will produce ... Read More

Find result within array of objects and match email address field in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 12:43:45

288 Views

Let us first create a collection with documents −>db.demo144.insertOne({"EmployeeDetails":[{"EmployeeName":"Chris", "EmployeeEmail":"Chris12@gmail.com"}, {"EmployeeName":"Bob", "EmployeeEmail":"bo22@gmail.com"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f1d8fdf09dd6d08539b9") } >db.demo144.insertOne({"EmployeeDetails":[{"EmployeeName":"David", "EmployeeEmail":"david@gmail.com"}, {"EmployeeName":"Carol", "EmployeeEmail":"Carol@gmail.com"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32f1f5fdf09dd6d08539ba") }Display all documents from a collection with the help of find() method −> db.demo144.find();This will produce the following output −{    "_id" : ObjectId("5e32f1d8fdf09dd6d08539b9"), "EmployeeDetails" : [       { "EmployeeName" : "Chris", "EmployeeEmail" : "Chris12@gmail.com" },       { "EmployeeName" : "Bob", "EmployeeEmail" : "bo22@gmail.com" }    ] } {    "_id" : ObjectId("5e32f1f5fdf09dd6d08539ba"), "EmployeeDetails" : [       { "EmployeeName" ... Read More

How do I $set and $push in single update with MongoDB?

AmitDiwan
Updated on 31-Mar-2020 12:41:11

873 Views

For this, simply use update() to update. Let us create a collection with documents −> db.dem0143.insertOne({"StudentId":1, "Details":{"Name":"Chris"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32eb9efdf09dd6d08539b7") } > db.dem0143.insertOne({"StudentId":2, "Details":{"Name":"David"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32eba5fdf09dd6d08539b8") }Display all documents from a collection with the help of find() method −> db.dem0143.find();This will produce the following output −{ "_id" : ObjectId("5e32eb9efdf09dd6d08539b7"), "StudentId" : 1, "Details" : { "Name" : "Chris" } } { "_id" : ObjectId("5e32eba5fdf09dd6d08539b8"), "StudentId" : 2, "Details" : { "Name" : "David" } }Following is the query to implement $set and $push in a single update ... Read More

Push query results into variable with MongoDB?

AmitDiwan
Updated on 31-Mar-2020 12:39:38

904 Views

For this, you can use aggregate(). Let us create a collection with documents −> db.demo142.insertOne({"Value":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9c6fdf09dd6d08539b2") } > db.demo142.insertOne({"Value":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9cafdf09dd6d08539b3") } > db.demo142.insertOne({"Value":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9cdfdf09dd6d08539b4") } > db.demo142.insertOne({"Value":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9d0fdf09dd6d08539b5") } > db.demo142.insertOne({"Value":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e32e9d9fdf09dd6d08539b6") }Display all documents from a collection with the help of find() method −> db.demo142.find();This will produce the following output −{ "_id" : ObjectId("5e32e9c6fdf09dd6d08539b2"), "Value" : 50 } { ... Read More

MongoDB query to find last object in collection?

AmitDiwan
Updated on 31-Mar-2020 12:37:36

6K+ Views

To find last object in collection, at first sort() to sort the values. Use limit() to get number of values i.e. if you want only the last object, then use limit(1).Let us first create a collection with documents −> db.demo141.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c347fdf09dd6d08539ae") } > db.demo141.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c34bfdf09dd6d08539af") } > db.demo141.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c34ffdf09dd6d08539b0") } > db.demo141.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c352fdf09dd6d08539b1") }Display all documents from a collection with the help of find() method −> db.demo141.find();This will ... Read More

MongoDB: $nin and $in not working together in $elemMatch to fetch documents having subjects “MongoDB”, but not “Java”

AmitDiwan
Updated on 31-Mar-2020 12:34:42

295 Views

For such kind of fetching, use only $nin and $in. Let us create a collection with documents −> db.demo140.insertOne({"Id":101, "Subjects":["MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c149fdf09dd6d08539a9") } > db.demo140.insertOne({"Id":102, "Subjects":["MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c14cfdf09dd6d08539aa") } > db.demo140.insertOne({"Id":103, "Subjects":["MongoDB", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c157fdf09dd6d08539ab") } > db.demo140.insertOne({"Id":104, "Subjects":["MongoDB", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c163fdf09dd6d08539ac") } > db.demo140.insertOne({"Id":105, "Subjects":["C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31c16ffdf09dd6d08539ad") }Display all documents from a collection with the help of find() method −> ... Read More

Advertisements