首页 > 解决方案 > 猫鼬查询的行为方式很奇怪

问题描述

我正在使用带有 mongoose 的 MongoDB 作为我的 NodeJS 项目的 ORM 库。这里我放了同一个查询的两个版本,问题是 NoSqlBooster 版本正确返回值,而 mongoose 版本返回空数组,请找到查询的两个版本。

NoSQLBooster 版本:

db.users.find({
  isDeleted: false,
  skills: { '$in': [ ObjectId('613b9951e42a5203c421cbcc'), ObjectId('613b9951e42a5203c421cbcd') ] },
  interests: { '$in': [ ObjectId('613b9951e42a5203c421cbce'), ObjectId('613b9951e42a5203c421cbcf') ] },
  university: { '$in': [ ObjectId('613b9951e42a5203c421cbd0'), ObjectId('613b9951e42a5203c421cbd1') ] },
  title: { '$in': [ ObjectId('613b785b7bacfe1336ab9dbf'), ObjectId('613b9951e42a5203c421cbd2') ] }
});

这工作得很好,但是当我在 mongoose find() 上推送相同的查询时,它返回空数组。请在下面找到猫鼬版本。

{
  isDeleted: false,
  skills: { '$in': [ '613b9951e42a5203c421cbcc', '613b9951e42a5203c421cbcd' ] },
  interest: { '$in': [ '613b9951e42a5203c421cbce', '613b9951e42a5203c421cbcf' ] },
  university: { '$in': [ '613b9951e42a5203c421cbd0', '613b9951e42a5203c421cbd1' ] },
  title: { '$in': [ '613b785b7bacfe1336ab9dbf', '613b9951e42a5203c421cbd2' ] }
}

请注意,上面是我从前端本身得到的请求,我只是在推动它去寻找。

请找到这些特定字段的架构结构,

{
  skills: [ 
    { type: schema.Types.ObjectId, ref: "Skills" }
  ],
  interests: [
    { type: schema.Types.ObjectId, ref: "Interests" }
  ],
    university: [{ type: schema.Types.ObjectId, ref: "University" }],
    title: [{ type: schema.Types.ObjectId, ref: "Titles"}]
}

这是我正在使用的 find() 函数,

if (
        this.req.body &&
        this.req.body.filterParam &&
        !_.isEmpty(this.req.body.filterParam)
      ) {
        makeQuery = JSON.parse(JSON.stringify(this.req.body.filterParam));
      } 

      if(_.isEmpty(this.req.body.searchText) && _.isEmpty(this.req.body.filterParam)) {
        makeQuery = { isDeleted: false };
      }
      console.log(makeQuery);
      let getListOfUsers = await Users.find(makeQuery)
        .sort(sort)
        .skip(skip)
        .limit(perPage)
        .populate("skills", { type: 1 }, { isDeleted: false })
        .populate("interests", { type: 1 }, { isDeleted: false })
        .populate("university", { type: 1 }, { isDeleted: false })
        .populate("business_tags", { type: 1 }, { isDeleted: false })
        .populate("title", { type: 1 }, { isDeleted: false })
        .populate("likesUserManagement", { likedFlag: 1 }, { isDeleted: false, likedFlag: true, createdBy: currentUser })
        .select({
          password: 0,
          status: 0,
          emailVerificationStatus: 0,
          isProfileCompleted: 0,
          isDeleted: 0,
          previouslyUsedPasswords: 0,
          ifForgetOtpVerified: 0,
          portfolio_name: 0,
          portfolio_s3_name: 0,
          image_name: 0,
          createdAt: 0,
          updatedAt: 0,
          __v: 0,
        });

谁能帮我解决这个问题。我被这个困住了。请帮我。谢谢。

标签: javascriptnode.jsmongodbmongoose

解决方案


推荐阅读