0

I Have Document Like,

{
    "_id" : ObjectId("5ab4cc12c773133bae3d8dc9"),
    "__v" : 19.0,
    "geoGraphicalFilter" : {
        "aCountries" : [
            {
                "country" : "Spain",
                "_id" : ObjectId("5ad49ab21210c42aa6ccba23"),
                "cities" : [ ]
            },
            {
                "country" : "India",
                "_id" : ObjectId("5ad49ab21210c42aa6ccba24"),
                "cities" : [ ]
            }
        ]
    }
}

Model

const HuntingWindow = new Schema({
  geoGraphicalFilter: {
    aCountries: [
      {
        country: String,
        cities: { type: Array }
      }
    ]
  },
});

Snippet

const addCityFilter = (req, res) => {
  if (req.body.aCities === "") {
    res.status(409).jsonp({ message: adminMessages.err_fill_val_properly });
    return false;
  } else {
    var Cities = req.body.aCities.split(",");
    huntingModel.update({ _id: req.body.id,"data.geoGraphicalFilter.aCountries":req.body.sName },
    {$push:{"geoGraphicalFilter.0.aCountries.$.cities":Cities}},
    (error, data) => {       
        // Cities.forEach(element => {
        //   data.geoGraphicalFilter.aCountries.find(req.body.sName)
        // });
      data.save((error, success) => {
        res.status(200).jsonp({
          message: adminMessages.succ_countryFilter_added
        });
      });
    });
  }
};

Now I want to first Find Document by root id and then I want to match the country name and insert the cities in the Array. I am new to MongoDB and nodejs how can i do this ? While i am trying to with update query but i thing i am doing it on wrong way plese help me with it.

1 Answer 1

3

Try following code:

huntingModel.update(
    { _id: ObjectId("5ab4cc12c773133bae3d8dc9"), "geoGraphicalFilter.aCountries.country": "Spain"  },
    { $addToSet: { "geoGraphicalFilter.aCountries.$.cities": { $each: [ "city1", "city2" ] } } }
)

You should specify two filtering conditions: one for entire document and one to match array element. Then you can use $ operator to update first matching document in that array. To push multiple values you can use $each operator. To ensure that there will be no duplicates you should use $addToSet

Sign up to request clarification or add additional context in comments.

4 Comments

but while adding the second time it should not accept a duplicate value
@NikhilSavaliya you need $addToSet instead of push. Modified my answer
still having one doubt what if the "geoGraphicalFilter.aCountries.country": "Spain" Does not exist, Positional operator will Throw Error that time
Yes, that's true. So you have to handle that error and push entire country probably. More here: stackoverflow.com/questions/8871363/…

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.