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"
}
]}
任何人都知道我在做什麼錯了?我應該刪除條目並重新創建它嗎?