Answer the question
In order to leave comments, you need to log in
When deleting an element of a collection, what is the right way to remove references to this element (located inside an array of other MongoDB collections)?
There are three entities: user, task & project
QUESTION: When deleting a task, for example, remove references to it from user and project?
What is the correct way to search through the collection in such cases?
Perhaps I organized the data storage itself incorrectly.
The arrays projects, tasks, developers store the ObjectId of the corresponding entities.
user schema:
{
"_id": {
"$oid": "5f7c39b9871108e68c2f05d3"
},
"position": "developer",
"isActive": true,
"projects": [],
"tasks": [{
"$oid": "5f65f18d95d65e3163c9dbda"
}, {
"$oid": "5f6efbc1a986526f5df0de2b"
}, {
"$oid": "5f7b283015d2c359de263d96"
}],
"username": "Robert Kabakov",
"email": "[email protected]",
"password": "$2b$10$t3UrCUadw3yJuVTRftZZJetchjXLn.7Y6LvAqv1KvwWkSGNwRKzli",
"activeToken": "6396e0682231a887d8add1c87f999b0b",
"activeExpires": {
"$date": {
"$numberLong": "1600599704592"
}
},
"createdAt": {
"$date": {
"$numberLong": "1600513304670"
}
{
"_id": {
"$oid": "5f7c3a1d871108e68c2f05d4"
},
"developers": [],
"tasks": [],
"name": " What is Lorem Ipsum?",
"content": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
"manager": {
"$oid": "5f65e5ebb08750307e2bc0d5"
},
"createdAt": {
"$date": {
"$numberLong": "1601973271567"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1601973271567"
}
},
"__v": {
"$numberInt": "0"
}
}
{
"_id": {
"$oid": "5f7c3a1d871108e68c2f05d4"
},
"developers": [],
"tasks": [],
"name": " What is Lorem Ipsum?",
"content": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
"manager": {
"$oid": "5f65e5ebb08750307e2bc0d5"
},
"createdAt": {
"$date": {
"$numberLong": "1601973271567"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1601973271567"
}
},
"__v": {
"$numberInt": "0"
}
}
Answer the question
In order to leave comments, you need to log in
It turned out to remove an array element as follows:
User.updateOne(
{ _id: userId},
{ $pull: { tasks: { $in: req.params.taskId } } }
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question