Nishtha Thakur has Published 387 Articles

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

Nishtha Thakur

Nishtha Thakur

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

184 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 }       ]    } ); { ... Read More

Performing mathematical operations in MySQL IF then ELSE is possible?

Nishtha Thakur

Nishtha Thakur

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

324 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 ... Read More

Convert a field to an array using MongoDB update operation?

Nishtha Thakur

Nishtha Thakur

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

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 ... Read More

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

Nishtha Thakur

Nishtha Thakur

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

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, ... Read More

Update a single list item of a MongoDB document?

Nishtha Thakur

Nishtha Thakur

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

224 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 ... Read More

Filter by several array elements in MongoDB?

Nishtha Thakur

Nishtha Thakur

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

229 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": [       {     ... Read More

How to store time created in a MySQL table?

Nishtha Thakur

Nishtha Thakur

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

226 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 ... Read More

Replace an array field value with MongoDB?

Nishtha Thakur

Nishtha Thakur

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

341 Views

You can use positional operator $. Let us first create a collection with documents −> db.replaceAnArrayFieldValueDemo.insertOne({"StudentTechnicalSubjects":["MySQL", "SQL Server", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea41e0ef71edecf6a1f68f") }Following is the query to display all documents from a collection with the help of find() method −> db.replaceAnArrayFieldValueDemo.find().pretty();This will produce the ... Read More

MySQL query to search exact word from string?

Nishtha Thakur

Nishtha Thakur

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

2K+ Views

To search exact word from string, use the below syntax −select *from yourTableName where yourColumnName regexp '(^|[[:space:]])yourWord([[:space:]]|$)';Let us first create a table −mysql> create table DemoTable    (    Title text    ); Query OK, 0 rows affected (0.23 sec)Insert some records in the table using insert command −mysql> insert ... Read More

How to dynamically build MongoDB query?

Nishtha Thakur

Nishtha Thakur

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

2K+ Views

To build query dynamically, you need to write some script. Let us first create a collection with documents −> db.dynamicQueryDemo.insertOne({"Name":"John", "Subject":["MongoDB", "MySQL"]}); {     "acknowledged" : true,     "insertedId" : ObjectId("5cef5c5def71edecf6a1f69a") } > db.dynamicQueryDemo.insertOne({"Name":"John", "Subject":["C", "C++"]}); {     "acknowledged" : true,     "insertedId" : ObjectId("5cef5c73ef71edecf6a1f69b") } ... Read More

Advertisements