我正在 MongoDb 中尝试使用“updateMany”语句来更新此表中的行,并收到“意外令牌”消息。我在这里做错了什么?

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

这是 MongoDb 表: 'email_status' collection

这是我使用“updateMany”命令的尝试:

db.getCollection('email_status').updateMany({
   SentOn: {
       $gte: ISODate("2010-01-01T00:00:00.000Z"),
       $lt: ISODate("2023-10-31T00:00:00.000Z")
    }, 
    "EmailMessage.ReplyAddress.Address": {$eq: "[email protected]"},
    $set: {EmailMessage.ReplyAddress.Address: "[email protected]", EmailMessage.ReplyAddress.DisplayName: "My Name"}
    })

我尝试以多种不同的方式修改 $set: 语句,但仍然没有得到有效的更新语句。

这是我在上面的代码中遇到的错误:

Error: Line 7: Unexpected token .
mongodb mongodb-query syntax-error
1个回答
0
投票
  • 如果字段名称包含点 (.),则必须将其括在引号中

  • $eq
    不需要(可以用,但是多写)

  • 您错过了一个大括号来分隔 filterupdate:

    db.collection.updateMany(filter, update, options)

会是这样的:

db.getCollection('email_status').updateMany(
   {
      SentOn: {
         $gte: ISODate("2010-01-01T00:00:00.000Z"),
         $lt: ISODate("2023-10-31T00:00:00.000Z")
      },
      "EmailMessage.ReplyAddress.Address": "[email protected]"
   },
   {
      $set: {
         "EmailMessage.ReplyAddress.Address": "[email protected]",
         "EmailMessage.ReplyAddress.DisplayName": "My Name"
      }
   }
)
© www.soinside.com 2019 - 2024. All rights reserved.