Found 6702 Articles for Database

Add data to existing data in a MySQL Database?

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

677 Views

You can use CONCAT() function for this. Let us first create a table −mysql> create table DemoTable    (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(100)    ); Query OK, 0 rows affected (0.43 sec)Insert records in the table using insert command −mysql> insert into DemoTable(UserName) values('John'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(UserName) values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserName) values('Robert'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+----------+ ... Read More

Is there a MAX function for rows and not for columns in MySQL?

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

87 Views

Yes, you can use GREATEST() from MySQL to check maximum from rows (not columns). Let us first create a table −mysql> create table DemoTable    (    Value1 int,    Value2 int,    Value3 int    ); Query OK, 0 rows affected (0.58 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(190, 395, 322); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output−+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 190 | 395 ... Read More

Find size of text stored in a specific MySQL column?

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

209 Views

You can use length() from MySQL to find the size of text stores in a specific column. Let us first create a tablemysql> create table DemoTable    (    CustomerName longtext    ); Query OK, 0 rows affected (0.67 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output−+--------------+ | CustomerName | +--------------+ | Robert       | +--------------+ 1 row in set (0.00 sec)Here is the query to find the size ... Read More

MySQL query to select ENUM('M', 'F') as 'Male' or 'Female'?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

1K+ Views

You can use IF() for this. Let us first create a table. One of the columns here is having ENUM typemysql> create table DemoTable    (    UserId int,    UserName varchar(40),    UserGender ENUM('M', 'F')    ); Query OK, 0 rows affected (1.11 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(1, 'John', 'M'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(2, 'Maria', 'F'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(3, 'David', 'M'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(4, 'Emma', ... Read More

How to select objects where an array contains only a specific field in MongoDB?

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

240 Views

Let us first create a collection with documents −> db.arrayContainOnlySpecificFieldDemo.insertOne( ...    { ...       "StudentName":"John", ...       "StudentAge":21, ...       "StudentTechnicalSubject":["C", "Java", "MongoDB"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc4921dac184d684e3fa26a") } > db.arrayContainOnlySpecificFieldDemo.insertOne( { "StudentName":"Carol", "StudentAge":23, "StudentTechnicalSubject":["MongoDB"] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc49237ac184d684e3fa26b") }Display all documents from a collection with the help of find() method. The query is as follows −> db.arrayContainOnlySpecificFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc4921dac184d684e3fa26a"),    "StudentName" : "John",    "StudentAge" : 21,   ... Read More

Get attribute list from MongoDB object?

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

927 Views

To get attribute list from MongoDB object, you can use for loop to extract key and value for document. Let us create a collection with documents −>db.getAttributeListDemo.insertOne({"StudentId":101, "StudentName":"John", "StudentAdmissi onDate":new ISODate('2019-01-12'), "StudentSUbjects":["MongoDB", "Java", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdfcc9ac184d684e3fa269") }Display all documents from a collection with the help of find() method −> db.getAttributeListDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbdfcc9ac184d684e3fa269"),    "StudentId" : 101,    "StudentName" : "John",    "StudentAdmissionDate" : ISODate("2019-01-12T00:00:00Z"),    "StudentSUbjects" : [       "MongoDB",       "Java",       "MySQL"    ] }Following is the ... Read More

Check for Existing Document in MongoDB?

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

87 Views

You can use findOne() for this. Following is the syntax −db.yourCollectionName.findOne({yourFieldName: 'yourValue'});Let us create a collection with documents −> db.checkExistingDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf90dac184d684e3fa265") } > db.checkExistingDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf912ac184d684e3fa266") } > db.checkExistingDemo.insertOne({"StudentName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf916ac184d684e3fa267") } > db.checkExistingDemo.insertOne({"StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf91bac184d684e3fa268") }Display all documents from a collection with the help of find() method −> db.checkExistingDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdf90dac184d684e3fa265"), "StudentName" : "John" } { "_id" : ObjectId("5cbdf912ac184d684e3fa266"), "StudentName" : "Carol" } ... Read More

Does Mongo shell treats numbers as float by default.? How can we work it around explicitly?

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

191 Views

Yes, Mongo shell treats numbers as float by default. To work it as int or any other type, you need to mention explicitly. You can use NumberInt() for this. The syntax is as follows −var anyVariableName= [NumberInt("yourValue1"), NumberInt("yourValue2"), .....N];Let us implement the above syntax in order to treat numbers as integer only (not float) −> var integerArrayDemo = [NumberInt("50"), NumberInt("60"),    NumberInt("70"), NumberInt("90"), NumberInt("40")];Following is the query to display the array value −> printjson(integerArrayDemo);This will produce the following output −[    NumberInt(50),    NumberInt(60),    NumberInt(70),    NumberInt(90),    NumberInt(40) ]To display the array value, you can use print() −> ... Read More

Convert NumberLong to String in MongoDB?

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

1K+ Views

In order to convert NumberLong to String in MongoDB, you can use toString() −NumberLong('yourValue').valueOf().toString();To convert NumberLong to String, you can use valueOf() as well −NumberLong('yourValue').valueOf();Let us implement both the above syntaxes.Following is the query to convert NumberLong to String with toString() −> NumberLong('1000').valueOf().toString();This will produce the following output −1000Following is the query to convert NumberLong to Number with valueOf()> NumberLong('1000').valueOf();This will produce the following output −1000

Get output of MongoDB shell script?

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

701 Views

You can use printjson() or print() to get output of MongoDB shell script. Let us create an array of objects.Following is the query to create an array of objects.> var studentDetails=[{"StudentName":"John","StudentAge":21},    {"StudentName":"Carol","StudentAge":24},{"StudentName":"David","StudentAge":25}];Following is the query to get the output of Mongo shell script using printjson() −> printjson(studentDetails);This will produce the following output −[    {       "StudentName" : "John",       "StudentAge" : 21    },    {       "StudentName" : "Carol",       "StudentAge" : 24    },    {       "StudentName" : "David",       "StudentAge" : 25    } ] > var studentDetails=[{"StudentName":"John","StudentAge":21},    {"StudentName":"Carol","StudentAge":24},{"StudentName":"David","StudentAge":25}];

Advertisements