Mongoose/Mongodb: Exclude fields from populated query data

后端 未结 4 660
灰色年华
灰色年华 2020-12-31 03:31

I use the following mongoose query in a MEAN-environment to find and output a particular author and his corresponding books.

Author
.findOne({personcode: co         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 03:37

    To exclude individually

    User.findOne({_id: userId}).select("-password")
    

    To exclude using the schema

    var userSchema = mongoose.Schema({
      email: {
        type: String,
        required: true,
        unique: true,
      },
      password: {
        type: String,
        required: true,
        select: false,
      },
    });
    

    or this will also work

    db.collection.find({},{"field_req" : 1,"field_exclude":0});
    

提交回复
热议问题