mongodb 中的 UpdateMany 使用其他字段的值
UpdateMany in mongodb using value of other field
我在 mongodb 中有此文档:
_id: "xxxx", "时间戳": ISODate("2022-03-26T10:33:47.738Z")
我想创建一个将时间戳复制到 timestamp2 字段的迁移。像这样:
db.task.updateMany(
{ "timestamp2": { $exists: false } },
{ $set: { "timestamp2": $timestamp }}
)
因此,如果文档 1 的时间戳为 2022-03-26T10:33:47.738Z,则其时间戳 2 将相同 (2022-03-26T10:33:47.738Z)。如果文档 2 的时间戳为 2021-03-26T10:33:47.738Z,则其时间戳 2 将相同 (2021-03-26T10:33:47.738Z)
我怎样才能做到这一点?谢谢
这就是我最终使用的:
module.exports = {
async up(db, client) {
await db.collection('task').updateMany(
{ timestamp2: { $exists: false }},
[ { $set: { timestamp2: "$timestamp" } } ]
)
},
async down(db, client) {
// Not possible to rollback updateMany
}
};
我在 mongodb 中有此文档:
_id: "xxxx", "时间戳": ISODate("2022-03-26T10:33:47.738Z")
我想创建一个将时间戳复制到 timestamp2 字段的迁移。像这样:
db.task.updateMany(
{ "timestamp2": { $exists: false } },
{ $set: { "timestamp2": $timestamp }}
)
因此,如果文档 1 的时间戳为 2022-03-26T10:33:47.738Z,则其时间戳 2 将相同 (2022-03-26T10:33:47.738Z)。如果文档 2 的时间戳为 2021-03-26T10:33:47.738Z,则其时间戳 2 将相同 (2021-03-26T10:33:47.738Z) 我怎样才能做到这一点?谢谢
这就是我最终使用的:
module.exports = {
async up(db, client) {
await db.collection('task').updateMany(
{ timestamp2: { $exists: false }},
[ { $set: { timestamp2: "$timestamp" } } ]
)
},
async down(db, client) {
// Not possible to rollback updateMany
}
};