通过mongo中的所有集合迭代正则表达式

时间:2016-01-19 20:33:28

标签: regex mongodb loops mongodb-query

我正在尝试确定哪些集合具有与特定模式匹配的文档。

我将以下关闭this answer,但不是打印结果,而是打印出看起来像函数定义的内容。

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);
    }
})

以下是输出示例:

{
    "_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,...

1 个答案:

答案 0 :(得分:1)

product seller.x seller.y p.value 1: banana A B 0.9384329 2: banana A C 0.2413946 3: banana B C 0.2154216 4: lemon A B 0.7282811 5: orange A C 0.0354320 变量是Cursor对象。您可以通过调用光标上的toArray方法获取所有文档的数组:

TheCoo
相关问题