0

I am trying to determine which collections have a document that matches a certain pattern.

I based the below off this answer, however instead of printing the result, what looks to be a function definition gets printed.

db.getCollectionNames().forEach(function(collname) {
    // find the last item in a collection
    var isPresent = db[collname].find().sort({_id:-1}).limit(1);
    // check that it's not empty
    if (isPresent.hasNext()) {
    var TheCoo = db[collname].find({"_id":{ $regex: 'thecoolest.com'} });
    printjson(TheCoo);
    printjson(collname);
    }
})

Here is a sample of the output:

{
    "_mongo" : connection to xxx.xxx.xxx,
    "_db" : etf,
    "_collection" : coo.data,
    "_ns" : "coo.data",
    "_query" : {
        "_id" : {
            "$regex" : "thecoolest.com"
        }
    },
    "_fields" : null,
    "_limit" : 0,
    "_skip" : 0,
    "_batchSize" : 0,...
Community
  • 1
  • 1
Peter
  • 1,065
  • 14
  • 29

1 Answers1

1

The TheCoo variable is a Cursor object. You can obtain an array of all the documents by calling the toArray method on the cursor:

TheCoo.toArray(function (err, documents) {
  printjson(documents);
});
gnerkus
  • 11,357
  • 6
  • 47
  • 71