Is it possible to sum two fields in MongoDB using the Aggregation framework?

Chandu yadav
Updated on 30-Jul-2019 22:30:25
Yes, it is possible using the $project operator. Let us first create a collection with documents> db.sumTwoFieldsDemo.insertOne({"FirstValue":150, "SecondValue":350}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b4bfe15e86fd1496b38cd") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":450, "SecondValue":1550}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b4c1215e86fd1496b38ce") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":2560, "SecondValue":2440}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b4c2715e86fd1496b38cf") }Following is the query to display all documents from a collection with the help of find() method> db.sumTwoFieldsDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9b4bfe15e86fd1496b38cd"),    "FirstValue" : 150,    "SecondValue" : 350 } {    "_id" : ObjectId("5c9b4c1215e86fd1496b38ce"),    "FirstValue" : 450,    "SecondValue" ... Read More

Java Program to generate custom random number -1 or 1

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25
To generate custom random number 1 or -1, you need to use nextBoolean(). At first take a loop and create a Random object on each iteration −for (int i = 0; i < 5; i++) {    Random rand = new Random(); }Now, use nextBoolean() to generate 1 on TRUE condition, ekse -1 −for (int i = 0; i < 5; i++) {    Random rand = new Random();    if (rand.nextBoolean())       System.out.println(1);    else       System.out.println(-1); }Example Live Demoimport java.util.Random; public class Demo {    public static void main(String[] args) {       for ... Read More

Benefits of C over other languages\

Smita Kapse
Updated on 30-Jul-2019 22:30:25
The C programming language was developed by Dennis Ritchie during early 1970. It was developed to redesign UNIX operating system.Earlier the B language, which was used for UNIX system, it has different drawbacks. It does not support structures, and did not understand datatypes. For this reason, the C language was introduced. C has high level functionality, and detailed feature for OS programming. The UNIX kernel was developed by using C.Advantages of C languageC is medium level language. It has both, the lower level and higher level functionality. We can use C to make driver or kernel level programs as well ... Read More

Dynamic_cast and static_cast in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25
static_cast: This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. This can cast related type classes. If the types are not same it will generate some error.Example#include using namespace std; class Base {}; class Derived : public Base {}; class MyClass {}; main(){    Derived* d = new Derived;    Base* b = static_cast(d); // this line will work properly    MyClass* x = static_cast(d); // ERROR will be generated ... Read More

What are some of the best love conversation?

Sashi K
Updated on 30-Jul-2019 22:30:25
Love is eternal. It is not physical, but the union of two hearts and souls. We read so many young boys and girl love stories, some of them which lead to a happy ending and some end up as great tragedies.The present generation cannot believe that there will be great love stories even in arranged marriages. In fact in arranged marriages, there will be more love. That is because they have more bondage with years of togetherness and sailing through life's ups and downs without leaving each other's hands.Once I happened to overhear this cute conversation of my grandparents who ... Read More

BCD numbers in 8085 Microprocessor

George John
Updated on 30-Jul-2019 22:30:25
Many a time, we are required to represent decimal numbers in a computer, and perform arithmetic on these numbers. For example, we may be required to total the marks a student has obtained in five different subjects, where obviously, the marks are awarded in decimal notation.For this purpose, the BCD code is extensively used. In BCD notation, 4 bits are used to code a digit, and so two digits of information are stored in a Byte. For example, decimal 45 is represented in BCD as of 0100 0101. Codes 10 to 15 i.e. 1010, 1011, 1100, 1101, 1110, and 1111 ... Read More

How to get phone number in android?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25
This example demonstrate about How to get phone number 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 text view. When user open application, Phone number going to append on textview.Step 3 − Add the following code to src/ com.example.andy.myapplication; import android.Manifest; import android.content.Context; import; import android.os.Build; import android.os.Bundle; import; import; import; import android.telephony.TelephonyManager; import android.widget.TextView; import ... Read More

How to drop a database in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25
To drop a database in MongoDB, you need to use dropDatabase() method. The syntax is as follows:db.dropDatabase()The above syntax will delete the present working database. If you haven’t selected any database then it will delete the default database.Before deleting the database, first list all the databases with the help of show command. The query is as follows:> show dbs;The following is the output:admin 0.000GB config 0.000GB local 0.000GB sample 0.000GBHere, let us delete the database ‘sample’. To delete the database ‘sample’, you need to switch the database with the help of use command. The syntax is as follows:use yourDatabaseName;The query ... Read More

How to apply if tag in JSP?

Samual Sam
Updated on 30-Jul-2019 22:30:25
The tag evaluates an expression and displays its body content only if the expression evaluates to true.AttributeThe tag has the following attributes −AttributeDescriptionRequiredDefaulttestCondition to evaluateYesNonevarName of the variable to store the condition's resultNoNonescopeScope of the variable to store the condition's resultNopageExample Tag Example My salary is: The above code will generate the following result −My salary is: 4000

LocalDate now() Method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25
The current date can be obtained from the system clock in the default time zone using the now() method in the LocalDate class in Java. This method requires no parameters and it returns the current date from the system clock in the default time zoneA program that demonstrates this is given as follows −Example Live Demoimport java.time.*; public class Demo { public static void main(String[] args) { LocalDate ld =; System.out.println("The LocalDate is: " + ld); } }OutputThe LocalDate is: 2019-02-15Now let ... Read More