Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through all Mongo collections and execute query

First of, I'm quite new to mongodb. Here's my question I've not been able to find a solution to.

Let's say I have 3 different collections.

mongos> show collections collectionA collectionB collectionC 

I want to create a script that iterates over all collections ind this database and find the last inserted timestamp in each of these collections. Here's what works inside mongos.

var last_element = db.collectionA.find().sort({_id:-1}).limit(1); printjson(last_element.next()._id.getTimestamp()); ISODate("2014-08-28T06:45:47Z") 

1. Problem (Iterate over all collections)

Is there any possibility to to sth. like.

var my_collections = show collections; my_collections.forEach(function(current_collection){     print(current_collection); }); 

Problem here, the assignment for my_collections does not work. I get SyntaxError: Unexpected identifier. Do I need to quote the 'show' statement ? Is it even possible ?

2. Problem (storing collection in js var)

I can workaround Problem 1 by doing this:

var my_collections = ["collectionA", "collectionB", "collectionC"]; my_collections.forEach(function(current_collection){     var last_element = db.current_collection.find().sort({_id:-1}).limit(1);     print(current_collection);     printjson(last_element.next()._id.getTimestamp()); }); 

The last_element.next() produces the following error:

error hasNext: false at src/mongo/shell/query.js:124

It seems that last_element isn't saved correctly.

Any suggestions on what I'm doing wrong??


UPDATE

Neils answer lead me to this solution. In addition to his code I had to check if the function getTimestamp really exist. For some 'virtual' collections there seem to be no _id property.

db.getCollectionNames().forEach(function(collname) {     var last_element = db[collname].find().sort({_id:-1}).limit(1);     if(last_element.hasNext()){         var next = last_element.next();         if(next._id !== undefined && typeof next._id.getTimestamp == 'function'){            printjson(collname + " >> "+next._id.getTimestamp());         }else{           print(collname + " undefined!! (getTimestamp N/A)")         }     } }); 
like image 907
cb0 Avatar asked Aug 28 '14 07:08

cb0


People also ask

How do I fetch all collections in MongoDB?

To list all collections in Mongo shell, you can use the function getCollectionNames().

Can MongoDB query joins between collections?

Fortunately, MongoDB Joins can be performed in MongoDB 3.2 as it introduces a new Lookup operation that can perform Join operations on Collections.

How do I search multiple collections in MongoDB?

For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents.

What is the command to view all the collections in the MongoDB?

To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections . This command will return all collections created within a MongoDB database.


1 Answers

There is the db.getCollectionNames() helper method that does this for you. You can then implement your code:

db.getCollectionNames().forEach(function(collname) {     // find the last item in a collection     var last_element = db[collname].find().sort({_id:-1}).limit(1);     // check that it's not empty     if (last_element.hasNext()) {         // print its timestamp         printjson(last_element.next()._id.getTimestamp());     } }) 

You probably also want a .hasNext() check in there to cater for possible empty collections.

like image 191
Neil Lunn Avatar answered Oct 04 '22 10:10

Neil Lunn