Nishtha Thakur has Published 564 Articles

Filter by several array elements in MongoDB?

Nishtha Thakur

Nishtha Thakur

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

136 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

137 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

226 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

Java DatabaseMetaData getMaxCharLiteralLength() method with example.

Nishtha Thakur

Nishtha Thakur

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

59 Views

The getMaxCharLiteralLength() method of the DatabaseMetaData interface is used to find out the maximum number of characters that the underlying database allows for a character literal.This method returns an integer value, representing the maximum length of a character literal. If this value is 0 it indicates that there is no ... Read More

Combine columns before matching it with LIKE in a single query in MySQL?

Nishtha Thakur

Nishtha Thakur

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

99 Views

You can use CONCAT() function for this. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value1 varchar(10),    Value2 varchar(10)    ); Query OK, 0 rows affected (0.21 sec)Insert some records in the table using insert command ... Read More

How to query all items in MongoDB?

Nishtha Thakur

Nishtha Thakur

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

236 Views

To query all items, use find(). Let us first create a collection with documents −> db.queryAllItemsDemo.insertOne({"StudentDetails":{"StudentName":"John", "StudentSubject":["MongoDB", "MySQL"], "StudentSubjectPrice":[4000, 6000]}, "OtherDetails":{"UserAge":29, "UserCountryName":"US"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef74ecef71edecf6a1f69f") }Display all documents from a collection with the help of find() method −> db.queryAllItemsDemo.find().pretty();This will produce the following output ... Read More

MySQL query to get result by month and year based on condition?

Nishtha Thakur

Nishtha Thakur

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

192 Views

You need to use OR condition along with WHERE clause. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    MonthNumber int,    YearNumber int    ); Query OK, 0 rows affected (0.22 sec)Insert some records in the table ... Read More

Count number of elements in an array with MongoDB?

Nishtha Thakur

Nishtha Thakur

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

904 Views

To count number of elements in an array, use the aggregate framework. Let us first create a collection with documents −>db.countNumberOfElementsDemo.insertOne({"UserMessage":["Hi", "Hello", "Bye", "Awesome"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef8ec2ef71edecf6a1f6a1") }Display all documents from a collection with the help of find() method −> db.countNumberOfElementsDemo.find().pretty();This will produce the ... Read More

Advertisements