我需要通过在Bid中插入出价来更新文档,该出价数组是出价的一部分。下面是一个示例文档:
{
"_id" : "1044300051",
"Bids" : {
"Bid" : [
{
"Bidder" : {
"_id" : "pickford25",
"_Rating" : 255,
"Location" : "ANIME PARADISE",
"Country" : "USA"
},
"Time" : "Dec-07-01 15:02:54",
"Amount" : 1.12
},
{
"Bidder" : {
"_id" : "arlnwtkwsk@aol.com",
"_Rating" : 61,
"Location" : "ARCADE, NEW YORK",
"Country" : "USA"
},
"Time" : "Dec-09-01 15:02:54",
"Amount" : 1.25
}
]
}
}
以下是我尝试的一些查询:
db.items.update({_id: "1678348584"}, {$set: {Bids: "Bid[]"}},{$push: {"Bids.Bid": {"Amount":1000}}});
db.items.update({_id: "1678348584"}, {$push: {"Bids": [{"Amount":1000}]}});
我想在上面的例子中更新的特定_id有"出价"最初设置为null
答案 0 :(得分:1)
你快到了。要访问嵌套数组,您必须使用Bids.Bid
。您的查询应该是
db.items.update({_id: "1678348584"}, {$push: {"Bids.Bid": {"Amount":1000}}});