title: mongoose own populate with custom query
body: <p>I'm trying to create a custom query method in mongoose - similar to the populate()-function of mongoose. I've the following two simple schemas:</p>
  
  <p>const mongoose = require('mongoose')</p>
  
  <pre><code>const bookSchema = new mongoose.Schema({
      title: String,
      author: {type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Author'}
  }, {versionKey: false})
  
  const authorSchema = new mongoose.Schema({
      name: String
  }, {versionKey: false})
  </code></pre>
  
  <p>Now, I want retrieve authors information and furthermore the books written by the author. As far as I know, mongoose provides custom queries, hence my idea was to write a custom query function like:</p>
  
  <pre><code>authorSchema.query.populateBooks = function () {
      // semi-code:
      return forAll(foundAuthors).findAll(books)
  }
  </code></pre>
  
  <p>Now, to get all authors and all books, I can simply run:</p>
  
  <p>authorModel.find({}).populateBooks(console.log)</p>
  
  <p>This should result in something like this:</p>
  
  <pre><code>[ {name: "Author 1", books: ["Book 1", "Book 2"]}, {name: "Author 2", books: ["Book 3"] } ]
  </code></pre>
  
  <p>Unfortunately, it doesn't work because I don't know how I can access the list of authors selected previously in my populateBooks function. What I need in my custom query function is the collection of the previous-selected documents.
  For example, authorModel.find({}) already returns a list of authors. In populateBooks() I need to iterate through this list to find all books for all authors. Anyone know how I can access this collection or if it's even possible?</p>