我有以下json结构:
{
"_id" : ObjectId("5203af83396d285ea2ecff8f"),
"brand" : "LG",
"comments" : [{
"user_id" : ObjectId("521b2785eda03d0f9cab3566"),
"text" : "Nice TV"
}],
"model" : "47LS5600",
"price" : 499.0,
"thumbnail" : "lg-47LS5600"
}
我需要在数组“comments”中插入一个新字段“datetime”,如下所示:
{
"_id" : ObjectId("5203af83396d285ea2ecff8f"),
"brand" : "LG",
"comments" : [{
"user_id" : ObjectId("521b2785eda03d0f9cab3566"),
"text" : "Nice TV",
"datetime": <value>
}],
"model" : "47LS5600",
"price" : 499.0,
"thumbnail" : "lg-47LS5600"
}
我尝试了以下指令:
db.tvs.update({ _id: ObjectId("5203af83396d285ea2ecff8f") }, { $addToSet: { "comments.1": { "datetime": Date() } } } )
但它不会在项目中插入字段,它会创建另一个与该项目分开的对象