1

My Collection:

  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": []
    }

Remove From Database Snippet

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

Now the scenario is like when I am trying to delete data it shows success message but data does not get deleted.

  var object = {
    id:this.id,
    zones: this.zonesDelete // Contains list of id
  };

I am getting object in a requested body and I want to find the document from a collection and delete the particular array element by finding an id in geoGraphicalFilter.aCoordinates and wants to remove it.

4
  • you want to remove an element from the array right ? in simple words ? Commented Apr 9, 2018 at 13:39
  • Yes I want to remove Whole element from array Commented Apr 9, 2018 at 13:42
  • whole element mean ? 1- [ 72.51520207511979, 23.038241551175616 ] or just 72.51520207511979 Commented Apr 9, 2018 at 13:44
  • { "_id": ObjectId("5acb641d93fa0e52557fc6ab"), "coordinates": [ [ 72.47451832878957, 23.045350028380867 ],[ 72.50576069939376, 23.04835127278581 ],[ 72.47949650871226, 23.031606634051897 ],[ 72.47451832878957, 23.045350028380867 ] ] } Whole elemnet associated with ID Commented Apr 9, 2018 at 15:17

1 Answer 1

2

As per documentation of $pull operator you can either specify a value or a condition

i.e.

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

In your scenario you need to either specify complete value of one or more aCoordinates item object or an condition that matches one or more aCoordinates item

Add the condition where you match id of aCoordinates item i.e.

Use following pull condition to solve the issue:

        huntingModel.update(
          { _id: req.body.id },
          { $pull: { 'geoGraphicalFilter.aCoordinates':  {'_id' : ObjectId(itemA.id)}} },
          (error, success) => {
            if (error) console.log(error);
            console.log(success);
          }
       );
Sign up to request clarification or add additional context in comments.

2 Comments

it is working with foreach loop but now I want to give array element of id to delete the Array from a collection. What I meant is it will take a lot of queries to delete one-one arrays, I want to archive in only one Query
Use can use $in clause to specify multiple _ids

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.