Found 6702 Articles for Database

How to delete n-th element of array in MongoDB?

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

236 Views

You can use $unset as well as $pull operator with an update to delete the nth element of an array.Let us create a collection with a document. The query to create a collection with a document is as follows −> db.getNThElementDemo.insertOne({"UserName":"John", "UserAge":23, "ListOfFriends":["Carol", "Sam", "Mike", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c94ee7516f542d757e2b43e") } > db.getNThElementDemo.insertOne({"UserName":"David", "UserAge":21, "ListOfFriends":["Chris", "Robert"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c94eeaa16f542d757e2b43f") }Display all documents from a collection with the help of find() method. The query is as follows −> db.getNThElementDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c94ee7516f542d757e2b43e"),    "UserName" ... Read More

Identify last document from MongoDB find() result set?

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

167 Views

To identify the last document from MongoDB find() result set, you can use sort() in descending order. The syntax is as follows −db.yourCollectionName.find().sort( { _id : -1 } ).limit(1).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.identifyLastDocuementDemo.insertOne({"UserName":"Larry", "UserAge":24, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c94a2ff4cf1f7a64fa4df57") } > db.identifyLastDocuementDemo.insertOne({"UserName":"Chris", "UserAge":21, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c94a3094cf1f7a64fa4df58") } > db.identifyLastDocuementDemo.insertOne({"UserName":"David", "UserAge":25, "UserCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c94a3174cf1f7a64fa4df59") } > db.identifyLastDocuementDemo.insertOne({"UserName":"Sam", "UserAge":26, "UserCountryName":"US"}); { ... Read More

Ignoring the year in MySQL Query with date range?

Samual Sam
Updated on 30-Jul-2019 22:30:25

497 Views

To ignore the year with date range, use the DATE_FORMAT() with the between clause. Let us first create a demo table. The query to create a table is as follows −mysql> create table igonreYearDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ShippingDate date    -> ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into igonreYearDemo(ShippingDate) values('2016-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into igonreYearDemo(ShippingDate) values('2018-01-31'); Query OK, 1 row affected (0.13 sec) mysql> insert into ... Read More

How to check the current configuration of MongoDB?

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

1K+ Views

In order to check the current configuration of MongoDB, you can use getCmdLineOpts. The query is as follows −> db._adminCommand( {getCmdLineOpts: 1});The following is the output −{ "argv" : [ "mongod" ], "parsed" : { }, "ok" : 1 }In order to check live settings, you can use the below query −> db._adminCommand({getParameter:"*"});The following is the output &minus{    "AsyncRequestsSenderUseBaton" : true,    "KeysRotationIntervalSec" : 7776000,    "ShardingTaskExecutorPoolHostTimeoutMS" : 300000,    "ShardingTaskExecutorPoolMaxConnecting" : 2,    "ShardingTaskExecutorPoolMaxSize" : -1,    "ShardingTaskExecutorPoolMinSize" : 1,    "ShardingTaskExecutorPoolRefreshRequirementMS" : 60000,    "ShardingTaskExecutorPoolRefreshTimeoutMS" : 20000,    "TransactionRecordMinimumLifetimeMinutes" : 30,    "adaptiveServiceExecutorIdlePctThreshold" : 60,    "adaptiveServiceExecutorMaxQueueLatencyMicros" ... Read More

Get the number of days between current date and date field?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

102 Views

To get the number of days between current date and date field, the syntax is as follows −SELECT DATEDIFF(CURDATE(), STR_TO_DATE(yourColumnName, '%d-%m-%Y')) AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table DateDifferenceDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ArrivalDate varchar(100)    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into DateDifferenceDemo(ArrivalDate) values('12-10-2011'); Query OK, 1 row affected (0.14 sec) mysql> insert ... Read More

Reserving MySQL auto-incremented IDs?

Samual Sam
Updated on 30-Jul-2019 22:30:25

223 Views

To reserve MySQL auto-incremented IDs, the syntax is as follows −START TRANSACTION; insert into yourTableName values(), (), (), (); ROLLBACK; SELECT LAST_INSERT_ID() INTO @anyVariableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table reservingAutoIncrementDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY    -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command. The query is as follows −mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> insert into reservingAutoIncrementDemo values(), (), (), (); Query ... Read More

Achieve Pagination with MongoDB?

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

186 Views

You can achieve pagination with the help of limit() and skip() in MongoDB.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.paginationDemo.insertOne({"CustomerName":"Chris", "CustomerAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c949de44cf1f7a64fa4df52") } > db.paginationDemo.insertOne({"CustomerName":"Robert", "CustomerAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c949df14cf1f7a64fa4df53") } > db.paginationDemo.insertOne({"CustomerName":"David", "CustomerAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c949dfc4cf1f7a64fa4df54") } > db.paginationDemo.insertOne({"CustomerName":"Carol", "CustomerAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c949e3e4cf1f7a64fa4df55") } > db.paginationDemo.insertOne({"CustomerName":"Bob", "CustomerAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c949e474cf1f7a64fa4df56") ... Read More

What does a “set+0” in a MySQL statement do?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

57 Views

The set+0 converts the set value to integer. Let us see an example by creating a table −mysql> create table SetZeroDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> TechnicalSkills set('C', 'Spring Framework /Hibernate', 'Python', 'Django Framework', 'Core Java') NOT NULL    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into SetZeroDemo(TechnicalSkills) -> values('C, Spring Framework /Hibernate, Python, Django Framework, Core Java'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement. The ... Read More

How to implement MongoDB $or query?

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

99 Views

The syntax is as follows for the $or query in MongoDB −db.yourCollectionName.find({ $or : [ { "yourFieldName" : "yourValue1" }, {"yourFieldName":"yourValue2"}, ...........N ] } ).pretty();To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.orDemo.insertOne({"UserName":"Larry", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9491fd4cf1f7a64fa4df4c") } > db.orDemo.insertOne({"UserName":"David", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9492074cf1f7a64fa4df4d") } > db.orDemo.insertOne({"UserName":"Mike", "UserAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c94920e4cf1f7a64fa4df4e") } > db.orDemo.insertOne({"UserName":"Sam", "UserAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9492144cf1f7a64fa4df4f") } ... Read More

How to update all documents in MongoDB?

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

699 Views

You can use updateMany() to update documents. Let us create a collection with a document. The query to create a collection with a document is as follows −> db.updateManyDocumentsDemo.insertOne({"StudentName":"John", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c948edd4cf1f7a64fa4df48") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c948ee64cf1f7a64fa4df49") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"Carol", "StudentLastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c948ef14cf1f7a64fa4df4a") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c948f044cf1f7a64fa4df4b") }Display all documents from a collection with the help of find() method. The query is as follows −> db.updateManyDocumentsDemo.find().pretty();The following is the ... Read More

Advertisements