【问题标题】:Using _id from MongoDb as ID in Loopback 4在 Loopback 4 中使用 _id 来自 MongoDb 作为 ID
【发布时间】:2020-07-29 05:21:27
【问题描述】:

我想使用 _id(ObjectId) create 作为 mongoDB 上的唯一键作为我模型中的 id。

(对于一个 sql 数据库,我可以使用数据库的自动增量来控制它)

属性所在的模型:

  @property({
    type: 'number',
    id: true,
    generated: true,
  })
  _id?: number;

但是当我使用 URL 进行查找时,返回:

   {
        "_id": null,
        "name": "sadsadsadasdasdsa",
        "email": "string"
    }

在 mongoDB 中: enter image description here

我如何检索 _id?

【问题讨论】:

  • 我建议您将模型中的type: 'number', 更改为type: 'string',。还将_id?: number; 更改为_id?: string;

标签: mongodb loopbackjs loopback4


【解决方案1】:

ID 属性必须使用type: string:

@model({
  settings: {
    strictObjectIDCoercion: true
}})
export class User extends Entity {
  @property({
    type: 'string',
    id: true,
  })
  id: string;
...
}

进一步阅读

【讨论】:

    猜你喜欢
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多