我的收藏:
geoGraphicalFilter: {
aCountries: [String],
aCities: [String],
aCoordinates: [{
coordinates: { type: Array }
}]
}
CollectionData
"geoGraphicalFilter": {
"aCoordinates": [
{
"_id": ObjectId("5acb641d93fa0e52557fc6aa"),
"coordinates": [
[
72.42919972527011,
23.0437703991947
],
[
72.45031407464302,
23.045823913521474
],
[
72.43263295281557,
23.030500782775746
],
[
72.42919972527011,
23.0437703991947
]
]
},
{
"_id": ObjectId("5acb641d93fa0e52557fc6ac"),
"coordinates": [
[
72.51520207511979,
23.038241551175616
],
[
72.55399754632015,
23.03934733892872
],
[
72.51812031852671,
23.025129376064214
],
[
72.51520207511979,
23.038241551175616
]
]
},
{
"_id": ObjectId("5acb641d93fa0e52557fc6ad"),
"coordinates": [
[
72.44653752434493,
23.02828905299478
],
[
72.4896245299627,
23.02828905299478
],
[
72.45477727044641,
23.0194417709901
],
[
72.44653752434493,
23.02828905299478
]
]
},
{
"_id": ObjectId("5acb641d93fa0e52557fc6ab"),
"coordinates": [
[
72.47451832878957,
23.045350028380867
],
[
72.50576069939376,
23.04835127278581
],
[
72.47949650871226,
23.031606634051897
],
[
72.47451832878957,
23.045350028380867
]
]
}
],
"aCities": [],
"aCountries": []
}
从数据库代码段中删除
const deleteZones = (req,res,next) => {
var body = _.pick(req.body, ["zones"]);
var zoneList = body.zones;
debug(zoneList)
var promise = function() {
return new Promise(function(resolve, reject) {
zoneList.forEach(itemA => {
console.log(itemA.coordinates)
huntingModel.update(
{ _id: req.body.id },
{ $pull: { 'geoGraphicalFilter.aCoordinates': itemA.id} },
(error, success) => {
if (error) console.log(error);
console.log(success);
}
);
});
resolve();
});
};
promise().then(function() {
return res.status(200).jsonp({
message: adminMessages.succ_zone_removed
});
});
}
现在的情况就像我试图删除数据时显示成功消息,但数据不会被删除。
var object = {
id:this.id,
zones: this.zonesDelete // Contains list of id
};
我在请求的正文中得到object
,我想从集合中找到该文档,并通过在geoGraphicalFilter.aCoordinates
中找到一个ID来删除特定的数组元素,并希望将其删除。
答案 0 :(得分:1)
根据$pull operator的文档,您可以指定值或条件
即
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
在您的方案中,您需要指定一个或多个 aCoordinates 项目对象的完整值或与一个或多个 aCoordinates 项目匹配的条件
添加符合aCoordinates项目id的条件,即
使用以下拉动条件来解决问题:
huntingModel.update(
{ _id: req.body.id },
{ $pull: { 'geoGraphicalFilter.aCoordinates': {'_id' : ObjectId(itemA.id)}} },
(error, success) => {
if (error) console.log(error);
console.log(success);
}
);