Found 6702 Articles for Database

Search for documents matching first item in an array with MongoDB?

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

97 Views

Let us first create a collection with documents −> db.matchingFirstItemInTheArrayDemo.insertOne(    {       "ClientDetails": [          {             "ClientName": "Larry",             "ClientAge":28          }       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7a5d26d78f205348bc636") } > db.matchingFirstItemInTheArrayDemo.insertOne( {    "ClientDetails": [       {          "ClientName": "Chris",          "ClientAge":56,       }    ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7a5f56d78f205348bc637") } > db.matchingFirstItemInTheArrayDemo.insertOne( ... Read More

Remove last char if it's a specific character in MySQL?

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

1K+ Views

To remove last char if it is a specific character then use SUBSTRING(). Let us first create a table −mysql> create table DemoTable    (    SubjectName varchar(100)    ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('MongoDB?'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Java?'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('C'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select ... Read More

Display dates after NOW() + 10 days from a MySQL table?

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

593 Views

You can use DATE_ADD() function with where clause for this. Let us first create a table −mysql> create table DemoTable    (    ShippingDate date    ); Query OK, 0 rows affected (0.54 sec)Note : The current date and time is as follows, we found using NOW() −mysql> select now(); +-----------------------+ | now() | +-----------------------+ | 2019-06-04 20 :43 :57 | +-----------------------+ 1 row in set (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-06-16'); Query OK, 1 ... Read More

How to exclude _id without including other fields using the aggregation framework in MongoDB?

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

640 Views

Let us first create a collection with documents −> db.excludeIdDemo.insertOne({"StudentFirstName":"John", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd701a56d78f205348bc632") } > db.excludeIdDemo.insertOne({"StudentFirstName":"Robert", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd701af6d78f205348bc633") } > db.excludeIdDemo.insertOne({"StudentFirstName":"Chris", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd701b86d78f205348bc634") }Following is the query to display all documents from a collection with the help of find() method −> db.excludeIdDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd701a56d78f205348bc632"), "StudentFirstName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cd701af6d78f205348bc633"), "StudentFirstName" : "Robert", "StudentAge" : 20 } { "_id" : ObjectId("5cd701b86d78f205348bc634"), "StudentFirstName" : "Chris", "StudentAge" ... Read More

How to search between columns in MySQL?

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

110 Views

Use BETWEEN clause to search between columns. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Score1 int,    Score2 int    ); Query OK, 0 rows affected (0.78 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Score1, Score2) values(45, 65); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable(Score1, Score2) values(450, 680); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Score1, Score2) values(800, 900); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement ... Read More

Concatenate fields in MongoDB?

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

275 Views

To concatenate fields, use the $concat operator. Let us first create a collection with documents −>db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Adam", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ebf46d78f205348bc62e") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ebfc6d78f205348bc62f") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ec376d78f205348bc630") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Sam", "StudentLastName":"Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ec436d78f205348bc631") }Following is the query to display all documents from a collection with the help of find() method −> db.concatenateFieldsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd6ebf46d78f205348bc62e"),    "StudentFirstName" : "Adam",    "StudentLastName" : "Smith" } ... Read More

Concatenate two tables in MySQL with a condition?

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

504 Views

To concatenate two tables, UNION ALL in MySQL. Let us create a table −mysql> create table DemoTable1    (    Id int,    FirstName varchar(20)    ); Query OK, 0 rows affected (1.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10, 'John'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable1 values(20, 'Carol'); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+------+-----------+ | Id | FirstName | +------+-----------+ | 10 | John | ... Read More

MongoDB query where all array items are less than a specified condition?

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

174 Views

Let us first create a collection with documents −> db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[89, 43, 32, 45]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e9f9b50a6c6dd317adb3") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[32, 33, 34, 40]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea13b50a6c6dd317adb4") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[45, 56, 66, 69]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea25b50a6c6dd317adb5") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[46, 66, 77, 88]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea3cb50a6c6dd317adb6") }Following is the query to display all documents from a collection with the help of find() method −> db.arrayElementsNotGreaterThanDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd9e9f9b50a6c6dd317adb3"),    "Scores" : [ ... Read More

How can I view the indexes I have set up in MySQL?

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

78 Views

To view the indexes, you can use SHOW command.Following is the syntax −show index from yourTableName;Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20),    LastName varchar(20)    ); Query OK, 0 rows affected (0.46 sec)Following is the query to create an index −mysql> create index id_FirstName on DemoTable(Id, FirstName); Query OK, 0 rows affected (0.52 sec) Records : 0 Duplicates : 0 Warnings : 0Following is the query to view the indexes I have set up in MySQL −mysql> show index from DemoTable;This will produce ... Read More

How to work multiple AND conditions in MySQL?

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

1K+ Views

To work with multiple AND conditions in MySQL, following is the syntax −select *from yourTableName where yourColumnName1=yourValue1 and yourColumnName2=yourValue2 and yourColumnName3=yourValue3;Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20),    StudentAge int,    StudentCountryName varchar(40)    ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentAge, StudentCountryName) values('John', 23, 'US'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName, StudentAge, StudentCountryName) values('Carol', 21, 'UK'); Query OK, 1 row affected (0.15 sec) mysql> insert ... Read More

Advertisements