
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6705 Articles for Database

512 Views
To update all elements in an array with a prefix string, use forEach(). Let us first create a collection with documents −> db.replaceAllElementsWithPrefixDemo.insertOne( { "StudentNames" : [ "John", "Carol" ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd91908b50a6c6dd317ad8e") } > > > db.replaceAllElementsWithPrefixDemo.insertOne( { "StudentNames" : [ "Sam" ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd9191cb50a6c6dd317ad8f") }Following is the query to display all documents from ... Read More

234 Views
Use the $pull to remove array elements from a MongoDB document as shown in the following syntax −db.yourCollectionName.update( { }, { $pull: { yourFieldName: yourValue }}, {multi:true });Let us first create a collection with documents −>db.removeArrayElementsDemo.insertOne({"AllPlayerName":["John", "Sam", "Carol", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd90d011a844af18acdffc1") } >db.removeArrayElementsDemo.insertOne({"AllPlayerName":["Chris", "Robert", "John", "Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd90d2e1a844af18acdffc2") }Following is the query to display all documents from a collection with the help of find() method −> db.removeArrayElementsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd90d011a844af18acdffc1"), "AllPlayerName" : [ "John", ... Read More

228 Views
You can use $exists operator for this. Let us first create a collection with documents −>db.checkFieldExistsDemo.insertOne({"StudentFirstName":"John", "StudentGender":"Male", "StudentMongoDBScore":89}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909611a844af18acdffbd") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Emma", "StudentGender":"Female", "StudentMongoDBScore":58}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909781a844af18acdffbe") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Carol", "StudentGender":"Male", "StudentMongoDBScore":77}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909871a844af18acdffbf") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"David", "StudentMongoDBScore":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909a31a844af18acdffc0") }Following is the query to display all documents from a collection with the help of find() method −> db.checkFieldExistsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd909611a844af18acdffbd"), "StudentFirstName" : "John", "StudentGender" ... Read More

154 Views
Yes, we can avoid the _id, using the following syntax in MongoDB −db.yourCollectionName.find({}, { _id:0});Let us first create a collection with documents:>> db.excludeIdDemo.insertOne({"CustomerName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f62c1a844af18acdffb9") } > db.excludeIdDemo.insertOne({"CustomerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6311a844af18acdffba") } > db.excludeIdDemo.insertOne({"CustomerName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6351a844af18acdffbb") } > db.excludeIdDemo.insertOne({"CustomerName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6381a844af18acdffbc") }Following is the query to display all documents from a collection with the help of find() method −> db.excludeIdDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd7f62c1a844af18acdffb9"), "CustomerName" : "Larry" } ... Read More

1K+ Views
Use $set to replace value in an array. Let us first create a collection with documents −> db.replaceValueInArrayDemo.insertOne({"StudentScores":[45, 56, 78]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f0421a844af18acdffb7") } > db.replaceValueInArrayDemo.insertOne({"StudentScores":[33, 90, 67]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f0521a844af18acdffb8") }Following is the query to display all documents from a collection with the help of find() method −> db.replaceValueInArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd7f0421a844af18acdffb7"), "StudentScores" : [ 45, 56, 78 ] } { "_id" : ObjectId("5cd7f0521a844af18acdffb8"), "StudentScores" : [ ... Read More

961 Views
Let us first create a collection with documents −> db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"JOHN Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7edef1a844af18acdffb2") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"SAM Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee011a844af18acdffb3") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"CAROL Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee101a844af18acdffb4") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"Bob Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee351a844af18acdffb5") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"DAVID Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee451a844af18acdffb6") }Following is the query to display all documents from a collection with the help of find() method −> db.upperCaseFiveLetterDemo.find().pretty();This will produce the following output −{ "_id" : ... Read More

333 Views
For year and month date fetching, you can use YEAR() and MONTH() function in MySQL. Let us first create a table −mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ShippingDate datetime ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ShippingDate) values('2019-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(ShippingDate) values('2018-12-01'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(ShippingDate) values('2019-06-02'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(ShippingDate) values('2019-11-18'); Query OK, 1 row ... Read More

426 Views
For this, you can use multiple OR. Let us first create a table −mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(10), LastName varchar(10), Age int, CountryName varchar(10) ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName, LastName, Age, CountryName) values('John', 'Smith', 21, 'US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName, LastName, Age, CountryName) values('Carol', 'Taylor', 22, 'AUS'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName, LastName, Age, CountryName) values('David', ... Read More

121 Views
The $gt is for greater than, wherein select those documents where the value of the field is greater than the specified value. Let us first create a collection with documents −> db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1000, "PlayerLevel":2}, "PlayerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7eba41a844af18acdffa9") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":0, "PlayerLevel":1}, "PlayerName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebbb1a844af18acdffaa") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":-10, "PlayerLevel":0}, "PlayerName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebd41a844af18acdffab") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1, "PlayerLevel":1}, "PlayerName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebe31a844af18acdffac") }Following is the query to display all documents from a collection with the help of ... Read More

1K+ Views
For this, use the aggregate function COUNT() along with GROUP BY. Let us first create a table −mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value int ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values(976); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Value) values(67); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Value) values(67); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Value) values(1); Query OK, 1 row affected (0.27 sec) mysql> ... Read More