Nishtha Thakur

Nishtha Thakur

398 Articles Published

Articles by Nishtha Thakur

Page 26 of 40

MongoDB query to remove empty objects in an object-array?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 944 Views

You can use $pull operator for this. Let us first create a collection with documents. Here, we have also added an empty object −> db.removeEmptyObjectsDemo.insertOne(    {       "_id" :101,       "LoginDate" :new ISODate(),       "UserDetails" : [          {             "UserName" : "John"          },          {          },          {             "UserName" : "Sam"          }       ]    } ); { ...

Read More

MongoDB query to update an array element matching a condition using $push?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 469 Views

Let us first create a collection with documents −> db.updateArrayElementDemo.insertOne(    {       "UserDetails":       [          {             "UserName":"Chris",             "UserAge":23          }       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce9029378f00858fb12e90d") }Following is the query to display all documents from a collection with the help of find() method −> db.updateArrayElementDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ce9029378f00858fb12e90d"),    "UserDetails" : [       {       ...

Read More

Updating boolean value in MySQL?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 668 Views

To update boolean value, you can use SET. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, isMarried boolean    ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(isMarried) values(false); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(isMarried) values(true); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(isMarried) values(true); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(isMarried) values(false); Query OK, 1 row affected (0.13 sec)Display all records ...

Read More

Find documents where all elements of an array have a specific value in MongoDB?\\n

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 187 Views

You can use find() for this. Let us first create a collection with documents −> db.findDocumentsDemo.insertOne(    {       _id: 101,       "ProductDetails": [          { "ProductValue":100 },          { "ProductValue":120 }       ]    } ); { "acknowledged" : true, "insertedId" : 101 } > db.findDocumentsDemo.insertOne(    {       _id: 102,       "ProductDetails": [          { "ProductValue":120},          { "ProductValue":120 },          { "ProductValue":120 }       ]    } ); { "acknowledged" ...

Read More

Performing mathematical operations in MySQL IF then ELSE is possible?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 327 Views

For performing mathematical operations and working with conditions, you can consider CASE statement. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FruitName varchar(100),    FruitPrice int    ); Query OK, 0 rows affected (0.26 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FruitName, FruitPrice) values('Orange', 250); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FruitName, FruitPrice) values('Banana', 100); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(FruitName, FruitPrice) values('Apple', 150); Query OK, 1 row affected (0.05 sec) ...

Read More

Convert a field to an array using MongoDB update operation?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 1K+ Views

To convert a field to an array, use $set operator. Let us first create a collection with documents −> db.convertAFieldToAnArrayDemo.insertOne({"StudentSubject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce92d7778f00858fb12e91d") }Following is the query to display all documents from a collection with the help of find() method −> db.convertAFieldToAnArrayDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce92d7778f00858fb12e91d"), "StudentSubject" : "MongoDB" }Following is the query to convert a field to an array using update operation with $set:−> db.convertAFieldToAnArrayDemo.find().forEach(function(myDocument) {    db.convertAFieldToAnArrayDemo.update(       { _id: myDocument._id },       { "$set": { "StudentSubject": [myDocument.StudentSubject] } }    ); })Let ...

Read More

Adding new column after a specific column and defining a default in MySQL?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 2K+ Views

You need to follow some steps to add a new column after a specific column and defining default value. In order to achieve this, you need to use ALTER command. Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20),    StudentAge int,    StudentCountryName varchar(100)    ); Query OK, 0 rows affected (0.21 sec)Let us check the description of table −mysql> desc DemoTable;This will produce the following output −+--------------------+--------------+------+-----+---------+----------------+ | Field              | Type         | Null | ...

Read More

Update a single list item of a MongoDB document?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 226 Views

To update a single list item, use positional operator($). Let us first create a collection with documents −> db.updateASingleListDemo.insertOne({ _id:1, "EmployeeName":"Chris", "EmployeeDetails": [ {"EmployeeId":"EMP-101", "EmployeeSalary": 18999 }] }); { "acknowledged" : true, "insertedId" : 1 }Following is the query to display all documents from a collection with the help of find() method −> db.updateASingleListDemo.find().pretty();This will produce the following output −{    "_id" : 1,    "EmployeeName" : "Chris",    "EmployeeDetails" : [       {          "EmployeeId" : "EMP-101",          "EmployeeSalary" : 18999       }    ] }Following is the query ...

Read More

Filter by several array elements in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 230 Views

For this, you can use $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Let us first create a collection with documents −> db.filterBySeveralElementsDemo.insertOne(    "_id":100,    "StudentDetails": [       {          "StudentName": "John",          "StudentCountryName": "US",       },       {          "StudentName": "Carol",          "StudentCountryName": "UK"       }    ] } ); { "acknowledged" : true, "insertedId" : 100 } > db.filterBySeveralElementsDemo.insertOne(    { ...

Read More

How to store time created in a MySQL table?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 229 Views

You can use DEFAULT CURRENT_TIMESTAMP. Keep in mind that it will work only at the time of insertion. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Arrivaltime TIMESTAMP DEFAULT CURRENT_TIMESTAMP    ); Query OK, 0 rows affected (0.31 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Arrivaltime) values('2018-01-31 10:34:56'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Arrivaltime) values('2019-01-31 11:10:12'); Query OK, 1 row affected (0.04 sec)Display ...

Read More
Showing 251–260 of 398 articles
« Prev 1 24 25 26 27 28 40 Next »
Advertisements