0

I'm trying to update an inner document in mongoDB and Node.js. I have a collection which references another collection. Through accessing the first collection, I want to update the second collection.

Here's the schema structure:

// User
mongoose.Schema({
    email: {
        type: String,
        required: true
    }
    listsID: {
        type: [{ type: mongoose.Types.ObjectId, ref: 'List' }],
        required: false
    }
});

// List
mongoose.Schema({
    name: {
        type: String,
        required: true
    }
});

What I would like to do is get a single List document based on the User, then update that List I just retrieved. It would look something like the following:

const list = await User.findById(someID).find({name: someName});

The above is obviously not the correct way of doing it, but that's what I'm trying to achieve. What's the correct way of doing that?

Basically, I would like to update the listID array of a specific User

Edit

Here's the result data using findByID on for the user:

Without populate

{
    "listsID": [
        "5d902b1b63ebcf9fff238faa",
        "5d90e61c185bd8cfafa04c6e"
    ],
    "_id": "5d8e28562d657f6ebb67c357",
    "email": "someEmail@gmail.com"
}

With Populate

{
    "listsID": [
        {
            "_id": "5d902b1b63ebcf9fff238faa",
            "name": "First List", // This is what I want to update
        },
        {
            "_id": "5d90e61c185bd8cfafa04c6e",
            "name": "another list",
        }
    ],
    "_id": "5d8e28562d657f6ebb67c357",
    "email": "someEmail@gmail.com"
}
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Jessica
  • 9,379
  • 14
  • 65
  • 136
  • 1
    What do you mean by update? Show what it is you actually want to "update" in the related item. Please also show the schema and specifically if you actually set any fields in the related "child" to include the parent reference. Also you really should consider including the parent reference within the child if you have not already. – Neil Lunn Sep 29 '19 at 23:32
  • @NeilLunn Basically, I would like to update the `listID` array of a specific `User` – Jessica Sep 29 '19 at 23:35
  • @NeilLunn Are you saying it's better to ref User in List, rather than ref List in User? – Jessica Sep 29 '19 at 23:36
  • 1
    Still not really clear what you are saying. `listsID` in your schema is an "array of `ObjectId` values" which point to the *"related"* object in another collection. It might be better if you actually showed a small sample of data and what result you expect. **Showing** works best when you are unsure of how to describe it otherwise, or don't know the methods to implement. Right now your usage of `find()` with a specific name **suggests** you actually mean something else than "update". So it is best here to just **show**. – Neil Lunn Sep 29 '19 at 23:39
  • @NeilLunn Thanks for the guidance! I updated the question with the data results and exactly what I want to change – Jessica Sep 29 '19 at 23:46
  • 1
    So that's not an "update" in the sense of "changing stored data". What you are asking for is how to return the related data applying a condition so you only return that which "matches" the condition in the related items. There are existing answers to that. – Neil Lunn Sep 29 '19 at 23:50
  • @NeilLunn Very well said! Thanks for clarifying! – Jessica Sep 29 '19 at 23:51

0 Answers0