0
數組的數組這是我的文檔結構:如何刪除MongoDB中
{
"_id" : ObjectId("5841eba5d3693034c49da7d7"),
"Key" : "01041655",
"matchingKey" : "01041655",
"brand" : "Brand",
"name" : "Product Name",
"availabilities" : [
{
"data1" : [
{
"number_a" : null,
"number_b" : "4.5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
},
{
"number_a" : null,
"number_b" : "5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
},
{
"number_a" : null,
"number_b" : "5.5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
}
]
},
{
"data2" : [
{
"number_a" : null,
"number_b" : "4.5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
},
{
"number_a" : null,
"number_b" : "5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
}
]
}
]
}
林不知道,如果它要那樣做的最好辦法,所以請讓我知道,如果有一個更好的方法來做到這一點。我的問題是:如何刪除數組data1或data2。在Java中做什麼是最好的方法。我目前的Java代碼如下所示:
Document array = new Document("availabilities", dataKey);
Document updateSet = new Document("$unset", array);
prodCollection.updateMany(eq("matchingKey", matchingKey), updateSet);
但是,此代碼會刪除可用性中的所有文檔。
UPDATE1: 我改變了文檔結構類似:
{
"_id" : ObjectId("5841eba5d3693034c49da7d7"),
"Key" : "01041655",
"matchingKey" : "01041655",
"brand" : "Brand",
"name" : "Product Name",
"availabilities" : {
"data1" : [
{
"number_a" : null,
"number_b" : "4.5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
},
{
"number_a" : null,
"number_b" : "5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
},
{
"number_a" : null,
"number_b" : "5.5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
}
],
"data2" : [
{
"number_a" : null,
"number_b" : "4.5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
},
{
"number_a" : null,
"number_b" : "5",
"number_c" : null,
"number_d" : null,
"amount" : 0,
"lastCheck" : "2016-12-02T22:46:13.393+01:00"
}
]
}
}
現在我可以在這樣的MongoDB特定陣列刪除:
db.products.update({"matchingKey":'01041655'},{$unset: {"availabilities.data1":1}});
我如何能做到這一點在Java中?我試過這樣:
Document array = new Document("availabilities.data1", 1);
Document updateSet = new Document("$unset", array);
prodCollection.updateOne(eq("matchingKey", matchingKey), updateSet);
但它仍然刪除所有數組。
刪除可用性數組內部值的標準是什麼? – Veeram
名稱data1和data2。我必須將它們設置爲名稱嗎? – jimb0p