如何为mongo编写shell脚本?

时间:2017-03-27 09:39:35

标签: mongodb shell aggregation-framework

帮帮我, 我需要为mongo编写shell脚本

代码:: mongo data --eval "db.collection1.aggregate({$lookup:{from: "collection2", localField: "id", foreignField: "id", as: "arrCol" }})"

但返回错误 SyntaxError:无效的属性id @(shell eval):1:36

1 个答案:

答案 0 :(得分:1)

脚本片段不应包含双引号。您应该在查询中对字符串使用单引号:

  

mongo data --eval“db.collection1.aggregate({$ lookup:{from:   'collection2',localField:'id',foreignField:'id',as:'arrCol'   }})“

否则var dates= ["Mar 02, 2017", "Mar 02, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017", "Mar 01, 2017"] var arrayObjects = dates.map(function(date) { return {date: date} }) console.log(arrayObjects)将被作为您传递的JavaScript片段插入。