mongo更新查询不适用于旧版本

时间:2020-08-11 13:50:41

标签: mongodb mongodb-query

以下查询在mongo 4.2版上正常运行,但是当我在旧版上运行相同的查询时,这是一个错误-“ ($)中以美元$concat为前缀的字段GUID.$concat对于存储无效”。

db.getCollection('xxxx').update(
    { "SHARE": false },
    [{ 
        "$set": { 
            "GUID": { 
                "$concat": [
                    { $toString: "$ID" }, 
                    { $toString: "$DATE" }
                ] 
            } 
        } 
    }],
    { 
        upsert: false,
        multi: true 
    }
)

有什么方法可以在较早的mongo版本中修改上述查询。

1 个答案:

答案 0 :(得分:0)

下面的查询对我有用,

db.getCollection('xxxx')。aggregate([{$ addFields:{GUID:{$ concat:[{$ toString:“ $ ID”}]}}}])。forEach(function(x){ db.getCollection('xxxx')。update({“ _ id”:x._id},{$ set:{GUID:x.GUID}},{multi:true}); //print((x._id。 toString()))})