- 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
Loop through all MongoDB collections and execute query?
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")
Advertisements