MongoError: cannot change _id of a document

后端 未结 2 467
借酒劲吻你
借酒劲吻你 2020-12-28 18:13

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.Mod

2条回答
  •  醉话见心
    2020-12-28 19:12

    MongoDB creates _id as an ObjectID, but doesn't retrieve _id as an ObjectID.

    Whether this inconsistency is 'correct behaviour' or not, it is certainly an annoying surprise for most MongoDB users.

    You can fix it with:

    if ( this._id && ( typeof(this._id) === 'string' ) ) {
      log('Fixing id')
      this._id = mongodb.ObjectID.createFromHexString(this._id)
    }
    

    See MongoDB can't update document because _id is string, not ObjectId

提交回复
热议问题