Upsert in MongoDB with Custom ID Values

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

620 Views

You need to use insert() for this. Whenever you insert custom _id values and the document already exist with the custom _id value then an error is visible. Let us first create a collection with documents. Under this, we tried adding the same document again and this resulted in an error> db.customIdDemo.insert({"_id":1, "StudentName":"John"}); WriteResult({ "nInserted" : 1 }) > db.customIdDemo.insert({"_id":1, "StudentName":"Carol"}); WriteResult({    "nInserted" : 0,    "writeError" : {       "code" : 11000,       "errmsg" : "E11000 duplicate key error collection: admin.customIdDemo index: _id_ dup key: { : 1.0 }"    } }) > db.customIdDemo.insert({"_id":2, "StudentName":"Carol"}); ... Read More

C++ Program to Perform Edge Coloring of a Graph

Paul Richard
Updated on 30-Jul-2019 22:30:25

672 Views

In this program, we will perform Edge Coloring of a Graph in which we have to color the edges of the graph that no two adjacent edges have the same color. Steps in Example.AlgorithmBegin    Take the input of the number of vertices, n, and then number of edges, e, in the graph.    The graph is stored as adjacency list.    BFS is implemented using queue and colors are assigned to each edge. EndExample#include using namespace std; int n, e, i, j; vector g; vector color; bool v[111001]; void col(int n) {    queue q;    int c = ... Read More

Get the Count of Documents in a MongoDB Collection

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

247 Views

To get the count of the number of documents in a collection MongoDB, you can use the below syntax −db.getCollectionNames().map(function(anyVariableName) {    return { "yourVariableName": yourVariableName, "count": db[yourVariableName].count() } });Here, we are using ‘test’ database.Let us implement the above syntax to get the count of the number of documents in a MongoDB collection −> db.getCollectionNames().map(function(ColName) { ... return { "ColName": ColName, "TotalDocument": db[ColName].count() } ... });This will produce the following output −[    {       "ColName" : "ConvertStringToDateDemo",       "TotalDocument" : 4    },    {       "ColName" : "Employee_Information",       "TotalDocument" ... Read More

MySQL New User Access Denied Even After Giving Privileges

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

394 Views

After creating a user and giving all privileges to the user, you need to FLUSH PRIVILEGES to set up and want the new settings to work correctly.The syntax is as follows −FLUSH PRIVILEGES;Here is the query to create a new user which has the name ‘Bob’ in my case. The query to create a new user is as follows −mysql> CREATE USER 'Bob'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.56 sec)Now given all privileges to user Bob −mysql> GRANT ALL PRIVILEGES ON *.* TO 'Bob'@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.23 sec)Now flush the privileges. ... Read More

ByteBuffer get() Method in Java

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

492 Views

The value at the current position of the buffer is read and then incremented using the method get() in the class java.nio.ByteBuffer. This method returns the value that is at the current buffer position. Also, the BufferUnderflowException is thrown if underflow situation occurs.A program that demonstrates this is given as follows −Example Live Demoimport java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { ByteBuffer buffer = ByteBuffer.allocate(n); ... Read More

Duration minusSeconds Method in Java

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

166 Views

An immutable copy of a duration where some seconds are removed from it can be obtained using the minusSeconds() method in the Duration class in Java. This method requires a single parameter i.e. the number of seconds to be subtracted and it returns the duration with the subtracted seconds.A program that demonstrates this is given as follows −Example Live Demoimport java.time.Duration; public class Demo { public static void main(String[] args) { Duration d = Duration.ofMinutes(1); System.out.println("The duration is: " + d); ... Read More

Count Two Different Columns in a Single Query in MySQL

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

5K+ Views

You can use CASE statement to count two different columns in a single query. To understand the concept, let us first create a table. The query to create a table is as follows.mysql> create table CountDifferentDemo    - > (    - > ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > ProductName varchar(20),    - > ProductColor varchar(20),    - > ProductDescription varchar(20)    - > ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into CountDifferentDemo(ProductName, ProductColor, ProductDescription) values('Product-1', 'Red', 'Used'); Query OK, 1 row ... Read More

Get Default Phone Network Country ISO in Android

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

579 Views

This example demonstrate about How to get default phone Network Country Iso 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 a text view to show phone country iso.Step 3 − Add the following code to java/MainActivity.xmlpackage com.example.myapplication; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.telephony.TelephonyManager; import android.widget.TextView; import static ... Read More

Instant isSupported Method in Java

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

166 Views

It can be checked if a ChronoUnit is supported by the Instant class or not by using the isSupported() method in the Instant class in Java. This method requires a single parameter i.e. the ChronoUnit to check. It returns true if the ChronoUnit is supported by the Instant class and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.time.*; import java.time.temporal.ChronoUnit; public class Demo {    public static void main(String[] args) {       Instant i = Instant.now();       System.out.println("The current instant is: " + i);       boolean flag = i.isSupported(ChronoUnit.SECONDS); ... Read More

StringJoiner Add Method in Java 8

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

227 Views

The add() method of the StringJoiner class is used in Java 8 to add a copy of the given CharSequence value as the next element of the StringJoiner value. If the new element ele is null, then the value null gets added.The syntax is as follows −public StringJoiner add(CharSequence ele)Here, the parameter ele is the element to be added, whereas, CharSequence is a readable sequence of char values.To work with the StringJoiner in Java 8, import the following package −import java.util.StringJoiner;We will first create a StringJoiner and set the delimeter −StringJoiner strJoin = new StringJoiner(", ")Use the add() method to ... Read More

Advertisements