Here is the mongoose schema I created:
The userId is a unique number that Okta generates for user profiles.
var book_listSchema = new mongoose.Schema({
userId:{type: String, required: true},
first_name: { type: String, required: true},
last_name:{ type: String, required: true},
newList: [{
list_name: String,
books:
[{
book_name: {type: String, required: true},
book_author: {type: String, required: true},
date_added: {type: Date, default: Date.now},
date_finished: {type: Date, default: Date.now},
}]}],
});
Below is the code snippet I'm using to find and update, specifically adding a book to the books array.
'''
book_list.findOneAndUpdate({"userId": req.userContext.userinfo.sub, newList:{"newList.list_name": list_name}}, {$push:{books:{book_name: title, book_author:author, date_added: new Date()}}}
'''
However, I am facing an issue where the books array does not get updated correctly. Each object in newList has a list_name associated with different lists, each containing their own books array. Therefore, it's essential to ensure the correct list is being updated when a new book is added.
For reference, here is a sample dataset:
'''
_id:625c81d7622b044fb297ce54
userId:"00uvgyn7OikAwud6"
first_name:"John"
last_name:"Smith"
newList:Array
0:Object
list_name:"read"
books:Array
0:Object
book_name:"The Great Gatsby"
book_author:"Francis Scott Fitzgerald"
date_added:2022-04-19T00:43:24.248+00:00
_id:625e05ac083663dc44f37804
date_finished:2022-04-19T00:43:24.252+00:00
_id:625e05ac083663dc44f37803
__v:0
'''