0

this is 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: [{
        list_name: String,
        books: [{
            list_name: String,
            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
            },
            book_image: {
                type: String,
                required: true
            }
        }]
    }],
});

var book_list = mongoose.model('book_list', book_listSchema);

module.exports = book_list;

This is my query:

book_list.findOne({
        "userId": req.userContext.userinfo.sub
    }, {
        newList: {
            $elemMatch: {
                "list_name": listName
            }
        }
    })

    .skip((perPage * page) - perPage)
    .limit(perPage)
    .exec(function(err, doc) {

        console.log(doc);

        response.render('display_list', {
            userContext,
            list_books: doc.newList[0].books,
            listName: listName,
            pages: Math.ceil(doc.newList[0].books.length / perPage),
            current: page
        })
    })
});

This is the doc/file that is returned:

{
    _id: new ObjectId("63b60f5844ffdd86146d6e5b"),
    newList: [{
        list_name: 'read',
        _id: new ObjectId("63b60f5844ffdd86146d6e5c"),
        books: [Array]
    }]
}

What I need returned is just the books:[Array]. I am trying to render the contents of the books:[Array] to a page with pagination.

Shivam
  • 3,514
  • 2
  • 13
  • 27
Daniel Semel
  • 175
  • 1
  • 1
  • 13
  • Does this answer your question? [Mongoose, Select a specific field with find](https://stackoverflow.com/questions/24348437/mongoose-select-a-specific-field-with-find) – Shivam Jan 06 '23 at 01:02
  • you need to add .select('books') before exec – Nonik Jan 06 '23 at 01:23
  • @Shivam That does not answer my question because I am trying to retrieve an array inside an array, so I can't exclude the array newList because books[] is inside of it. – Daniel Semel Jan 06 '23 at 05:20

1 Answers1

0

Try this:

book_list.findOne({
    "userId": req.userContext.userinfo.sub,
    "newList.list_name": listName
  }, {
    "newList.$.books": 1
  })
  .skip((perPage * page) - perPage)
  .limit(perPage)
  .exec(function(err, doc) {
    console.log(doc);
    response.render('display_list', {
      userContext,
      list_books: doc.newList[0].books,
      listName: listName,
      pages: Math.ceil(doc.newList[0].books.length / perPage),
      current: page
    })
  })
});

It'll only return the array of books and if you use doc.newList[0].books, it will give you the array you want.

Make sure, $ is a positional operator only project the 1st element that match the query, so if you have multiple lists with the same name, it'll just return the first one.

If you face any query. Please let me know

Mandeep
  • 364
  • 2
  • 16