我的收藏:

  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
  };


我在请求的正文中得到了object,我想从一个集合中找到文档,并通过在geoGraphicalFilter.aCoordinates中找到一个id删除特定的数组元素,并希望将其删除。

最佳答案

根据$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);
          }
       );

08-05 12:08