我有以下模型架构:
{
_id: foo
cart: {
items: [
{
id: number
name: string,
}
]
}
}
我想按其ID查找文档,然后更新具有另一个指定ID的name
数组中对象的items
值。
我已经尝试过了,但是没有用:
Order.findOneAndUpdate(
{
"_id": orderId,
"cart.items": {
$elemMatch: {
id: itemId,
}
},
},
{
$set: {
"cart.items.$.name": name,
},
},
);
此查询找到正确的文档,但没有正确更新。正确的方法是什么?
答案 0 :(得分:0)
@objc func startRedBalloon() {
let xOrigin = arc4random_uniform(208) + 1
let xEnding = arc4random_uniform(208) + 1
//red
redBalloon.isHidden = false
redBalloon.image = UIImage(named: "redBalloon")
redBalloon.contentMode = .scaleAspectFit
redBalloon.frame = CGRect(x: Int(xOrigin), y: 667, width: Int(redBalloon.frame.size.width), height: Int(redBalloon.frame.size.height))
animRed.addAnimations {
self.redBalloon.frame = CGRect(x: Int(xEnding), y: -116, width: 100, height: 116)
}
animRed.addCompletion { _ in
self.redEndGame()
}
let imageTapRed = UITapGestureRecognizer(target: self, action: #selector(imageTappedRed))
redBalloon.isUserInteractionEnabled = true
redBalloon.addGestureRecognizer(imageTapRed)
animRed.startAnimation()
}
或
const order = await Order.findOneAndUpdate({
"_id": orderId,
"cart.items.id": itemId,
},
{
$set: {
"cart.items.$.printUrl": printUrl,
},
},
});