вы можете определить свою схему следующим образом:
const userSchema = new mongoose.Schema({
likedBooks: [{
type: mongoose.Types.ObjectId,
ref: 'books'
}],
email: {
type: String,
required: true
},
name: {
type: String,
required: true
}
});
exports.User = mongoose.model('users', userSchema);
, затем вы можете заполнить данные, выполнив
user = User.find({ email: req.body.email }).populate('likedBooks');
здесь «Любимые книги» содержит _id каждой книги
const bookSchema = new mongoose.Schema({
isbn: {
type: Number,
required: true
},
name: {
type: String,
required: true
},
author: {
type: String,
required: true
},
publisher: {
type: String,
default: ""
},
imageUrl: {
type: String,
required: true
},
description: {
type: String,
default: ""
}
});
exports.Book = mongoose.model('books', bookSchema);
для обеих схем я не поставил _id, так как он автоматически генерируется mongodb и используется как ссылка