jugglingdb - 设置主键字段

时间:2013-09-21 06:42:30

标签: jugglingdb

是否可以设置哪个字段应为“id”字段?我用以下方式定义我的方案:

var Person = app.ormDb.define('person', {
    id          : { type: String, index: true, default: function () { return uuid.v4(); } },
    oAuthID     : { type: String, index: true },
    name        : { type: String },
    gender      : { type: String },
    birth       : { type: Date },
    email       : { type: String },
    imageID     : { type: String },
    favorites   : { type: JSON, default: function() { return {cars : [], animals : []}; } },
    date        : { type: Date, default: function() { return new Date(); } },
    updated     : { type: Date, default: function() { return new Date(); } }
});

和我定义的id字段显示在MongoDB中但是当我使用jugglingdb查找某个人时,Person.id的返回值是MongoDB _id ObjectId值。所以隐藏了我的id

1 个答案:

答案 0 :(得分:0)

_id在MongoDB中保留为主键。这可以解释为什么它不能改变。只要它是唯一的,您可以将任何值放入其中。希望有所帮助。

jugglingdb-mongodb适配器中当前还有open issue。这解决了未返回id的原因。基本上,如果在调用create时存在object.id,则在插入集合之前将其删除。