- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 1349 Articles for MongoDB
646 Views
First of all, you need to get your collection with the help of getCollectionNames().The database name is “test”. Let us loop through all MongoDB collections and execute the query. The query is as follows −> db.getCollectionNames().forEach(function(collectioNameDemo) ... { ... var nextDemo = db[(collectioNameDemo) ].find().sort({_id:-1}).limit(1); ... if (nextDemo.hasNext()) ... { ... printjson(nextDemo.next()._id.getTimestamp()); ... } ... });The following is the output −ISODate("2019-02-21T18:52:43Z") ISODate("2019-03-19T17:49:00Z") ISODate("2019-03-06T15:40:12Z") ISODate("2019-03-15T16:31:50Z") ISODate("2019-02-21T15:40:52Z") ISODate("2019-03-06T06:14:37Z") ISODate("2019-02-21T19:29:15Z") ISODate("2019-03-15T13:35:33Z") ISODate("2019-03-14T21:13:58Z") ISODate("2019-03-18T22:02:54Z") ISODate("2019-03-22T18:01:45Z") ISODate("2019-03-06T16:21:14Z") ISODate("2019-02-20T15:04:32Z") ISODate("2019-03-06T07:45:42Z") ISODate("2019-03-19T12:33:17Z") ISODate("2019-03-20T21:39:21Z") ISODate("2019-03-15T16:44:26Z") ISODate("2019-03-22T06:20:45Z") ISODate("2019-02-21T16:40:55Z") ISODate("2019-02-21T12:45:20Z") ISODate("2019-03-06T16:05:48Z") ISODate("2019-03-06T16:00:08Z") ISODate("2019-02-28T12:43:56Z") ISODate("2019-03-20T22:11:41Z") ISODate("2019-03-06T05:56:45Z") ISODate("2019-03-06T07:34:12Z") ISODate("2019-03-14T21:00:16Z") ISODate("2019-02-28T10:33:39Z") ISODate("2019-03-06T05:11:10Z") ISODate("2019-02-28T09:44:28Z") ISODate("2019-03-06T10:13:22Z") ISODate("2019-03-17T21:35:26Z")Read More
714 Views
To understand the query on list field, and/or, you can create a collection with documents.The query to create a collection with a document is as follows −> db.andOrDemo.insertOne({"StudentName":"Larry", "StudentScore":[33, 40, 50, 60, 70]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9522d316f542d757e2b444") } > db.andOrDemo.insertOne({"StudentName":"Larry", "StudentScore":[87, 67, 79, 98, 90]}); { "acknowledged" : true, "insertedId" : ObjectId("5c95230916f542d757e2b445") }Display all documents from a collection with the help of find() method. The query is as follows −> db.andOrDemo.find().pretty();The following is the output −{ "_id" : ObjectId("5c9522d316f542d757e2b444"), "StudentName" : "Larry", "StudentScore" : [ 33, ... Read More
234 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
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
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
185 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
98 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
696 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
235 Views
Yes, it is possible to cast in a MongoDB query −db.yourCollectionName.find("this.yourFieldName >yourValue);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.castingDemo.insertOne({"Amount":"200"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947e874cf1f7a64fa4df42") } > db.castingDemo.insertOne({"Amount":"100"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947e8e4cf1f7a64fa4df43") } > db.castingDemo.insertOne({"Amount":"110"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947e944cf1f7a64fa4df44") } > db.castingDemo.insertOne({"Amount":"95"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947e9d4cf1f7a64fa4df45") } > db.castingDemo.insertOne({"Amount":"85"}); { "acknowledged" : true, "insertedId" : ObjectId("5c947ea44cf1f7a64fa4df46") } > db.castingDemo.insertOne({"Amount":"75"}); { "acknowledged" ... Read More
182 Views
You can use $cmp operator for this. 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.matchBetweenFieldsDemo.insertOne({"FirstValue":40, "SecondValue":70}); { "acknowledged" : true, "insertedId" : ObjectId("5c92c9625259fcd19549980d") } > db.matchBetweenFieldsDemo.insertOne({"FirstValue":20, "SecondValue":5}); { "acknowledged" : true, "insertedId" : ObjectId("5c92c96b5259fcd19549980e") }Display all documents from a collection with the help of find() method. The query is as follows −> db.matchBetweenFieldsDemo.find().pretty();The following is the output −{ "_id" : ObjectId("5c92c9625259fcd19549980d"), "FirstValue" : 40, "SecondValue" : 70 } { "_id" : ... Read More