【问题标题】:Populate none ref objects of a nested array填充嵌套数组的无引用对象
【发布时间】:2020-08-30 20:20:52
【问题描述】:

我正在做一个使用以下项目的项目:

    "@nestjs/core": "^7.0.0",
    "@nestjs/mongoose": "^7.0.0",
    "mongoose": "^5.9.12",
    // ...
    "typescript": "^3.7.4",

使用 mongoose/mongoDB 配置:

      uri: MONGO_DB_URI,
      useUnifiedTopology: true,
      useNewUrlParser: true,
      useFindAndModify: false,
      useCreateIndex: true,

我正在尝试为此模型构建一个简单的 CRUD

export const ContactSchema = new mongoose.Schema(
  {
    source_id: { type: String, required: true },
    firstName: { type: String, trim: true },
    lastName: { type: String, trim: true },
    phones: [
      {
        number: {
          type: String,
          required: true,
          unique: true,
          validate: {
            validator: function(value) {
              const phoneNumber = parsePhoneNumberFromString(value)
              return phoneNumber && phoneNumber.isValid()
            },
          },
        },
        type: {
          type: String,
          default: function() {
            return parsePhoneNumberFromString(this.number).getType() || "N/A"
          },
        },
        code: {
          type: Number,
          default: function() {
            return parsePhoneNumberFromString(this.number).countryCallingCode || undefined
          },
        },
        national: {
          type: Number,
          default: function() {
            return parsePhoneNumberFromString(this.number).nationalNumber || undefined
          },
        },
      },
    ],
    email: { type: String, unique: true, required: true, lowercase: true, trim: true },
  },
  { timestamps: true },
)

ContactSchema.plugin(mongoosePaginate)

像每个 CRUD 应用程序一样,我愿意让 fildAll()fildOne() 路由返回给定 Contact 的正文 他的所有信息,包括其电话号码列表 .所以我用了:

  // ...
  async findAll(): Promise<Contact[]> {
    return this.contactModel.find()
    // then I add
    .populate('phones')
  }

  async findBySourceId(id: string): Promise<Contact> {
    return this.contactModel.findOne({ source_id: id })
    // then I add
    .populate('phones')
  }
  // ...

所有信息都很好地保存在数据库中,并且没有丢失数据(两部手机),而且我确信它甚至可以在不添加 .poplate('x') 的情况下工作,但它在某处发生了变化,它现在返回未填充电话阵列

现在它返回了:

    {
        "_id": "5ebc22072e18637d84bcf6f0",
        "firstName": "Maher",
        "lastName": "Boubakri",
        "phones": [],
        "email": "mhb@test.im",
        // ...
    }

但是,它应该返回:

    {
        "_id": "5ebc22072e18637d84bcf6f0",
        "firstName": "Maher",
        "lastName": "Boubakri",
        "phones": [
            {
                "_id": "5ebc22072e18637d8fd948f9",
                "number": "+21622123456",
                "code": 216,
                "type": "MOBILE",
                "national": 22123456,
            }
        ],
        "email": "mhb@test.im",
        // ...
    }

注意:很明显,MongoDB 会为每个 phone 对象生成_id,但它不是 ref 对象。

任何想法都会很有帮助,

谢谢。

【问题讨论】:

    标签: mongodb typescript mongoose nestjs mongoose-populate


    【解决方案1】:

    populate 用于使用引用连接两个(或更多)集合

    这里你没有任何引用,所以你不需要它

    只使用find() 而不使用populate

    【讨论】:

    • 正如我所说,当我得到空数组时,我刚刚添加了.populate()!那么,您认为可能是什么问题?
    • 你能发布如何调用这些函数findAll和findSource吗?还要检查手机数组是否存在于数据库中
    • phones 对象存在于数据库中,正如我之前所说,所有数据都保存得很好,我调用函数的方式也没有问题!
    • 尝试在.find()this.contactModel.find().lean()之后添加.lean()
    • 在更新mongoose 后添加.lean() 解决了这个问题。只需更新您的答案,提及并告诉我更多有关.lean() 功能的信息,以便我接受它✅。谢谢@Mohammed。
    【解决方案2】:

    根据@Mohammed 的评论和回答,更新mongoose 后添加.lean() 解决了问题。

    // ...
      async findAll(): Promise<Contact[]> {
        return this.contactModel.find().lean()
      }
    
      async findBySourceId(id: string): Promise<Contact> {
        return this.contactModel.findOne({ source_id: id }).lean()
      }
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-23
      • 2017-10-03
      • 2021-05-23
      • 2018-07-05
      • 2017-01-01
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      相关资源
      最近更新 更多