mongodb 填充平面文件结果?

mongodb populate flat file results?

我正在尝试从文件系统中删除旧文件
文件列表在 mongoDb 集合中维护,如下所示:

{
    "_id" : ObjectId("59a39215953f77968789d692"),
    "className" : "com.xerox.model.ModuleDocument",
    "documentId" : "0000001643",
    "documentDate" : ISODate("2017-05-11T04:00:00.000Z"),
    "documentType" : "PRINTER_VALIDATION_2017",
    "module" : "PRINTERS",
    "printerId" : "1002563",
    "origFileName" : "0000001643.JPG",
    "isDeleted" : true
}

但是当我使用 mongo shell

查询数据库时
 db.getCollection('xerox_documents').find({"isDeleted":true})

结果显示为JSON, 是否有可能获得纯文本格式的结果? 所需的输出类似于:

DEL c:\DOCS\PRINTERS\PRINTER_VALIDATION_2017[=13=]00001643.JPG  
DEL c:\DOCS\PRINTERS\PRINTER_VALIDATION_2017[=13=]00001643.JPG

您需要使用$concat连接集合中的任何字段

db.getCollection('xerox_documents').aggregate([
  { "$match": { "isDeleted": true }},
  { "$project": {
    "origFileName": {
      "$concat": [ "c:\DOCS\PRINTERS\PRINTER_VALIDATION_2017\", "$origFileName" ]
    }
  }}
])

不要忘记mongodb脚本与js脚本密切相关。

db.getCollection('xerox_documents').find({"isDeleted": true}).forEach(elem => {
    print(`DEL c:\DOCS\${elem.module}\${elem.documentType}\${elem.origFileName}`);
});

您可以使用 print() 将结果格式化为原始文本。 find()函数returns一个cursor,是一个指向结果查询的指针。将其视为一个迭代器,您可以在其上调用 forEach 函数,它与 js 中的 Array.forEach 具有相同的签名。

可以通过其他两种方式循环 Cursor

  • 手动调用 next() 直到 hasNext() 为真。这是 Iterable 的典型方案。
  • 使用Cursor.toArray()生成一个数组,将所有的结果存储到一个数组中。请注意,因为它们已加载到 RAM 中。

我在 forEach 中作为参数放入的 lambda 函数被解释为成熟的 js 函数。