Found 6702 Articles for Database

How to remove object from array in MongoDB?

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

2K+ Views

You can use $pull operator to remove the object from an array in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.removeObjectFromArrayDemo.insertOne( ... {    ...    ... "StudentName": "John",    ... "StudentAcademicProjectDetails":    ... [{          ... "StudentProjectId": 101,          ... "StudentProjectName": "Pig Dice Game"       ... },       ... {          ... "StudentProjectId": 110,          ... "StudentProjectName": "Library Management System"       ... ... Read More

How does MongoDB index arrays?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

113 Views

MongoDB indexes every value of an array so that you can query for single elements.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.indexingForArrayElementDemo.insertOne({"StudentFavouriteSubject":["MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8acdca6cea1f28b7aa0816") }Display all documents from a collection with the help of find() method. The query is as follows −> db.indexingForArrayElementDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8acdca6cea1f28b7aa0816"),    "StudentFavouriteSubject" : [       "MongoDB",       "MySQL"    ] }Here is the query by which MongoDB index ... Read More

How to alter a MySQL Column from varchar(30) to varchar(100)?

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

496 Views

You need to use ALTER TABLE command along with MODIFYThe syntax is as followsALTER TABLE yourTableName MODIFY COLUMN yourColumnName varchar(100) NOT NULL;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table syntaxOfAlterCommandDemo    -> (    -> UserId int,    -> UserName varchar(30),    -> UserAge int,    -> UserCityName varchar(50)    -> ); Query OK, 0 rows affected (0.51 sec)Let us check the description of the table.The query is as followsmysql> desc syntaxOfAlterCommandDemo;The following is the output+--------------+-------------+------+-----+---------+-------+ | Field | Type ... Read More

MySQL replace values in a table?

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

188 Views

To replace values in a table, use the CASE statement. To understand the concept, let us create a table. The query to create a table is as followsmysql> create table replaceValueDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(10),    -> isGreaterThan18 varchar(10)    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into replaceValueDemo(Name, isGreaterThan18) values('John', 'YES'); Query OK, 1 row affected (0.24 sec) mysql> insert into replaceValueDemo(Name, isGreaterThan18) values('Carol', 'NO'); Query OK, 1 row affected (0.16 sec) ... Read More

MongoDB aggregation framework match OR is possible?

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

161 Views

Let us first create a collection with the document. The query to create a collection with a document is as follows −> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"John",    "StudentLastName":"Smith", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3a96cea1f28b7aa080d") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Carol",    "StudentLastName":"Tayor", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3bc6cea1f28b7aa080e") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"David",    "StudentLastName":"Miller", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3ce6cea1f28b7aa080f") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Bob",    "StudentLastName":"Taylor", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3e16cea1f28b7aa0810") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Robert",    "StudentLastName":"Smith", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3fb6cea1f28b7aa0811") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Mike",    "StudentLastName":"Miller", ... Read More

Retrieve time from MySQL as HH:MM format?

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

2K+ Views

To retrieve time as HH:MM format, use the DATE_FORMAT() function. To understand the function and retrieve time, let us create a table.The query to create a table is as followsmysql> create table retrieveTimeDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserArrivalTime datetime    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into retrieveTimeDemo(UserArrivalTime) values('2013-01-21 13:45:34'); Query OK, 1 row affected (0.16 sec) mysql> insert into retrieveTimeDemo(UserArrivalTime) values('2014-11-25 11:40:56'); Query OK, 1 row affected (0.11 sec) mysql> insert into retrieveTimeDemo(UserArrivalTime) ... Read More

MongoDB query condition on comparing 2 fields?

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

305 Views

To query condition on comparing 2 fields, use the following syntax −db.yourCollectionName.find( { $where: function() { return this.yourFirstFieldName < this.yourSecondFieldName } } ).pretty();To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.comparingTwoFieldsDemo.insertOne({"StudentName":"John", "StudentAge":21, "StudentMathMarks":99, "StudentPhysicsMarks":98}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac09e6cea1f28b7aa0807") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"Carol", "StudentAge":22, "StudentMathMarks":79, "StudentPhysicsMarks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac0b46cea1f28b7aa0808") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"David", "StudentAge":24, "StudentMathMarks":39, "StudentPhysicsMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac0c96cea1f28b7aa0809") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"Bob", "StudentAge":23, "StudentMathMarks":87, "StudentPhysicsMarks":78}); {    "acknowledged" : ... Read More

Get MySQL maximum value from 3 different columns?

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

6K+ Views

To get the maximum value from three different columns, use the GREATEST() function.The syntax is as followsSELECT GREATEST(yourColumnName1, yourColumnName2, yourColumnName3) AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table MaxOfThreeColumnsDemo    -> (    -> First int,    -> Second int,    -> Third int    -> ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into MaxOfThreeColumnsDemo values(30, 90, 60); Query OK, 1 row affected (0.16 sec) mysql> insert into MaxOfThreeColumnsDemo values(100, ... Read More

How to restart MySQL server?

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

1K+ Views

Restart the MySQL Server with the help of restart command.The syntax is as followsRestartLet us first check the MySQL version.The query is as followsSELECT version();Now, implement the above command in order to restart the MySQL Server.The query is as followsmysql> restart; Query OK, 0 rows affected (0.00 sec)Case 1Now, MySQL Server is being restarted. If you try to perform any query during the restart command, you will get an error.The query is as followsmysql> show databases; ERROR 2013 (HY000): Lost connection to MySQL server during queryCase 2If the MySQL restart process is complete, then a new connection id will be ... Read More

How to remove an array element by its index in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

876 Views

You can remove an array element by its index using the following two steps −The first step is as follows −db.yourCollectionName.update({}, {$unset : {"yourArrayFieldName.yourIndexValue" : 1 }});The above syntax will put a null value at the location of ‘yourIndexValue’. After that, you need to pull the null value from array filed to remove from an array element.The second step is as follows −db.yourCollectionName.update({}, {$pull : {"yourArrayFieldName" : null}});To implement the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.removeArrayElementByItsIndexDemo.insertOne({"InstructorName":"David",    "InstructorAge":28, "InstructorSubject":["MongoDB", "MySQL", "Java", "SQL Server", ... Read More

Advertisements