【问题标题】:Mongo: new is not getting the latest documentMongo:新的没有得到最新的文件
【发布时间】:2021-06-12 08:16:43
【问题描述】:

我遇到了一个问题,即我的代码没有返回最新的文档,即使我指定了new: true 并且我也尝试了returnOriginal:false. 知道我在这里做错了什么吗?我正在使用$addToSet,这可能会搞砸一些事情?

  async addFlagsToFeature(_id: number, flagDtos: FlagDto[]): Promise<FeatureResponse> {
    return this.featureModel
      .findOneAndUpdate({ new: true , $addToSet: { flags: flagDtos }})
      .then((doc) => {
        const featureDto = new FeatureDto();
        featureDto.mapFromSchema(doc);

        featureDto.flags.forEach((item) => {
          flagIds.push(new FlagDto({ featureFlagId: item.featureFlagId }));
        });
        success = new SuccessFeatureResponseDto(featureDto.id, flagIds);
        return new FeatureResponse(success, null);
      })
      .catch((error) => {
        this.logger.error('SystemError', {}, error);
        throw new ExceptionHandler(500, 'SystemError', error);
      });
  }

【问题讨论】:

    标签: typescript mongodb mongoose


    【解决方案1】:

    您的过滤器查询中似乎设置了 {new: true}。试试这样的:

    return this.featureModel
      .findOneAndUpdate({_id: _id}, { $addToSet: { flags: flagDtos }}, {new : true})
      .then((doc) => {
        console.log(doc)
      }
    

    https://mongoosejs.com/docs/tutorials/findoneandupdate.html

    【讨论】:

      猜你喜欢
      • 2016-08-11
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多