【问题标题】:remove all from subdoocument with mongoose/mongodb使用 mongoose/mongodb 从子文档中删除所有内容
【发布时间】:2017-09-28 02:39:50
【问题描述】:

我有一个收藏:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
ObjectId = Schema.ObjectId;
var User = new Schema({
    id: ObjectId,
    name: String,
    positionsApplied:[{
            position_id:ObjectId,
            index_position: Number
    }],
})

我需要做的是使用 mongoose 删除 positionApplied 子集合中存在的所有文档。 我不确定我哪里出错了:

app.post('/deleteAll', function(req, res){
  User.find(req.user._id, // this is the object Id which matches the logged in user
        {$unwind : "$positionsApplied"}).remove().exec(function (err, result) {
          console.log(result);
          res.send({results:result});

        });

});

更新: 架构:

从 remove() 返回:

【问题讨论】:

    标签: mongodb mongoose subdocument


    【解决方案1】:

    架构

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    ObjectId = Schema.ObjectId;
    var PositionApplied = new Schema({
        position_id:ObjectId,
        index_position: Number
    }, { _id : false });
    
    var User = new Schema({
        _id: ObjectId,
        name: String,
        random_index_number:Number,
        companyname: String,
        password: String,
        email: String,
        username: String,
        location: String,
        role:String,
        teamwork:Number,
        initiative:Number,
        technical_skills: Number,
        communication:Number,
        employees: String,
        profile_image_link: String,
        video_url: String,
        biodescription: String,
        twitter: String,
        facebook: String,
        instagram: String,
        linkedin: String,
        google: String,
        biodescription: String,
        positionsApplied:[PositionApplied],
        experience: [{
              title: String,
              location: String,
              company: String,
              start: String,
              end:String,
              description:String
      }],
        position: [{
              _id:String,
              title: String,
              location: String,
              start: String,
              term:Number,
              description:String,
              teamwork_value:Number,
              communication_value:Number,
              initiative_value:Number,
              pos_index_number:Number,
                preference:[{
                            candidate_id: String,
                            weightedScore:Number,
                            position_id:String,
                            candidate_index_number:Number
                        }],
                        date: {type: Date, default: Date.now},
                            applied:[{
                                    candidate_id: ObjectId,
                                    date: {type: Date, default: Date.now},
                                    profile_image: String,
                                    location: String,
                                    name: String,
                                    _id:ObjectId
                            }],
      }],
      education: [{
              school: String,
              location: String,
              degree: String,
              start: String,
              end:String,
              description:String,
                        _id:String
      }],
        survey: [{
                        teamwork:Number,
                        communication:Number,
                        initiative:Number
        }],
    
        images: [{
                        one: String,
                        two: String,
                        three: String,
                        four: String,
                        five:String,
                        six:String
        }],
    });
    
    
    module.exports = mongoose.model('User', User);
    module.exports = {
      User : mongoose.model('User', User),
      PositionApplied : mongoose.model('PositionApplied', PositionApplied)
    }
    

    呼叫:

         app.post('/deleteall', function(req, res){
              User.findOne({_id: req.user._id}, 'positionsApplied', function(err, user){
            if (err) return handleError(err);
            user.positionsApplied = user.positionsApplied.map(e => e.position_id);
            PositionApplied.remove({position_id: {$in: user.positionsApplied}}, function(){
    
            });
          }
      );
          });
    

    更新的错误信息:

    【讨论】:

    • 感谢您的回复!我收到错误消息:“无法读取未定义的属性 'map'”,我更新了我的问题,知道哪里出错了吗?
    • 您正在使用用户。回调中返回的应该是用户(小写)
    • 删除似乎运行没有错误,但是它拾取了数据库中不存在的奇怪 ID,我已将图像附加到问题中,有什么我遗漏的吗?感谢您的帮助!
    • 你能做一个 user.positionsApplied 的 console.log 吗?
    • 是的,如果我 console.log(user.positionsApplied) .. 我不确定 _id 来自哪里,上面的图像(从 remove() 行返回)是返回给我的
    猜你喜欢
    • 2013-09-04
    • 2019-01-04
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2015-03-24
    相关资源
    最近更新 更多