Remove Array Element from MongoDB Collection

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

216 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

237 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

305 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

Print All Attributes in StackFrame API in Java 9

raja
Updated on 31-Mar-2020 12:41:29

235 Views

StackWalker API is a new feature in Java 9, and it improves the performance of the predecessor stack track element. It can also provide a way to filter the stack elements in case of exception or to understand application behavior. In Java 9, the way to access the stack trace is very limited and provide the entire stack information at once.In the below example, we need to print all attributes in Stack Frame Exampleimport java.lang.StackWalker.StackFrame; import java.util.*; import java.util.stream.*; import java.lang.StackWalker.Option; public class AllAttributesTest {    public static void main(String args[]) {       System.out.println("Java 9 Stack Walker API - Print all ... Read More

Set and Push in Single Update with MongoDB

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

896 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

932 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 with $elemMatch to Fetch Documents

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

324 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

Retrieve All Nested Fields from MongoDB Collection

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

1K+ Views

For this, use aggregate(). Let us create a collection with documents −>db.demo138.insertOne({"Id":101, "PlayerDetails":[{"PlayerName":"Chris", "PlayerScore":400}, {"PlayerName":"David", "PlayerScore":1000}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31bb9ffdf09dd6d08539a1") } >db.demo138.insertOne({"Id":102, "PlayerDetails":[{"PlayerName":"Bob", "PlayerScore":500}, {"PlayerName":"Carol", "PlayerScore":600}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31bbcefdf09dd6d08539a2") }Display all documents from a collection with the help of find() method −> db.demo138.find();This will produce the following output −{    "_id" : ObjectId("5e31bb9ffdf09dd6d08539a1"), "Id" : 101, "PlayerDetails" : [       { "PlayerName" : "Chris", "PlayerScore" : 400 },       { "PlayerName" : "David", "PlayerScore" : 1000 }    ] } {    "_id" : ObjectId("5e31bbcefdf09dd6d08539a2"), ... Read More

Compare Two Fields in Aggregation Filter with MongoDB

AmitDiwan
Updated on 31-Mar-2020 12:26:21

2K+ Views

For this, use aggregate() along with $filter. Let us create a collection with documents −> db.demo137.insertOne( ...    { ...       Name1:"Chris", ...       Name2:"David", ...       Detail1:[ ...          {_id:"John", Name3:"Chris"}, ...          {_id:"Chris", Name3:"Chris"}, ...       ], ...       Detail2:[{_id:"David", Name3:"Chris"}, ...       {_id:"Carol", Name3:"Chris"}, ...       {_id:"John", Name3:"Chris"}] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e31b4cefdf09dd6d08539a0") }Display all documents from a collection with the help of find() method −> db.demo137.find();This will ... Read More

Advertisements