MongoError:无法更改文档的_id [英] MongoError: cannot change _id of a document

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

问题描述

我是MongoDB和Backbone的新手,所以我试着去了解他们,但很难。我有一个很大的大问题:我无法理解如何操纵Backbone.Model中的属性,仅在视图中使用我需要的。更具体 - 我有一个模型

  window.User = Backbone.Model.extend 

urlRoot:/ user,
idAttribute:_id,

默认值:{
_id:null,
name: ,
电子邮件: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);
返回false;
}
this.saveUser();
返回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','尝试保存此项时发生错误','alert-error');
}
});
}

我必须使用除'_id之外的任何字段的'put'方法白名单数据',所以它必须是这样的:

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

但每次,不依赖于我所做的发送

  {**_ id:5083e4a7f4c0c4e270000001**,name:Foo,email:foo @ bar .baz} 

,服务器发生此错误:


$ b $ MongoError:无法更改文档的_id:旧的:{_id:ObjectId('5083e4a7f4c0c4e270000001'),name:Foo} new:{_id:
5083e4a7f4c0c4e270000001,名称:Bar,电子邮件:foo@bar.baz}


Github链接: a href =https://github.com/pruntoff/habo =noreferrer> https://github.com/pruntoff/habo



提前感谢!

解决方案

从看你的mongo错误,问题不在于m ongo,它只是做它应该做的事情。它有一个Objectid类型为_id的对象:ObjectId('xxx'),现在您正在尝试将该对象更改为具有String类型(_id:5083e4a7f4c0c4e270000001)的_id,并且Mongo显然不喜欢。 / p>

所以问题是:为什么对象首先有一个类型为ObjectId的id?你是怎么设置的?如果您使用其他方法来初始化它(我猜测服务器端),那么您应该将id类型设置为String,以使其与脚本库中的相同。如果你希望它保留一个ObjectId,那么在将它保存到Mongo之前,您需要将脚本中的String转换为ObjectId。



HTH。


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"
});

And I have a View:

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');
        }
    });
}

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"}

and this error from server:

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

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

Thanks in advance!

解决方案

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.

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.

HTH.

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

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