This is my Schema:
let userSchema = new Schema({
email: { type: String, required: true },
password: { type: String, required: true },
books: [{ cover: String, title: String, link: String }],
});
I am pushing an object to the books array, only if that object does not already exist. I don't understand why this does not work: I am saying find the user, check the books
array to ensure that title does not exist add the new object to books array. However duplicates still show up in my array.
note: The query is being executed inside a function which is passed model
, user
, and an object containing the book data called info
hence the info.title
.
model.findOneAndUpdate(
{ email: user.email, 'books.title': { $ne: 'info.title ' } },
{ $addToSet: { "books": { title: info.title, cover:
info.cover, link: info.link } } },
{ new: true },
(err, user) => {
if (err) throw err;
console.log(user);
}
)
Appreciate the direction