Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ========== THIS IS MY AUTHOR SCHEMA ============
- authorSchema = new mongoose.Schema({
- name: {
- type: String,
- required: true
- },
- books: [{
- type: mongoose.Schema.Types.ObjectId,
- ref: 'Book'
- }],
- });
- // ========= THIS IS MY BOOK SCHEMA =========
- const bookSchema = new mongoose.Schema({
- title: {
- required: true,
- type: String
- },
- authors: [{
- id: {
- type: mongoose.Schema.Types.ObjectId,
- ref: 'Author'
- },
- name: String
- }],
- identifiers: [],
- publisher: String,
- publishedDate: Date,
- description: String,
- textSnippet: String,
- imageURL: String,
- categories: [],
- });
- // ====== THIS IS HOW I CURRENTLY GET ALL THE BOOKS (THIS IS WHERE I WANT TO FILTER THE RESULT TO MATCH OTHER SEARCHES) ==========
- db.Author.find({'name': authorSearch})
- .populate('books')
- .limit(limit)
- .skip(limit * page)
- .then(function(authors){
- if(authors.length > 0){
- const books = [];
- authors.forEach(function(author){
- books.push(...author.books);
- });
- return books;
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment