Query Top N Rows in MongoDB

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

3K+ Views

To query on top N rows in MongoDB, you can use aggregate framework. Let us create a collection with documents> db.topNRowsDemo.insertOne({"StudentName":"Larry", "Score":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26eee6304881c5ce84b91") } > db.topNRowsDemo.insertOne({"StudentName":"Chris", "Score":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26ef66304881c5ce84b92") } > db.topNRowsDemo.insertOne({"StudentName":"Mike", "Score":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26efe6304881c5ce84b93") } > db.topNRowsDemo.insertOne({"StudentName":"Adam", "Score":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26f066304881c5ce84b94") } > db.topNRowsDemo.insertOne({"StudentName":"John", "Score":86}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26f0f6304881c5ce84b95") }Following is the query to display all documents from a collection with the help of find() method> ... Read More

Generate Large Random Numbers in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

686 Views

For large random numbers, use BigInteger type in Java. At first, create a Random object −Random randNum = new Random();Now, declare a byte array and generate random bytes −byte[] b = new byte[max]; randNum.nextBytes(b);Now, generate a large random number with BigInteger type −BigInteger bigInt = new BigInteger(b);Example Live Demoimport java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String... a) {       int max = 10;       Random randNum = new Random();       byte[] b = new byte[max];       randNum.nextBytes(b);       // BigInteger type       BigInteger bigInt ... Read More

Get Today's Date in YYYY-MM-DD Format in MySQL

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

3K+ Views

To get today’s date in (YYYY-MM-DD) format in MySQL, you can use CURDATE().Following is the query to get the current date:mysql> SELECT CURDATE();This will produce the following output:+------------+ | CURDATE() | +------------+ | 2019-04-09 | +------------+ 1 row in set (0.00 sec)You can also use NOW() for this. Following is the query:mysql> SELECT DATE(NOW());This will produce the following output+-------------+ | DATE(NOW()) | +-------------+ | 2019-04-09 | +-------------+ 1 row in set (0.00 sec)

LinkedBlockingDeque in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

351 Views

The LinkedBlockingDeque Class in Java has a blockingdeque that is optionally bounded and based on linked nodes. This class implements the Collection interface as well as the AbstractQueue class. It is a part of the Java Collection Framework.A program that demonstrates this is given as follows −Example Live Demoimport java.util.concurrent.LinkedBlockingDeque; public class Demo {    public static void main(String[] args) {       LinkedBlockingDeque lbDeque = new LinkedBlockingDeque();       lbDeque.add("James");       lbDeque.add("May");       lbDeque.add("John");       lbDeque.add("Sara");       lbDeque.add("Anne");       System.out.println("Size of LinkedBlockingDeque is: " + lbDeque.size());     ... Read More

Java Signature getAlgorithm Method

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

219 Views

The name of the algorithm for the signature object can be obtained using the method getAlgorithm() in the class java.security.Signature. This method requires no parameters and it returns the name of the algorithm for the signature object.A program that demonstrates this is given as follows −Example Live Demoimport java.security.*; import java.util.*; public class Demo {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          String algorithm = signature.getAlgorithm();          System.out.println("The Algorithm is: " + algorithm);       } catch (NoSuchAlgorithmException e) { ... Read More

Check if ArrayList is Empty for ListView in Android

Nitya Raut
Updated on 30-Jul-2019 22:30:25

420 Views

This example demonstrates How to check arraylist is empty for Listview in Android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.               In the above code, we have taken name and record number as Edit text, when user click on save button it will store the data into ... Read More

Get Android Device Brand Name Programmatically

Nitya Raut
Updated on 30-Jul-2019 22:30:25

1K+ Views

This example demonstrate about How to get programmatically android device brand name.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show device brand name .Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.Manifest; import android.app.ProgressDialog; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.webkit.CookieManager; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; ... Read More

Execute SQL SELECT Statement in JSP

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

3K+ Views

The tag executes an SQL SELECT statement and saves the result in a scoped variable.AttributeThe tag has the following attributes −AttributeDescriptionRequiredDefaultsqlSQL command to execute (should return a ResultSet)NoBodydataSourceDatabase connection to use (overrides the default)NoDefault databasemaxRowsMaximum number of results to store in the variableNoUnlimitedstartRowNumber of the row in the result at which to start recordingNo0varName of the variable to represent the databaseNoSet defaultscopeScope of variable to expose the result from the databaseNoPageExampleTo start with the basic concept, let us create an Employees table in the TEST database and create few records in that table as follows −Follow these steps ... Read More

Period multipliedBy Method in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

91 Views

An immutable copy of a Period where all the Period elements are multiplied by a value can be obtained using the method multipliedBy() in the Period class in Java. This method requires a single parameter i.e. the value which is to be multiplied and it returns the immutable copy of the Period which is multiplied by a value.A program that demonstrates this is given as followsExample Live Demoimport java.time.Period; public class Demo {    public static void main(String[] args) {       String period = "P5Y9M4D";       Period p = Period.parse(period);       System.out.println("The Period is: " ... Read More

Use MySQL CONCAT and LOWER Effectively

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

The contact() method is used to concatenate. However, lower() is used to change the case to lowercase. For our example, let us create a table.The query to create a table is as followsmysql> create table concatAndLowerDemo    -> (    -> FirstValue varchar(10),    -> SecondValue varchar(10),    -> ThirdValue varchar(10),    -> FourthValue varchar(10)    -> ); Query OK, 0 rows affected (0.55 sec)Now you can insert some records in the table using insert command.The query is as followsmysql> insert into concatAndLowerDemo values('John', '12345', 'Java', 'MySQL'); Query OK, 1 row affected (0.21 sec) mysql> insert into concatAndLowerDemo values('Hi', '12345', ... Read More

Advertisements