代码之家  ›  专栏  ›  技术社区  ›  Anjil Dhamala

根据结果是否具有特定属性列出结果

  •  0
  • Anjil Dhamala  · 技术社区  · 7 年前

    当我列出数据库中的所有用户时,这里有两种响应。

    {
    _id: "5bcf4f4d48a3067897c22344",
    __v: 0
    },
    {
    _id: "5bcf507b65f77778ab23b53f",
    firstname: "major",
    lastname: "general",
    __v: 0
    }
    

    async deleteUsersWithoutNames() {
        const usersWithNoName = await this.userModel.find()
            .where((response: any) => response.hasOwnProperty('firstname')).equals(false);
        return usersWithNoName;
      }
    

    path must be a string or object .

    这是userModel初始化。

      private userModel = new UserEntity().getModelForClass(UserEntity);
    

    UserEntity类扩展 Typegoose 通过 Typegoose .

    1 回复  |  直到 7 年前
        1
  •  1
  •   Andrei    7 年前

    现在我想这是一个更好的方法来解决你的问题

    https://docs.mongodb.com/manual/reference/operator/query/exists/

    async deleteUsersWithoutNames() {
        return await this.userModel.where({firstname: {$exists:false}})       
    }
    
    推荐文章