繁体   English   中英

MongoDB - $merge 可以在 Compass 中工作,但不能来自 Node.js Lambda

[英]MongoDB - $merge that works in Compass but not from a Node.js Lambda

我有一个在 Compass 中运行良好的聚合管道,但是当我从 NodeJS Lambda 使用它时,它什么也没做。 没有错误,当然也没有连接问题或类似问题,因为我还在使用相同的连接详细信息动态插入数据和创建索引。 为什么这行不通?

Lambda 代码:

// 'db' obtained from connection pooling elsewhere

await db.collection(collectionName).aggregate(aggregationStatement);

聚合声明:

[
    {
        "$match": {
            "type": "costForecast"
        }
    },
    {
        "$sort": {
            "when": 1
        }
    },
    {
        "$group": {
            "_id": {
                "type": "$type"
            },
            "when": {
                "$last": "$when"
            },
            "type": {
                "$last": "$type"
            },
            "serviceId": {
                "$last": "$serviceId"
            },
            "serviceName": {
                "$last": "$serviceName"
            },
            "data": {
                "$last": "$data"
            }
        }
    },
    {
        "$project": {
            "when": 1,
            "type": 1,
            "serviceId": 1,
            "serviceName": 1,
            "data": "$data.ForecastResultsByTime"
        }
    },
    {
        "$merge": {
            "into": "CostsOut",
            "on": [ "type", "serviceId" ],
            "whenMatched": "replace",
            "whenNotMatched": "insert"
        }
    }
]

不用说所有字段都存在,并且 $merge 的目标集合包含支持“on”声明的唯一索引。 我试图实现的function是在原始集合中找到“costForecast”项的最新示例,挑出几个字段并稍微展平数据,然后插入或更新带有文档的“CostsOut”集合“ type”和“serviceId”作为唯一的复合索引。

非常感谢您的帮助。

.aggregate() 不可等待,它只返回 cursor 而不是 promise。

告诉 Mongo,它应该执行聚合,你必须调用 .toArray() 或 .forEach()

await db.collection(collectionName).aggregate(aggregationStatement).toArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM