Found 6702 Articles for Database

Can MongoDB find() function display avoiding _id?

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

99 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

MongoDB query to replace value in an array?

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

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

MongoDB regex to display records whose first five letters are uppercase?

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

811 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

MySQL query to fetch date with year and month?

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

280 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

How to select a row where one of several columns equals a certain value in MySQL?

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

332 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

How to perform $gt on a hash in a MongoDB document?

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

72 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

Get the count of the most frequently occurring values in MySQL?

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

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

MySQL query to get current datetime and only current date

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

294 Views

The query SELECT NOW() gives the current date as well as current time. If you want only current date, use only CURDATE(). Following is the syntax for datetime −SELECT NOW();The syntax for only date.SELECT CURDATE();Let us now implement the above syntax −Case 1 : If you want both current date and time −mysql> SELECT NOW();Output+-----------------------+ | NOW() | +-----------------------+ | 2019-06-02 15 :30 :39 | +-----------------------+ 1 row in set (0.00 sec)Case 2 : If you want the current date only −mysql> SELECT CURDATE();Output+------------+ | CURDATE() | +------------+ | 2019-06-02 | +------------+ 1 row in set (0.00 sec)

Check for null in MongoDB?

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

500 Views

We will use the Null type here. Following are the null types with the alias −TypeNumberAliasDouble1“double”String2“string”Object3“object”Array4“array”Binary data5“binData”Undefined6“undefined”ObjectId7“objectId”Boolean8“bool”Date9“date”Null10“null”Regular Expression11“regex”Following is the syntax for type 10 i.e. null −db.yourCollectionName.find({"yourFieldName":{ $type: 10 } });The above syntax will find only those documents which have null value. Let us first create a collection with documents −> db.mongoDbEqualDemo.insertOne({"Age":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9121a844af18acdffa3") } > db.mongoDbEqualDemo.insertOne({"Age":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9161a844af18acdffa4") } > db.mongoDbEqualDemo.insertOne({"Age":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9191a844af18acdffa5") } > db.mongoDbEqualDemo.insertOne({"Age":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e91e1a844af18acdffa6") } > db.mongoDbEqualDemo.insertOne({}); ... Read More

Add two weeks to a date in MySQL?

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

627 Views

To add two weeks to a date in MySQL, use DATE_ADD() −insert into yourTableName(yourColumnName) values(date_add(now(), interval 2 week));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.62 sec)Following is the query to get the current date −mysql> select now(); +-----------------------+ | now() | +-----------------------+ | 2019-06-02 10 :36 :49 | +-----------------------+ 1 row in set (0.00 sec)Insert some records in the table using ... Read More

Advertisements