Update Two Separate Arrays in a Document with One Update Call in MongoDB

George John
Updated on 30-Jul-2019 22:30:25

151 Views

You can use $push operator for this. Let us first create a collection with documents>db.twoSeparateArraysDemo.insertOne({"StudentName":"Larry", "StudentFirstGameScore":[98], "StudentSecondGameScore":[77]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b152815e86fd1496b38b8") } >db.twoSeparateArraysDemo.insertOne({"StudentName":"Mike", "StudentFirstGameScore":[58], "StudentSecondGameScore":[78]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b152d15e86fd1496b38b9") } >db.twoSeparateArraysDemo.insertOne({"StudentName":"David", "StudentFirstGameScore":[65], "StudentSecondGameScore":[67]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b153315e86fd1496b38ba") }Following is the query to display all documents from a collection with the help of find() method> db.twoSeparateArraysDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9b152815e86fd1496b38b8"),    "StudentName" : "Larry",    "StudentFirstGameScore" : [       98    ],    "StudentSecondGameScore" : [       ... Read More

Sort List in Descending Order Using Comparator in Java

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

697 Views

Let us first create an ArrayList −ArrayListarrList = new ArrayList(); arrList.add(10); arrList.add(50); arrList.add(100); arrList.add(150); arrList.add(250);Use Comparators interface to order in reverse order with reverseOrder() −Comparator comparator = Collections.reverseOrder(); Now, sort with Collections: Collections.sort(arrList, comparator);Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add(10);       arrList.add(50);       arrList.add(100);       arrList.add(150);       arrList.add(250);       arrList.add(100);       arrList.add(150);       arrList.add(250);       Comparator comparator = Collections.reverseOrder();       ... Read More

C++ Program to Perform Baillie-PSW Primality Test

Smita Kapse
Updated on 30-Jul-2019 22:30:25

597 Views

The Baillie-PSW Primality Test, this test named after Robert Baillie, Carl Pomerance, John Selfridge, and Samuel Wagstaff. It is a test which tests whether a number is a composite number or possibly prime.AlgorithmMillerTest()Begin    Declare a function MillerTest of Boolean type.    Declare MT_dt and MT_num of integer datatype and pass as the parameter.    Declare MT_a and MT_x of integer datatype.       Initialize MT_a = 2 + rand( ) % (MT_num - 4).       Initialize MT_x = pow(MT_a, MT_dt, MT_num).    if (MT_x == 1 || MT_x == MT_num - 1) then       ... Read More

Select All Rows Except Last One in MySQL

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

2K+ Views

You need to use != operator along with subquery. The syntax is as follows −select *from yourTableName where yourIdColumnName != (select max(yourIdColumnName) from yourTableName );To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table AllRecordsExceptLastOne    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> UserName varchar(10),    -> UserAge int    -> ,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.65 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> ... Read More

Create Pair Tuple from List in Java

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

945 Views

Use the fromCollection() method to create a Pair Tuple from List collection.Let us first see what we need to work with JavaTuples. To work with Pair class in JavaTuples, you need to import the following package −import org.javatuples.Pair;Note − Steps to download and run JavaTuples program. If you are using Eclipse IDE to run Pair Class in JavaTuples, then Right Click Project ->Properties ->Java Build Path ->Add External Jars and upload the downloaded JavaTuples jar file.The following is an example −Exampleimport org.javatuples.Pair; import java.util.*; public class Demo {    public static void main(String[] args) {       List < ... Read More

Create Unique Index in Android SQLite

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

616 Views

Before getting into example, we should know what sqlite data base in android is. SQLite is an open source SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC, ODBC etc.This example demonstrate about How to create unique index in Android sqliteStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to ... Read More

Write JDBC Application to Connect to Multiple Databases Simultaneously

Nancy Den
Updated on 30-Jul-2019 22:30:25

6K+ Views

To connect with a database, you need toRegister the Driver: Select the required database, register the Driver class of the particular database using the registerDriver() method of the DriverManager class or, the forName() method of the class named Class.DriverManager.registerDriver(new com.mysql.jdbc.Driver());Get connection: Create a connection object by passing the URL of the database, username and password (of a user in the database) in string format, as parameters to the getConnection() method of the DriverManager class.Connection mysqlCon = DriverManager.getConnection(mysqlUrl, "root", "password");To connect to multiple databases in a single JDBC program you need to connect to the two (or more) databases simultaneously using ... Read More

Set Result of a Java Expression in a Property in JSP

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

168 Views

The tag is JSTL-friendly version of the setProperty action. The tag is helpful because it evaluates an expression and uses the results to set a value of a JavaBean or a java.util.Map object.AttributeThe tag has the following attributes −AttributeDescriptionRequiredDefaultValueInformation to saveNobodytargetName of the variable whose property should be modifiedNoNonepropertyProperty to modifyNoNonevarName of the variable to store informationNoNonescopeScope of variable to store informationNoPageIf target is specified, property must also be specified.Example Tag Example The above code will generate the following result −4000

Period.ofDays Method in Java

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

328 Views

The Period can be obtained with the given number of days using the ofDays() method in the Period class in Java. This method requires a single parameter i.e. the number of days and it returns the Period object with the given number of days.A program that demonstrates this is given as follows −Example Live Demoimport java.time.Period; public class Demo { public static void main(String[] args) { int days = 5; Period p = Period.ofDays(days); System.out.println("The Period is: " + p); ... Read More

Period plusDays Method in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

175 Views

An immutable copy of the Period object where some days are added to it can be obtained using the plusDays() method in the Period class in Java. This method requires a single parameter i.e. the number of days to be added and it returns the Period object with the added days.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 = "P5Y7M15D";       Period p1 = Period.parse(period);       System.out.println("The Period is: " + p1);       Period p2 ... Read More

Advertisements