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