我必须使用Mongo shell命令在下面的{"category" : "Vehicle"}
数组中插入一个对象rated
。我怎么能这样做?
{
"rating": {
"userid": 1234,
"bookingid": 4567,
"rated": [
{
"_id": "5aaa356f6b992b2068a1b691",
"category": "Driver",
"comment": "Good",
"rating": 5
},
{
"_id": "5aaa356f6b992b2068a1b690",
"category": "Overall",
"rating": 7
}
]
},
"_id": "5aaa356f6b992b2068a1b68f",
"__v": 0 }
答案 0 :(得分:3)
这很简单。使用$push
方法将对象推送到数组中。
db.ratings.update(
{ _id: ObjectId('5aaa356f6b992b2068a1b68f') },
{ $push: { rated: {"category" : "Vehicle"} } }
)
此处,ratings
是集合名称。
这会将对象插入到额定数组中。
答案 1 :(得分:0)
db.collection.update({
"_id": ObjectId("5aaa356f6b992b2068a1b68f")
}, {
$addToSet: {
'rating.rated': {
"category": "Vehicle"
}
}
}
)