AmitDiwan has Published 10744 Articles

MongoDB query to add a document in an already created collection

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 07:36:36

184 Views

To add a document in an already created collection, use $push in MongoDB. Let us create a collection with documents −> db.demo177.insertOne(    { "Id": "101", "details": [         { "StudentName": "Chris",  "Scores": [67, 71, 74], "SubjectName": ["MySQL", "Java"]  },       { "StudentName": "David", "Scores": ... Read More

Get substring in MongoDB aggregate

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 07:27:23

625 Views

To get substring, use $substr in MongoDB. Let us create a collection with documents −> db.demo176.insertOne({"ProductName":"PRODUCT-1"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843a09e4f06af551997ef") } > db.demo176.insertOne({"ProductName":"PRODUCT-102"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843a69e4f06af551997f0") } > db.demo176.insertOne({"ProductName":"PRODUCT-105"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843aa9e4f06af551997f1") }Display ... Read More

How do I use MongoDB to count only collections that match two fields?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 06:52:51

115 Views

To count only collections that match two fields, use count(). Let us create a collection with documents −> db.demo175.insertOne({"EmployeeName":"Bob", "isMarried":"YES"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840969e4f06af551997e8") } > db.demo175.insertOne({"EmployeeName":"David", "isMarried":"NO"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e38409e9e4f06af551997e9") } > db.demo175.insertOne({"EmployeeName":"Mike", "isMarried":"YES"}); {    "acknowledged" : ... Read More

Evaluate one of more values from a MongoDB collection with documents

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 06:51:19

117 Views

To evaluate one or more values, use $or along with find(). Let us create a collection with documents −> db.demo174.insertOne({"StudentName":"Chris", "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383c709e4f06af551997e5") } > db.demo174.insertOne({"StudentName":"David", "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383c779e4f06af551997e6") } > db.demo174.insertOne({"StudentName":"Bob", "CountryName":"AUS"}); {    "acknowledged" ... Read More

Limit number of values in a field with MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 06:49:39

189 Views

To limit the number of values in a field, use MongoDB $slice. Let us create a collection with documents −> db.demo173.insertOne({"ListOfValues":[10, 40, 100, 560, 700, 900]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383a4f9e4f06af551997e4") }Display all documents from a collection with the help of find() method −> db.demo173.find().pretty();This will ... Read More

Find a value in lowercase from a MongoDB collection with documents

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 06:45:06

3K+ Views

To find a value in lowercase, use the toLowerCase() method in MongoDB. Use the method in find() to find the value in lowercase.Let us create a collection with documents −> db.demo172.insertOne({"SubjectName":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3838ce9e4f06af551997e1") } > db.demo172.insertOne({"SubjectName":"mongodb"}); {     "acknowledged" : true,   ... Read More

How to re-map the fields of a MongoDB collection?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 06:40:03

257 Views

To re-map the fields of a MongoDB collection, use update() along with $rename. Let us first create a collection with documents −> db.demo171.insertOne( { "Name": "Chris", "Details": { "SubjectName": "MySQL", "CountryName": "US" } } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3837 399e4f06af551997e0") }Display all documents from a ... Read More

How to add a specific character to any empty space in MySQL table values?

AmitDiwan

AmitDiwan

Updated on 09-Mar-2020 08:32:46

393 Views

For this, use REPLACE() function and replace empty space with the character. Let us first create a table −mysql> create table DemoTable (Subject text); Query OK, 0 rows affected (0.86 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row ... Read More

How do I re-format datetime in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Mar-2020 11:59:13

410 Views

To re-format datetime in MySQL, you can use DATE_FORMAT(). MySQL gives in the format yyyy-mm-dd.Let us first create a table −mysql> create table DemoTable1558    -> (    -> EmployeeJoiningDate datetime    -> ); Query OK, 0 rows affected (1.10 sec)Insert some records in the table using insert command −mysql> ... Read More

Select equal or nearest greater number from table in MySQL

AmitDiwan

AmitDiwan

Updated on 03-Mar-2020 07:04:48

756 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Value int    -> ); Query OK, 0 rows affected (1.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(25); Query OK, 1 row affected (0.11 sec) mysql> insert into ... Read More

Advertisements