我是MongoDB的新手,我尝试用来自bidder的bid amount的值来更新bidder值,同时向JSON添加一个投标人数组。这个想法是更新"Current bid price"与出价金额时,一个投标人被添加。
我的json对象没有投标人数组,但是当它与投标人更新时,当前的出价应该用出价额更新。
{
"_id" : ObjectId("62266e4feb82220b6ccbe5a3"),
"user" : 10,
"name" : "Panamera",
"category" : "Porche",
"description" : "Exclusively Fast",
"closing date" : "12/10/22",
"starting bid price" : "1250.00",
"Current bid price" : "0.00",
"auction status" : "open"
}投标人
bidder: [
{
"user": "Bob Marley",
"date and time": ISODate("2022-03-11T00:00:00Z"),
"bid amount": "450.00",
}
]当我尝试运行更新代码时,它只更新当前的出价,它不会将投标人数组添加到json
db.auction.update({"name": "Panamera"},
{$set: {"Current bid price": "450.00"}},
{$push: {"bidder":{ "user": "Bob Marley","date and time": ISODate("2022-03-11T00:00:00Z"),"bid amount": "450.00"}}})在上面的代码中使用set和push函数,这就是我想要实现的,但是我不能用一个代码来实现它。
{
"_id" : ObjectId("62266e4feb82220b6ccbe5a3"),
"user" : 10,
"name" : "Panamera",
"category" : "Porche",
"description" : "Exclusively Fast",
"closing date" : "12/10/22",
"starting bid price" : "1250.00",
"Current bid price" : "450.00",
"auction status" : "open",
"bidder" : [
{
"user" : "Bob Marley",
"date and time" : ISODate("2022-03-11T00:00:00Z"),
"bid amount" : "450.00"
}
]
}发布于 2022-03-07 21:21:34
我已经看到了这个问题,我需要用布景来包装一下。谢谢
db.auction.update({"name": "Panamera"},
{$push: {"bidder":{ "user": "Bob Marley","date and time":
ISODate("2022-03-11T00:00:00Z"),"bid amount": "450.00"}},
$set: {"Current bid price": "450.00"}})https://stackoverflow.com/questions/71387269
复制相似问题