0

I want to update collection's array, I have problems with finding object in collection's array and pushing new values to object, I tried few thing but then It seems I cannot use collection method on array?

router.post('/mountain_rescue_update', function(req, res) {
  var collection = db.collection('rescuemodels');
  var id = req.body.id;

  collection.update({"type": "FeatureCollection"},function (err, doc) {
     if (doc) {
         doc.find({"features": []}, function (err, result) {
             if (err) throw err;

             res.send(result);
         });
     }
   });
    }); 

In FeatureCollection i have array features, i want to do find method on that array and find object by id and then make push if it is possible.

Actually How to find array? so some operations like find and update can be done on that array. I now that expression features: [] looks incorrectly but i don't have idea how to find it.

I tried something like this

collection.find({"features":{"properties":{"date":id}}}, function(err,doc){
     console.log(doc);
  }

If collection has one document which have array features? Shouldn't it work?

In mongodb i found

       db.rescuemodels.find({"features.properties":{"title":"Wild Wolfs"}})

So it should look into collection features and give result of all objects which properties.title is Wild Wolfs?

My json

{
  "_id" : ObjectId("54f50753a879d4e045b24878"),
  "features" : [
     {
         "properties" : {
             "title" : "Alona 45D",
             "description" : "...",
             "date" : ISODate("2015-03-03T01:00:40.842Z"),
             "urgency" : "Low",
             "phone" : "675 675 345",
             "completion" : "NO",
             "rescuer" : "Aleksander Gagarin"
             },
         "geometry" : {
             "coordinates" : [
                 11.2637333,
                 23.1135565
                 ],
             "type" : "Point"
           },
        "type" : "Feature"
        },...etc

      ],
  "type" : "FeatureCollection",
    "__v" : 0
   }

Ok I succeeded with finding object in document's array, So now just replacing some properties left.

 db.rescuemodels.find({"type":"FeatureCollection"},{"features": {$elemMatch:{"properties.title":"W"}}})

Maybe somebody know how to make this statement ok

   db.rescuemodels.update({"type":"FeatureCollection"},{"features":{$elemMatch:{"properties.title":"W"}}},{$set:{"features":{"properties":{"title":"XXX"}}}})

1 Answer 1

1

You probably want to use findOneByIdAndUpdate. In terms of working with Arrays in mongodb, for adding the item to an array you want to use $push and to remove an item from an array, you want to use $pull.

http://docs.mongodb.org/manual/reference/operator/update/push/

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

3 Comments

I asked another question but your thoughts were right first ;) stackoverflow.com/questions/28845053/…
Glad I could help sir and thanks a ton for voting the answer :). Wish you happy coding!
Also, I forgot to say that each time you want to have a unique value in your array, you should use $addToSet instead of push. You will find this feature useful in many cases where you want to avoid duplications in your arrays. Cheers

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.