我是新的MongoDB。 我有一个名为" surveyCollection"的集合。 我使用shell将一些数据插入到集合中,
db.surveyCollection.insert({
"count":30,
"people": [
{
"firstName":"John",
"lastName":"Jason"
}
]
})
db.surveyCollection.insert({
"count":30,
"people": [
{
"firstName":"Sarah",
"lastName":"Smith"
}
]
})
我的数据如下(db.surveyCollection.find()。pretty());
{
"_id" : ObjectId("5873d0c86c3319a579754d00"),
"count" : 30,
"people" : [
{
"firstName" : "John",
"lastName" : "Jason"
}
]
}
{
"_id" : ObjectId("5873d0c86c3319a579754d00"),
"count" : 30,
"people" : [
{
"firstName" : "Sarah",
"lastName" : "Smith"
}
]
}
我不知道如何在任何调查中插入新人。我的意思是,我想拥有如下数据;
{
"_id" : ObjectId("5873d0c86c3319a579754d00"),
"count" : 30,
"people" : [
{
"firstName" : "John",
"lastName" : "Jason"
}
]
}
{
"_id" : ObjectId("5873d0c86c3319a579754d00"),
"count" : 30,
"people" : [
{
"firstName" : "Sarah",
"lastName" : "Smith"
},
{
"firstName" : "George",
"lastName" : "Smith"
}
]
}
我的意思是,如何在JsonArray中达到值?
谢谢
答案 0 :(得分:1)
使用$push方法将项目推送到文档数组中,如下所示:
db.surveyCollection.update(
{ "_id" : ObjectId("5873d0c86c3319a579754d00") },
{ $push: { people: { "firstName" : "George", "lastName" : "Smith"} } }
)
这会将对象{ "firstName" : "George", "lastName" : "Smith"}
添加到people
{"_id" : ObjectId("5873d0c86c3319a579754d00")}