I'm having trouble updating an array within a collection. I can't seem to find the object in the array and add new values to it. I've tried a few methods, but it looks like I can't use collection methods on arrays?
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 the FeatureCollection, there is an array called features. I want to perform a find operation on that array, locate the object by its id, and then push new data if possible.
How do I find an array so that operations like find and update can be performed? I know that the expression features: [] seems incorrect, but I don't know how to locate it.
I tried something like this
collection.find({"features":{"properties":{"date":id}}}, function(err,doc){
console.log(doc);
}
If a collection contains a document with an array of features, shouldn't this query work?
In MongoDB, I found
db.rescuemodels.find({"features.properties":{"title":"Wild Wolfs"}})
This should search within the collection features and return all objects where properties.title is Wild Wolfs?
Here is 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
}
I managed to locate the object in the document's array. Now, I just need to update some properties.
db.rescuemodels.find({"type":"FeatureCollection"},{"features": {$elemMatch:{"properties.title":"W"}}})
If anyone knows how to fix this statement, please let me know
db.rescuemodels.update({"type":"FeatureCollection"},{"features":{$elemMatch:{"properties.title":"W"}}},{$set:{"features":{"properties":{"title":"XXX"}}}})