Found 6705 Articles for Database

MongoDB query to get last inserted document?

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

14K+ Views

To get last inserted document, use sort() along with limit(1).Let us first create a collection with documents −> db.getLastInsertedDocument.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb17eef71edecf6a1f6a8") } > db.getLastInsertedDocument.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb181ef71edecf6a1f6a9") } > db.getLastInsertedDocument.insertOne({"Name":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb185ef71edecf6a1f6aa") }Display all documents from a collection with the help of find() method −> db.getLastInsertedDocument.find();Output{ "_id" : ObjectId("5cefb17eef71edecf6a1f6a8"), "Name" : "John" } { "_id" : ObjectId("5cefb181ef71edecf6a1f6a9"), "Name" : "Chris" } { "_id" : ObjectId("5cefb185ef71edecf6a1f6aa"), "Name" : "Robert" }Following is the query to get last inserted document −> db.getLastInsertedDocument.find({}).sort({_id:-1}).limit(1);Output{ "_id" ... Read More

MongoDB query to find data from an array inside an object?

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

540 Views

Let us first create a collection with documents −> db.findDataDemo.insertOne(    {       "_id": new ObjectId(),       "CustomerName":"John",       "CustomerDetails" : {          "CountryName" : [             "AUS"          ],          "isMarried" : [             false          ]       }    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefa5eeef71edecf6a1f6a5") } > db.findDataDemo.insertOne(    {       "_id": new ObjectId(),       "CustomerName":"Carol",       ... Read More

Projection result as an array of selected items in MongoDB?

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

158 Views

Use the distinct() for this, since it finds the distinct values for a specified field across a single collection or view and returns the results in an array.Let us first create a collection with documents −> db.projectionListDemo.insertOne({"_id":"1", "Subject":["MongoDB", "MySQL", "Java"]}); { "acknowledged" : true, "insertedId" : "1" } > db.projectionListDemo.insertOne({"_id":"2", "Subject":["MongoDB", "C", "C++"]}); { "acknowledged" : true, "insertedId" : "2" } > db.projectionListDemo.insertOne({"_id":"3", "Subject":["Java", "Python"]}); { "acknowledged" : true, "insertedId" : "3" }Display all documents from a collection with the help of find() method −> db.projectionListDemo.find().pretty();Output{ "_id" : "1", "Subject" : [ "MongoDB", "MySQL", "Java" ] } { "_id" : ... Read More

Removing an array element from MongoDB collection using update() and $pull

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

192 Views

Let us first create a collection with documents -> db.removingAnArrayElementDemo.insertOne({"UserMessage":["Hi", "Hello", "Bye"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef97bdef71edecf6a1f6a4") }Display all documents from a collection with the help of find() method -> db.removingAnArrayElementDemo.find().pretty();Output{    "_id" : ObjectId("5cef97bdef71edecf6a1f6a4"),    "UserMessage" : [       "Hi",       "Hello",       "Bye"    ] }Following is the query to remove an array element from MongoDB -> db.removingAnArrayElementDemo.update(    {_id:ObjectId("5cef97bdef71edecf6a1f6a4")},    { "$pull": { "UserMessage": "Hello" } } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Let us check the document once again:> db.removingAnArrayElementDemo.find().pretty();Output{   ... Read More

Write an equality in MongoDB without using $eq operator

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

116 Views

Let us first create a collection with documents -> db.operatorDemo.insertOne({"StudentSubject":["MongoDB", "MySQL", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef94eaef71edecf6a1f6a2") } > db.operatorDemo.insertOne({"StudentSubject":["Java", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef94faef71edecf6a1f6a3") }Display all documents from a collection with the help of find() method -> db.operatorDemo.find().pretty();Output{    "_id" : ObjectId("5cef94eaef71edecf6a1f6a2"),    "StudentSubject" : [       "MongoDB",       "MySQL",       "Java"    ] } {    "_id" : ObjectId("5cef94faef71edecf6a1f6a3"),    "StudentSubject" : [       "Java",       "C",       "C++"    ] }Following is the query for ... Read More

How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?

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

127 Views

Let us first create a table −mysql> create table DemoTable1    (    value int    ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+-------+ | value | +-------+ | 10 | +-------+ 1 row in set (0.00 sec)Following is the query to create second table −mysql> create table DemoTable2    (    value1 int    ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table ... Read More

Set all values in a MySQL field to 0?

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

2K+ Views

To set all the values in a field to 0, use the update command −update yourTableName set yourColumnName=0;Let us first create a table −mysql> create table DemoTable    (    Number int    ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(4757); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(48474); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(35465); Query OK, 1 row affected (0.16 sec)Display all records from ... Read More

Select specific rows in a range with MySQL?

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

619 Views

Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20)    ); Query OK, 0 rows affected (1.23 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName) values('John'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(StudentName) values('Carol'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(StudentName) values('David'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(StudentName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName) values('Chris'); Query OK, 1 row affected (0.12 sec) ... Read More

Get the last days of all the months in MySQL?

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

128 Views

To get the last days of all the months, use the LAST_DAY() function from MySQL −SELECT LAST_DAY(yourColumnName) from yourTableName;Let us first create a table −mysql> create table DemoTable    (    ShippingDate date    ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-12'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-02-01'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2019-03-04'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('2019-04-21'); Query OK, 1 row affected (0.14 sec)Display all records ... Read More

MySQL query to select date >= current date - 3 weeks?

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

787 Views

Use the concept of DATE_SUB(). Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ArrivalDate datetime    ); Query OK, 0 rows affected (1.02 sec)Note: Let’s say the current date is 2019-06-08Insert some records in the table using insert command −mysql> insert into DemoTable(ArrivalDate) values('2019-05-15'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ArrivalDate) values('2019-06-08'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(ArrivalDate) values('2019-05-20'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(ArrivalDate) values('2019-05-12'); Query OK, 1 row affected (0.12 sec)Display ... Read More

Advertisements