updateMany给出错误:collection.updateOne需要更新运算符

问题描述 投票:0回答:1

我正在尝试使用updateMany和聚合管道来更新许多记录,但是在尝试运行它时,我一直收到此错误。这个错误让我感到困惑,因为它提到了updateOne,而这不是我正在使用的错误。我想更新一些记录,设置游戏次数,然后根据分数字段的当前值设置低分数和高分数。

我看过updateMany的文档,据我所知,我拥有应有的状态。

[Error] Error: collection.updateOne requires update operator

db.items.updateMany(
    {'game': 'pacman'},
    [
        {$set: {'games_played': 53}},
        {$set: {
            $min: {'score_low': '$score'},
            $max: {'score_high': '$score'}
        }}  
    ]
)

我也尝试了以下操作,并得到相同的错误。

db.items.updateMany(
    {'game': 'pacman'},
    [
        {$set: {'games_played': 53}},
        {$set: {
            'score_low': {$min: {'score_low': '$score'}},
            'score_high': {$max: {'score_high': '$score'}}
        }}  
    ]
)

编辑:

这会产生相同的错误。

db.items.updateMany(
    {'game': 'pacman'},
    [
        {$set: {'games_played': 53}},
        {$set: {
            'score_low': {$min: '$score'},
            'score_high': {$max: '$score'}
        }}  
    ]
)
mongodb
1个回答
0
投票

类似于$min$max的语法问题。您可以尝试将updatemulti:true选项或updateMany结合使用,如下所示:

 db.items.updateMany(
        {'game': 'pacman'},
        [
            {$set: {'games_played': 53}},
            {$set: {
                'score_low': {$min: '$score'}},
                'score_high': {$max: '$score'}}
            }}  
        ])

db.items.update(
    {'game': 'pacman'},
    [
        {$set: {'games_played': 53}},
        {$set: {
            'score_low': {$min: '$score'}},
            'score_high': {$max: '$score'}}
        }}  
    ], {multi:true}
)
© www.soinside.com 2019 - 2024. All rights reserved.