【问题标题】:Iterate regex through all collections in mongo遍历 mongo 中的所有集合的正则表达式
【发布时间】:2016-01-19 20:33:28
【问题描述】:

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

我以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,...

【问题讨论】:

    标签: regex mongodb loops mongodb-query


    【解决方案1】:

    TheCoo 变量是一个Cursor 对象。可以通过调用光标上的toArray方法获取所有文档的数组:

    TheCoo.toArray(function (err, documents) {
      printjson(documents);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      相关资源
      最近更新 更多