search

Home  >  Q&A  >  body text

node.js - How to delete array elements in mongoose

Now there are two schemas, one is students and the other is students’ employment information

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<code class="javascript">var Student = new mongoose.Schema({

    jobs:[{

        type:ObjectId,

        ref: 'job'

    }]

});

 

var job = new mongoose.Schema({

    student:{

        type: ObjectId,

        ref: 'student'

    }

})

 

删除一条job文档的时候如何把学生文档的jobs数组中该job的ObjectId也删了,

我用了如下的写法:

Job.remove({_id:_id}, function(err,job){

    var jobId = job._id;

    Student.update({_id:StudentId},{

    $pull:{jobs:{ObjectId: jobId}}

    }, function(){})

})

</code>

This will report an error, what is the correct way to write it

PHPzPHPz2897 days ago1385

reply all(1)I'll reply

  • 阿神

    阿神2017-05-24 11:37:44

    It feels superfluous. These are just references. The data in the real database will be filled in during populate. How can the deleted data be referenced?

    reply
    0
  • Cancelreply