【问题标题】:jugglingdb - Setting the primary key fieldjugglingdb - 设置主键字段
【发布时间】:2013-09-21 06:42:30
【问题描述】:

是否可以设置哪个字段应该是“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 被隐藏了。

【问题讨论】:

    标签: jugglingdb


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多