1

I need to remove a specific object that is nested inside an array of objects.

The following db structure looks like:

imd

I would like to remove one of the teams based on the roomId (to find the specific room) and based on the team approved state. If a team has "approved: false" it needs to be deleted from the array of objects.

I'm using mongoose and came up with the following, but without succes:

     Room.update({"roomId": req.params.roomId},
        {"$pull": { "teams.approved": false } })

screenshot thats shows the correct roomId: img

2
  • The answers of Nicolas Maties and mickl are correct, it might be req.params.roomId giving you a bad value, console.log(req.params.roomId) and make sure it has a value Commented Jun 19, 2019 at 10:14
  • it does have a value and its the correct roomId, Look at the screenshot above in the post Commented Jun 19, 2019 at 10:16

3 Answers 3

1

The array name and equality condition should be specified separately like in this example, try:

await Room.update({"roomId": req.params.roomId}, {"$pull": { "teams": { approved: false } } })
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried the above, it doesn't work unfortunately
@Krelis could you check if req.params.roomId has expected value and type ?
it does, see screenshot above in the initial post.
@Krelis I don't see any callback / await in your code, possibly that can be the problem, could you check that ?
0

Room.updateOne({ roomId: req.params.roomId}, {"$pull": { "teams": { approved: false } } })

This might work for your case.

Comments

0

I think something like this should work:

Room.find({roomId: 1234}).then((result) => {
if (result) {
 return Room.remove({result.teams.approved: false})
 } 
} ).catch()

You still need to first find all results matching the roomId number and then removing the teams based on approved. There are probably better ways to handle this but I think it's a fair approach.

1 Comment

TypeError: result.remove is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.