MongoDb脚本ReferenceError

时间:2019-07-10 20:48:15

标签: mongodb mongodb-query

我已经构建了以下MongoDB脚本来组织我的purchaseorders集合:

db.purchaseorders.find({}).snapshot().forEach(function(purchaseOrder)
{    
    let items = [];
    purchaseOrder.items.map(item => {

        let dataItem = {};
        dataItem.state = item.state;
        dataItem.requestedQuantity = item.requestedQuantity;
        dataItem.startDateTime = purchaseOrder.creationDateTime;

        items.push(dataItem);

        printjson(dataItem);
    });


    let data = {};
    data.code = purchaseOrder.code;
    data.state = purchaseOrder.state;
    data.items = items;

    printjson(data)


    db.purchaseorders.update({
        _id: purchaseOrder._id
    },
    {
        $set: data
    });
});

尝试运行时,出现以下错误:

MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017/testdb
MongoDB server version: 3.6.4
Server has startup warnings:
2019-07-10T06:58:42.825-0300 I CONTROL  [initandlisten]
2019-07-10T06:58:42.826-0300 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-10T06:58:42.826-0300 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-10T06:58:42.826-0300 I CONTROL  [initandlisten]
>
> db.purchaseorders.find({}).snapshot().forEach(function(purchaseOrder)
... {
...     let items = [];
...     purchaseOrder.items.map(item => {
...
...         let dataItem = {};
...         dataItem.state = item.state;
...         dataItem.requestedQuantity = item.requestedQuantity;
...         dataItem.startDateTime = purchaseOrder.creationDateTime;
...
...         items.push(dataItem);
...
...         printjson(dataItem);
...     });
...
...
>     let data = {};
>     data.code = purchaseOrder.code;
2019-07-10T17:44:09.664-0300 E QUERY    [thread1] ReferenceError: purchaseOrder is not defined :
@(shell):1:1
>     data.state = purchaseOrder.state;
2019-07-10T17:44:09.664-0300 E QUERY    [thread1] ReferenceError: purchaseOrder is not defined :
@(shell):1:1
>     data.items = items;
2019-07-10T17:44:09.665-0300 E QUERY    [thread1] ReferenceError: items is not defined :
@(shell):1:1
>
>     printjson(data)
{ }
>
>
>     db.purchaseorders.update({
...         _id: purchaseOrder._id
...     },
...     {
...         $set: data
...     });
2019-07-10T17:44:09.668-0300 E QUERY    [thread1] ReferenceError: purchaseOrder is not defined :
@(shell):2:9
> });
2019-07-10T17:44:09.668-0300 E QUERY    [thread1] SyntaxError: expected expression, got '}' @(shell):1:0
>

找不到我为什么得到这些ReferenceError的原因。帮助表示赞赏。

1 个答案:

答案 0 :(得分:-1)

当您连续键入(或复制粘贴)两个空行时,shell取消输入并返回到提示。您可以看到提示从...回到>的位置。

删除let data = {};db.purchaseorders.update行之前的双空行,然后重试。