Cha*_*mar 26 mongodb mongo-shell
我正在学习 MongoDB 课程。以下是我在名为“flightData”的集合中的文档列表。下面是find查询的结果:
flights> db.flightData.find()
[
{
_id: ObjectId("611aaa1c4a0269583c8d81b0"),
aircraft: 'AirBus A308',
departingFrom: 'Mumbai',
arrivalStation: 'Moscow',
departureDate: '24/09/2021',
departureTime: '01:44',
arrivingAt: '12:00',
isOneWay: false,
status: {
description: 'on time',
lastUpdated: 'One hour ago',
details: { contact: 'John Doe' }
}
},
{
_id: ObjectId("611aaa554a0269583c8d81b1"),
aircraft: 'AirBus A308',
departingFrom: 'Kolkata',
arrivalStation: 'Stockholm',
departureDate: '24/09/2021',
departureTime: '01:44',
arrivingAt: '12:00',
isOneWay: false,
status: {
description: 'on time',
lastUpdated: 'One hour ago',
details: { contact: 'Cool User' }
}
}
]
Run Code Online (Sandbox Code Playgroud)
当他们通过类似于下面的示例展示 update 和 updateMany 之间的区别时:
flights> db.flightData.update({_id:ObjectId("611aaa554a0269583c8d81b1")},{"delayed":false})
Run Code Online (Sandbox Code Playgroud)
在讲座中它有效。但是,就我而言,它会抛出以下错误:
MongoInvalidArgumentError:更新文档需要原子运算符
有人可以解释一下这种行为吗?是我的版本不支持还是其他版本不支持?
我正在使用 MongoDB 5.0.2、mongosh 1.0.5
I.B*_*air 37
如果您想将“延迟”字段添加到文档中,您将需要使用 $set 运算符
db.flightData.update({_id:ObjectId("611aaa554a0269583c8d81b1")},{$set:{"delayed":false}})
Run Code Online (Sandbox Code Playgroud)
如果您想替换文档,您应该使用replaceOne
该命令是在 mongodb 3.2 中添加的,以避免在更新字段时意外替换整个文档
归档时间: |
|
查看次数: |
38479 次 |
最近记录: |