简体   繁体   English

Expressjs和MongoDB更新集合数组中的对象

[英]Expressjs & MongoDB update object in collection's array

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. 在FeatureCollection中,我具有数组功能,我想在该数组上执行find方法并通过id查找对象,然后在可能的情况下进行推送。

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 在mongodb中,我发现

       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? 因此,它应该研究集合特征并给出所有对象的结果,其中properties.title是Wild Wolfs?

My json 我的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"}}}})

You probably want to use findOneByIdAndUpdate. 您可能要使用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. 就在mongodb中使用数组而言,要将项添加到要使用$ push的数组中并从数组中删除项,需要使用$ pull。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM