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

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

问题描述

我是 MongoDB 和 Backbone 的新手,所以我试图理解它们,但这很难.我有一个很大的问题:我无法理解如何操作 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');
        }
    });
}

我必须使用 'put' 方法来处理除 '_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"}

来自服务器的这个错误:

and this error from server:

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

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

提前致谢!

推荐答案

从你的 mongo 错误来看,问题不在于 mongo,它只是在做它应该做的事情.它有一个 _id 为 ObjectId 类型的对象:ObjectId('xxx'),现在您正试图将该对象更改为具有 String 类型的 _id (_id: "5083e4a7f4c0c4e270000001") 而 Mongo 显然不喜欢.

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,则需要将来自脚本的 String 转换为 ObjectId,然后再将其保存到 Mongo.

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天全站免登陆