My Mongoose schema:
var book_listSchema = new mongoose.Schema({
userId: {
type: String,
required: true
},
first_name: {
type: String,
required: true
},
last_name: {
type: String,
required: true
},
newList: [],
});
In Express I have the post function
router.post('/fileExpress/:title&:author?',...
var list_name = req.body.list;
var title = req.params.title;
var author = req.params.author;
addToList[list_name] = [{book_name: title, book_author:author, date_added: new Date()}];
I've added this array to newList through mongoose push command.
book_list.findOneAndUpdate({"userId": req.userContext.userinfo.sub},{ $push:{newList: addToList}},
This sends data to MongoDB that looks like this:
newList:Array
0:Object
read:Array
0:Object
book_name:"A Game of Thrones"
book_author:"George R. R. Martin"
date_added:2022-04-14T21:23:59.815+00:00
Inside this post function I have the Mongoose command
book_list.findOne({"userId":req.userContext.userinfo.sub,"newList.list_name.book_name":title
The code section newList.list_name.book_name does not work with the variable list_name. It does work when I type in the name of the actual list. In this case 'list_name' is 'read'.
I am using this because there will be lists with many different names and one function needs to work for all of them.