Node.js 按ID查找并从MongoDB阵列中删除
我的收藏:Node.js 按ID查找并从MongoDB阵列中删除,node.js,mongodb,mongoose,mongodb-query,Node.js,Mongodb,Mongoose,Mongodb Query,我的收藏: geoGraphicalFilter: { aCountries: [String], aCities: [String], aCoordinates: [{ coordinates: { type: Array } }] } 收集数据 "geoGraphicalFilter": { "aCoordinates": [ { "_id":
geoGraphicalFilter: {
aCountries: [String],
aCities: [String],
aCoordinates: [{
coordinates: { type: Array }
}]
}
收集数据
"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
};
我正在请求的正文中获取对象
,我想从集合中查找文档,并通过在地理过滤器中查找id来删除特定数组元素。a坐标
,并想将其删除。根据的文档,您可以指定值或条件
i、 e
是否要从数组中删除元素,对吗?简单地说?是的,我想从ArrayWhere元素中删除整个元素意味着什么?1-
[72.515202075511979,23.038241551175616]
或仅72.515202075511979
{u id:ObjectId(“5ACB641D93FA0E5257FC6AB”),“坐标”:[[ 72.47451832878957, 23.045350028380867 ],[ 72.50576069939376, 23.04835127278581 ],[ 72.47949650871226, 23.031606634051897 ],[ 72.47451832878957, 23.045350028380867 ] ] }
与ID
关联的整个元素它正在使用foreach循环,但现在我想给数组元素ID来从集合中删除数组。我的意思是删除一个数组需要很多查询,我只想在一个查询中存档,因为可以使用$in子句指定多个_ID
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
huntingModel.update(
{ _id: req.body.id },
{ $pull: { 'geoGraphicalFilter.aCoordinates': {'_id' : ObjectId(itemA.id)}} },
(error, success) => {
if (error) console.log(error);
console.log(success);
}
);