Improve MongoDB Queries with Multikey Index in Array

AmitDiwan
Updated on 06-Apr-2020 13:32:46

177 Views

For this, use $elemMatch, which is used to query nested objects. Let us create a collection with documents −> db.demo444.insertOne( ...    { ...       "Information": [{ ...          id:1, ...          Name:"Chris" ...       }] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78ea87bbc41e36cc3caebf") } > db.demo444.insertOne( ...    { ...       "Information": [{ ...          id:2, ...          Name:"David" ...       }] ...    } ... ); {    "acknowledged" : true,   ... Read More

Find and Replace a Part of URL Records in MySQL

AmitDiwan
Updated on 06-Apr-2020 13:31:49

942 Views

The simplest way to replace records is using MySQL REPLACE() −mysql> create table DemoTable2025    -> (    -> URL text    -> ); Query OK, 0 rows affected (0.88 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2025 values('http=//www.facebook.com'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable2025 values('http=//www.google.com'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2025 values('http=//www.gmail.com'); Query OK, 1 row affected (0.26 sec)Display all records from the table using select statement −mysql> select *from DemoTable2025;This will produce the following output −+-------------------------+ | URL ... Read More

MongoDB Profiler Output: Command Operation Explained

AmitDiwan
Updated on 06-Apr-2020 13:30:22

245 Views

The following operations are treated as command operation in MongoDB −1.count 2.findAndModify 3.aggregateFollowing is the example of count in MongoDB −Let us create a collection with documents −> db.demo443.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78d281bbc41e36cc3caeb9") } > db.demo443.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78d285bbc41e36cc3caeba") } > db.demo443.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78d288bbc41e36cc3caebb") }Display all documents from a collection with the help of find() method −> db.demo443.find();This will produce the following output −{ "_id" : ObjectId("5e78d281bbc41e36cc3caeb9"), "Name" : "Chris" } { "_id" : ObjectId("5e78d285bbc41e36cc3caeba"), "Name" : "Bob" } { "_id" : ... Read More

Return a List from Different Rows into a Single Field with MySQL

AmitDiwan
Updated on 06-Apr-2020 13:29:37

883 Views

For this, use GROUP_CONCAT(). Let us first create a table −mysql> create table DemoTable2024    -> (    -> SubjectName varchar(20),    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2024 values('MySQL', 'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2024 values('MySQL', 'David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2024 values('MongoDB', 'Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2024 values('Java', 'Sam'); Query OK, 1 row affected (0.18 sec) mysql> ... Read More

Getting Distinct Values from Object Array in MongoDB

AmitDiwan
Updated on 06-Apr-2020 13:28:05

1K+ Views

To get distinct values from object array in MongoDB, use distinct(). Let us create a collection with documents −> db.demo442.insertOne( ...    { ... ...       "Information" : [ ...          { ...             "FirstName" : "John", ...             "Age" : 21 ...          }, ...          { ...             "FirstName" : "Sam", ...             "Age" : 23 ...          }, ...         ... Read More

MySQL Regular Expression to Update Table with Column Values

AmitDiwan
Updated on 06-Apr-2020 13:26:18

747 Views

For this, use UPDATE command along with REGEXP. Let us first create a table −mysql> create table DemoTable2023    -> (    -> StreetNumber varchar(100)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2023 values('7'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2023 values('1'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2023 values('AUS-100'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2023 values('US-101'); Query OK, 1 row affected (0.11 sec)Display all records from the table using ... Read More

MongoDB Query to Aggregate Nested Array

AmitDiwan
Updated on 06-Apr-2020 13:25:14

1K+ Views

To aggregate nested array in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo441.insertOne( ...    { ... ...       "Name" : "David", ...       "Age" : 21, ... ...       "details" : [ ...          { ...             "id" : 1, ...             "CountryName" : "US", ...             "details1" : [ ...                { ...                   "SubjectName" : ... Read More

Insert Multiple Values in a Column with a Single MySQL Query

AmitDiwan
Updated on 06-Apr-2020 13:24:43

2K+ Views

To insert multiple values in a column, the syntax is as follows −insert into yourTableName values(yourValue1), (yourValue2), ..........N;To understand the above syntax, let us create a table −mysql> create table DemoTable2022    -> (    -> Department varchar(100)    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2022 values('Computer Science'), ('Information Technology'), ('Civil'), ('Mechanical'), ('Electronics'), ('Electrical'); Query OK, 6 rows affected (0.46 sec) Records: 6 Duplicates: 0 Warnings: 0Display all records from the table using select statement −mysql> select *from DemoTable2022;This will produce the following ... Read More

Insert Data from One Schema to Another in MySQL

AmitDiwan
Updated on 06-Apr-2020 13:20:37

1K+ Views

To insert data from one scheme to another, the syntax is as follows. Here, we have two databases “yourDatabaseName1” and “yourDatabaseName2” −insert into yourDatabaseName2.yourTableName2 select *from yourDatabaseName1.yourTableName1;To understand the above syntax, let us create a table. We are creating a table in database “web” −mysql> create table DemoTable2020    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2020 values(101, 'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2020 values(102, 'David'); Query OK, 1 ... Read More

Count Items in Array with MongoDB

AmitDiwan
Updated on 06-Apr-2020 13:17:15

283 Views

To count items in array, use length. Let us create a collection with documents −> db.demo440.insertOne( ...    { ...       "Name":"Chris", ...       "ListOfFriends":["John", "Sam", "Mike"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78c63cbbc41e36cc3caeb5") } > > db.demo440.insertOne( ...    { ...       "Name":"David", ...       "ListOfFriends":["Mike", "Bob", "Carol"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78c63cbbc41e36cc3caeb6") }Display all documents from a collection with the help of find() method −> db.demo440.find();This will produce the following output −{ "_id" : ... Read More

Advertisements