0
我在previous question上询问了如何在嵌入式MongoDB文档中查找条目。 现在坐在一个新的问题。我的结构是这样的:在MongoDB中更新嵌入式数组
{
"_id" : ObjectId("5260c4bd95f8e3ad08000000"),
"ip" : "127.0.0.1",
"services" : [
{
"port" : "22"
}
]}
我想添加一个新的键/值对services.port其中端口是22,所以它看起来是这样的:
{
"_id" : ObjectId("5260c4bd95f8e3ad08000000"),
"ip" : "127.0.0.1",
"services" : [
{
"port" : "22",
"product" : "ssh"
}
]}
这是更新我运行:
db.hosts.update({ "ip" : "127.0.0.1" }, { $addToSet : { services : { "port" : "22", "product" : "ssh" }}});
而这就是我得到
{
"_id" : ObjectId("5260c4bd95f8e3ad08000000"),
"ip" : "127.0.0.1",
"services" : [
{
"port" : "22"
},
{
"port" : "22",
"product" : "ssh"
}
]}
任何人都知道我在做什么错了?我应该删除条目并重新创建它吗?