首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongodb...Mongoose更新嵌套集..。为什么这太难了

mongodb...Mongoose更新嵌套集..。为什么这太难了
EN

Stack Overflow用户
提问于 2012-08-29 17:35:29
回答 1查看 1.5K关注 0票数 0

我试着在全球范围内修正一些数据..。为什么我要得到这个error...TypeError:对象函数模型(doc,field,skipId) { if (!(这个模型的实例))r

我得到所有的数据,它应该更新,但它不是。而且误差很大。

代码语言:javascript
复制
UpdateAllThings: function(req, res) {
    things.find().exec( function(err, items) {
        items.forEach(function(th) {
            th.Features.forEach(function(f) {
                thingsfeatures.findOne({'fName': f.fName},function(err, item) {
                    if (item != null ) {
                        try{
                            things.Update({Features: {"$elemMatch":{_id:f._id}}}, {$addToSet: {'Features.$.tType': item.tType, 'Features.$.fGroup': item.fGroup }}, function (err, inventory) {
                                if (err) return handleError(err);
                            });
                        }catch(err){
                            if (err) return handleError(err);
                        }

                    }
                });
            });
        });

    });
     return res.send('thanks please come again');
}

*_________________________*

当然:这或多或少是.

代码语言:javascript
复制
{
  "Features" : [{
      "_id" : ObjectId("5013c9742df16337609b2706"),
      "tID" : "5013c9742df16337609b26f4",
      "tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
      "fName" : "Retail Price:",
      "fValue" : "899.99",
      "_keywords " : ["899.99"]
    }, {
      "_id" : ObjectId("5013c9742df16337609b2707"),
      "_keywords " : ["1", "x", "Proprietary", "LP-E8", "Lithium-ion", "rechargeble"],
      "fGroup" : ["Power"],
      "fName" : "Batteries Included:",
      "fValue" : "1 x Proprietary LP-E8 Lithium-ion rechargeble",
      "tID" : "5013c9742df16337609b26f4",
      "tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
      "tType" : ["Cammera"]
    }],
  "_id" : ObjectId("5013c9742df16337609b26f4"),
  "tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
  "tType" : "Camera"
}

这是可行的,但是它添加了一个包含数据的数组,而不仅仅是我想要的新字段和数据。

代码语言:javascript
复制
db.things.update({"_id":ObjectId("5013c9742df16337609b26f4"), "Features._id" : ObjectId("5013c9742df16337609b2706") }, { $addToSet : { "Features.$.fType" : "Cammera", "Features.$.fGroup"  : "Power" }})
EN

回答 1

Stack Overflow用户

发布于 2012-08-30 18:45:16

发行一个大写。谢谢约翰尼

第二个问题: Addtoset创建数组,而不是将数组添加到它创建新集合的集合中。所以,只需使用set。(令人困惑的文件)

第三,要更新嵌套的set,只需这样做:这比使用元素匹配和其他表单要好得多。

代码语言:javascript
复制
db.things.update({"_id":ObjectId("5013c9742df16337609b26f4"), "Features._id" : ObjectId("5013c9742df16337609b2706") }, { $set : { "Features.$.fType" : "Cammera", "Features.$.fGroup"  : "Power" }})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12183498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档