MongoError:不能改变_id文档的 [英] MongoError: cannot change _id of a document

查看:143
本文介绍了MongoError:不能改变_id文档的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,MongoDB的骨干,所以我试着去了解他们,但做起来很难。我有一个很大很大的问题:我不明白如何操作在Backbone.Model属性在视图中只使用我需要什么。更具体的 - 我有一个模式

I'm newbie to MongoDB and Backbone, so I try to understand them, but it is hard. I have a big, big problem: I cannot understand how to manipulate attributes in Backbone.Model to use in Views only what I need. More specific - I have a model:

window.User = Backbone.Model.extend({

    urlRoot:"/user",
    idAttribute: "_id",

    defaults: {
        _id: null,
        name: "",
        email: "foo@bar.baz"
    }
});

window.UserCollection = Backbone.Collection.extend({
    model: User,

    url: "user/:id"
});

和我有一个查看

beforeSave: function(){
    var self = this;
    var check = this.model.validateAll();
    if (check.isValid === false) {
        utils.displayValidationErrors(check.messages);
        return false;
    }
    this.saveUser();
    return false;
},

saveUser: function(){
    var self = this;
    console.log('before save');
    this.model.save(null, {
        success: function(model){
            self.render();
            app.navigate('user/' + model.id, false);
            utils.showAlert('Success!', 'User saved successfully', 'alert-success');
        },
        error: function(){
            utils.showAlert('Error', 'An error occurred while trying to save this item', 'alert-error');
        }
    });
}

我必须使用'放'方法白衣数据的任何字段除了'_id',所以它必须像水木清华:

I have to use 'put' method whit data from any fields except '_id', so it must be smth like:

{"name": "Foo", "email": "foo@bar.baz"}

但每一次,不取决于什么,我将它发送

But every time, doesn't depend on what I do it send

{**"_id": "5083e4a7f4c0c4e270000001"**, "name": "Foo", "email": "foo@bar.baz"}

和从服务器这个错误:

* MongoError:不能改变旧文档的_id:{_id:的ObjectId('5083e4a7f4c0c4e270000001'),名称:富}新:{_id:
  5083e4a7f4c0c4e270000001,名称:酒吧,电子邮件:foo@bar.baz} *

*MongoError: cannot change _id of a document old:{ _id: ObjectId('5083e4a7f4c0c4e270000001'), name: "Foo" } new:{ _id: "5083e4a7f4c0c4e270000001", name: "Bar", email: "foo@bar.baz" }*

Github上的链接: https://github.com/pruntoff/habo

Github link: https://github.com/pruntoff/habo

在此先感谢!

推荐答案

从看你蒙戈错误,问题不在于蒙戈,它只是在做它应该做的事。它与的ObjectId类型的_id的对象:的ObjectId('XXX'),现在你要改变这种对象有一个String类型的_id(_id:5083e4a7f4c0c4e270000001)。那显然蒙戈不喜欢

From looking at your mongo error, the problem is not with mongo, it is just doing what it's supposed to do. It had an object with _id of ObjectId type: ObjectId('xxx') and now you're trying to change that object to have an _id of a String type (_id: "5083e4a7f4c0c4e270000001") and that Mongo apparently does not like.

所以,问题是:为什么对象具有类型的ObjectId的摆在首位的ID?你是如何设置它的第一次?如果你使用其他一些方法来初始化它(我猜服务器端),你应该设置ID类型是一个String,它是同一个从你的脚本库的到来。如果你希望它留一个的ObjectId,您将需要转换的字符串从您的脚本即将的ObjectId您保存到蒙戈之前。

So, the question is: why did the object have an id of type ObjectId in the first place? How did you set it the first time? If you used some other method to initialize it (I'm guessing server side), you should set the id type to be a String so that it is the same as the one coming from your script library. If you want it to stay an ObjectId, you will need to convert the String coming from your script to an ObjectId before you save it to Mongo.

心连心。

这篇关于MongoError:不能改变_id文档的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆