【问题标题】:Why does Mongodb create an array with an ObjectsId instead of a simple array of objects?为什么 Mongodb 创建一个带有 ObjectsId 的数组而不是一个简单的对象数组?
【发布时间】:2021-12-21 15:53:44
【问题描述】:

我有一个包含对象数组的文档。当我创建一个新文档时,对象数组是使用 ObjectsId 创建的。

这正常吗? 我需要指定一些东西来避免这种情况吗?

猫鼬选项:

mongoose.connect(config.mongoUrl, {
useNewUrlParser: true,
useCreateIndex: false,
useUnifiedTopology: true,
useFindAndModify: false,

架构:

...    
tags: [{
        name: { type: String },
        experience: { type: Number },
      }],

graphql 类型定义:

type _tag {
    name: String
    experience: Int
  }
...

type Freelance implements IObjetoWerk {
...
    tags: [_tag]
...
}

graphql 解析器:

`...
const newObjetoWerk = new werkModels.ObjetoWerk(input);
await newObjetoWerk.save();
console.log(newObjetoWerk);
return newObjetoWerk;
...`

graphiQL 查询变量:

...
"tags": [
  {
    "name": "Clown",
    "experience": 2
  }
],

结果:

enter image description here

【问题讨论】:

  • id 或 _id(或任何唯一键)是 graphql 客户端(如 apollo)中的缓存所必需的

标签: mongodb mongoose graphql


【解决方案1】:

据我了解,您想删除添加在子文档中的 ObjectId,因此您应该在架构定义中指定它

...
tags: [{
        _id: false,
        name: { type: String },
        experience: { type: Number },
}],
...

还有什么要告诉我的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多