Z
Z
Zhanna_K2020-10-06 12:45:30
MongoDB
Zhanna_K, 2020-10-06 12:45:30

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"
        }

project schema:
{
    "_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"
    }
}

task scheme:
{
    "_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

2 answer(s)
Z
Zhanna_K, 2020-10-06
@Zhanna_K

It turned out to remove an array element as follows:

User.updateOne(
      { _id: userId},
      { $pull: { tasks: { $in: req.params.taskId } } }
    )

https://docs.mongodb.com/manual/reference/operator...

H
hzzzzl, 2020-10-06
@hzzzzl

unfortunately only manually
https://docs.mongodb.com/manual/reference/operator...

User.update(
  { /* все юзеры */ }, 
  { $pull: 
     { tasks: { $oid: task_id } } 
  }
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question