Java 9 has provided an efficient way of stack walking for lazy access, filtering stack trace using StackWalker API. An object of StackWalker can allow us to traverse and access to stacks. This class contains some useful methods like walk(), forEach(), and getCallerClass().The getCallerClass() method returns the class that invokes the method that calls this method. To get hold of calling class instance, we need RETAIN_CLASS_REFERENCE while getting StackWalker instance. RETAIN_CLASS_REFERENCE retains an instance of all classes walked by StackWalker.Syntaxpublic Class getCallerClass()Exampleimport java.lang.StackWalker.Option; public class StackWalkerTest { public static void main(String args[]) { StackWalkerTest1.test1(); ... Read More
To append an entry in MongoDB, use $push. Let us create a collection with documents −> db.demo253.insertOne( ... { ... _id: "101", ... isActive: false, ... details: [ ... { ... Name: "Chris", ... }, ... { ... CountryName:"US" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() ... Read More
To sort array, use $sort. For projection, use $project in MongoBD aggregate(). Let us create a collection with documents −> db.demo252.insertOne( ... {"Values" : [ { "v1" : 20, "v2" :30 }, { "v1" : 20, "v2" : 20 }, { "v1" : 10, "v2" : 7 } ] } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e46c2761627c0c63e7dba78") }Display all documents from a collection with the help of find() method −> db.demo252.find();This will produce the following output −{ "_id" : ObjectId("5e46c2761627c0c63e7dba78"), "Values" : [ { "v1" : 20, "v2" : 30 }, { "v1" : 20, ... Read More
For min/max in MongoDB, use $min and $max. Let us create a collection with documents −> db.demo251.insertOne({"Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0001627c0c63e7dba74") } > db.demo251.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0031627c0c63e7dba75") } > db.demo251.insertOne({"Marks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0061627c0c63e7dba76") } > db.demo251.insertOne({"Marks":76}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c00c1627c0c63e7dba77") }Display all documents from a collection with the help of find() method −> db.demo251.find();This will produce the following output −{ "_id" : ObjectId("5e46c0001627c0c63e7dba74"), "Marks" : 78 } { "_id" : ObjectId("5e46c0031627c0c63e7dba75"), "Marks" : 87 } { "_id" : ... Read More
For this, use bsonsize() in MongoDB. Let us create a collection with documents −> db.demo250.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd501627c0c63e7dba70") } > db.demo250.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd531627c0c63e7dba71") } > db.demo250.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd561627c0c63e7dba72") } > db.demo250.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46bd5b1627c0c63e7dba73") }Display all documents from a collection with the help of find() method −> db.demo250.find();This will produce the following output −{ "_id" : ObjectId("5e46bd501627c0c63e7dba70"), "Name" : "Chris" } { "_id" : ObjectId("5e46bd531627c0c63e7dba71"), "Name" : "Bob" } { "_id" : ObjectId("5e46bd561627c0c63e7dba72"), "Name" ... Read More
To query by type, use subtype() in MongoDB. Let us create a collection with documents −> db.demo249.insertOne({ "_id" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }); { "acknowledged" : true, "insertedId" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") } > db.demo249.insertOne({"_id" : BinData(4, "CNDF66qIlCY92q1vFAAAAQ==")}); { "acknowledged" : true, "insertedId" : UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001") } > db.demo249.insertOne({"_id" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==")}); { "acknowledged" : true, "insertedId" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==") }Display all documents from a collection with the help of find() method −> db.demo249.find();This will produce the following output −{ "_id" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") } { "_id" : UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001") } { "_id" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==") }Following is ... Read More
For aggregate in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo248.insertOne({"id":101, "Name":"Chris", "Age":21, "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6651627c0c63e7dba6d") } > db.demo248.insertOne({"id":101, "Name":"Bob", "Age":22, "CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6741627c0c63e7dba6e") } > db.demo248.insertOne({"id":102, "Name":"Mike", "Age":20, "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6811627c0c63e7dba6f") }Display all documents from a collection with the help of find() method −> db.demo248.find();This will produce the following output −{ "_id" : ObjectId("5e46b6651627c0c63e7dba6d"), "id" : 101, "Name" : "Chris", "Age" : 21, "CountryName" : "US" } { "_id" : ObjectId("5e46b6741627c0c63e7dba6e"), "id" : 101, ... Read More
To find documents that contain specific field, use $exists. Let us create a collection with documents −> db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"Chris", "ClientAge":34}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2a31627c0c63e7dba69") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"John", "ClientLastName":"Smith", "ClientAge":31}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2be1627c0c63e7dba6a") } > db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David", "ClientAge":33}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2cd1627c0c63e7dba6b") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David", "ClientLastName":"Miller", "ClientAge":31}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2de1627c0c63e7dba6c") }Display all documents from a collection with the help of find() method −> db.demo247.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e46b2a31627c0c63e7dba69"), "ClientDetails" : [ { ... Read More
To skip a specific number of documents, use skip() along with limit. Let us create a collection with documents −> db.demo246.insertOne({"StudentFirstName":"Chris", "StudentLastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0d71627c0c63e7dba65") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0e21627c0c63e7dba66") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0ea1627c0c63e7dba67") } > db.demo246.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0f91627c0c63e7dba68") }Display all documents from a collection with the help of find() method −> db.demo246.find();This will produce the following output −{ "_id" : ObjectId("5e46b0d71627c0c63e7dba65"), "StudentFirstName" : "Chris", "StudentLastName" : "Brown" } ... Read More
To sort by subdocument, use $sort in MongoDB. Let us create a collection with documents −> db.demo245.insertOne( ... { ... "_id": 101, ... "deatils": [ ... { "DueDate": new ISODate("2019-01-10"), "Value": 45}, ... {"DueDate": new ISODate("2019-11-10"), "Value": 34 } ... ] ... } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo245.insertOne( ... { ... "_id": 102, ... "details": [ ... { "DueDate": new ISODate("2019-12-11"), "Value": 29}, ... {"DueDate": ... Read More