Rename ID Field After MongoDB Group Aggregation

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

2K+ Views

Yes, it is possible to rename using aggregation. Let us first create a collection with documents> db.renameIdDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a1760353decbc2fc927c5") } > db.renameIdDemo.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a1765353decbc2fc927c6") } > db.renameIdDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a176b353decbc2fc927c7") }Following is the query to display all documents from a collection with the help of find() method> db.renameIdDemo.find();This will produce the following output{ "_id" : ObjectId("5c9a1760353decbc2fc927c5"), "StudentName" : "Chris" } { "_id" : ObjectId("5c9a1765353decbc2fc927c6"), "StudentName" : "Robert" } { "_id" : ObjectId("5c9a176b353decbc2fc927c7"), "StudentName" : "David" }Following is the query to ... Read More

Copy Data from One Field to Another in MySQL

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

2K+ Views

To copy data from one field to another on every row, use the UPDATE command.Let us first create a table −mysql> create table DemoTable    (    StudentId int,    StudentFirstName varchar(20),    StudentMarks int default 0    ); Query OK, 0 rows affected (0.49 sec)Following is the query to insert records in the table using insert command −mysql> insert into DemoTable(StudentId, StudentFirstName) values(89, 'Larry'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentId, StudentFirstName) values(35, 'Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(StudentId, StudentFirstName) values(48, 'Chris'); Query OK, 1 row affected (0.13 sec) ... Read More

Find Connected Components of an Undirected Graph in C++

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

894 Views

Weakly or Strongly Connected for a given a undirected graph can be found out using DFS. This is a C++ program of this problem.Functions usedBegin Function fillorder() = fill stack with all the vertices.    a) Mark the current node as visited and print it    b) Recur for all the vertices adjacent to this vertex    c) All vertices reachable from v are processed by now, push v to Stack End Begin Function DFS() :    a) Mark the current node as visited and print it    b) Recur for all the vertices adjacent to this vertex EndExample#include ... Read More

Setting Column Values as Column Names in MySQL Query Result

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

4K+ Views

To set column values as column names in the query result, you need to use a CASE statement.The syntax is as follows −select yourIdColumnName, max(case when (yourColumnName1='yourValue1') then yourColumnName2 else NULL end) as 'yourValue1', max(case when (yourColumnName1='yourValue2') then yourColumnName2 else NULL end) as 'yourValue2', max(case when yourColumnName1='yourValue3') then yourColumnName2 else NULL end) as 'yourValue3’, . . N from valueAsColumn group by yourIdColumnName order by yourIdColumnName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table valueAsColumn    -> (    -> UserId int,    -> UserColumn1 varchar(10),    -> ... Read More

Duration withNanos Method in Java

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

117 Views

The immutable copy of a duration with the required nanoseconds is obtained using the method withNanos() in the Duration class in Java. This method requires a single parameter i.e. the number of nanoseconds and it returns the duration with the required nanoseconds that were passed as a parameter.A program that demonstrates this is given as follows −Example Live Demoimport java.time.Duration; public class Demo { public static void main(String[] args) { int nanoseconds = 1000000; Duration duration = Duration.ofHours(10); System.out.println("The duration ... Read More

Quartet Class in JavaTuples

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

128 Views

A Quartet class is a Tuple of four elements. It is part of the JavaTuples library.The following is the declaration −public final class Quartet extends Tuple implements IValue0, IValue0, IValue0, IValue0Let us first see what we need to work with JavaTuples. To work with Quartet class in JavaTuples, you need to import the following package −import org.javatuples.Quartet;Note − Steps to download and run JavaTuples program If you are using Eclipse IDE to run Quartet Class in JavaTuples, then Right Click Project → Properties → Java Build Path → Add External Jars and upload the downloaded JavaTuples jar file.The following is ... Read More

Bind Variables in JDBC: How to Execute a Query

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

7K+ Views

A bind variable is an SQL statement with a temporary variable as place holders which are later replaced with appropriate values. For example, if you have a table named employee in the database created as shown below:+---------+--------+----------------+ | Name | Salary | Location | +---------+--------+----------------+ | Amit | 30000 | Hyderabad | | Kalyan | 40000 | Vishakhapatnam | | Renuka | 50000 | Delhi | | Archana | ... Read More

Instant toString Method in Java

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

260 Views

The range of values for a field can be obtained using the range() method in the Instant class in Java. This method requires a single parameter i.e. the ChronoField for which the range of values are required and it returns the range of valid values for the ChronoField.A program that demonstrates this is given as follows −Example Live Demoimport java.time.*; import java.time.temporal.ChronoField; import java.time.temporal.ValueRange; public class Demo { public static void main(String[] args) { Instant i = Instant.now(); ValueRange range1 = i.range(ChronoField.MILLI_OF_SECOND); ... Read More

Instant equals Method in Java

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

1K+ Views

The equality of two Instant objects can be determined using the equals() method in the Instant class in Java. This method requires a single parameter i.e. the Instant to be compared. Also it returns true if both the Instant objects are equal and false otherwise.A program that demonstrates this is given as followsExample Live Demoimport java.time.*; import java.time.temporal.ChronoUnit; public class Demo {    public static void main(String[] args) {       Instant i1 = Instant.parse("2019-01-13T16:10:35.00Z");       Instant i2 = Instant.parse("2019-01-13T16:10:35.00Z");       System.out.println("Instant object i1 is: " + i1);       System.out.println("Instant object i2 is: " ... Read More

DoubleStream SummaryStatistics Method in Java

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

145 Views

The summaryStatistics() method of the DoubleStream class returns a DoubleSummaryStatistics describing various summary data about the elements of this stream. This is a special case of a reduction.The syntax is as followsDoubleSummaryStatistics summaryStatistics()Here, DoubleSummaryStatistics is a state object for collecting statistics such as count, min, max, average, etc. It works with streams. To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;First, create a DoubleStream and add some elementsDoubleStream doubleStream = DoubleStream.of(20.5, 35.8, 45.9, 50.8, 80.7);Now, get the statisticsDoubleSummaryStatistics details = doubleStream.summaryStatistics();The following is an example to implement DoubleStream summaryStatistics() methodExample Live Demoimport java.util.stream.DoubleStream; import java.util.DoubleSummaryStatistics; public class Demo ... Read More

Advertisements