mongoose: populate option limit - not working

code below gives 2 friends instead of 1.

var opts = { path: ‘author.friends’, select: ‘name’, options: { limit: 1 } }

  var assert = require('assert')
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  var ObjectId = mongoose.Types.ObjectId;

  /**
   * Connect to the db
   */

  var dbname = 'testing_populateAdInfinitum_1'
  mongoose.connect('localhost', dbname);
  mongoose.connection.on('error', function() {
    console.error('connection error', arguments);
  });

  /**
   * Schemas
   */

  var user = new Schema({
    name: String,
    friends: [{
      type: Schema.ObjectId,
      ref: 'User'
    }]
  });
  var User = mongoose.model('User', user);

  var blogpost = Schema({
    title: String,
    tags: [String],
    author: {
      type: Schema.ObjectId,
      ref: 'User'
    }
  })
  var BlogPost = mongoose.model('BlogPost', blogpost);

  /**
   * example
   */

  mongoose.connection.on('open', function() {

    /**
     * Generate data
     */

    var userIds = [new ObjectId, new ObjectId, new ObjectId, new ObjectId];
    var users = [];

    users.push({
      _id: userIds[0],
      name: 'mary',
      friends: [userIds[1], userIds[2], userIds[3]]
    });
    users.push({
      _id: userIds[1],
      name: 'bob',
      friends: [userIds[0], userIds[2], userIds[3]]
    });
    users.push({
      _id: userIds[2],
      name: 'joe',
      friends: [userIds[0], userIds[1], userIds[3]]
    });
    users.push({
      _id: userIds[3],
      name: 'sally',
      friends: [userIds[0], userIds[1], userIds[2]]
    });

    User.create(users, function(err, docs) {
      assert.ifError(err);

      var blogposts = [];
      blogposts.push({
        title: 'blog 1',
        tags: ['fun', 'cool'],
        author: userIds[3]
      })
      blogposts.push({
        title: 'blog 2',
        tags: ['cool'],
        author: userIds[1]
      })
      blogposts.push({
        title: 'blog 3',
        tags: ['fun', 'odd'],
        author: userIds[2]
      })

      BlogPost.create(blogposts, function(err, docs) {
        assert.ifError(err);

        /**
         * Population
         */

        BlogPost
        .find({ tags: 'fun' })
        .lean()
        .populate('author')
        .exec(function(err, docs) {
          assert.ifError(err);

          /**
           * Populate the populated documents
           */

          var opts = {
            path: 'author.friends',
            select: 'name',
            options: { limit: 1 }
          }

          BlogPost.populate(docs, opts, function(err, docs) {
            assert.ifError(err);
            console.log('populated');
            var s = require('util').inspect(docs, { depth: null })
            console.log(s);
            done();
          })
        })
      })
    })
  });

  function done(err) {
    if (err) console.error(err.stack);
    mongoose.connection.db.dropDatabase(function() {
      mongoose.connection.close();
    });
  }

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 26 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Related to this issue:

I have a nested set of items (versions) and I’d like to have a limit of 30 versions for each item in the array…

[
document1 (versions: [long array of 500+ versions]),
document2 (versions: [long array of 500+ versions]),
document3 (versions: [long array of 500+ versions]),
...
]

Using populate I want to make sure I get the top 20 of each of the documents, but it simply searches for $in( document1, document2, document3...) and then sets the limit to 20 * 3… isn’t it possible that it will be all 60 from document1 or is there some deeper wisdom used.

Here’s what the query says:

Mongoose: docversions.find({
 _id: { '$in': [ 
ObjectId("5c07d1e90feb71c693934ddc"), ObjectId("5c07d1e90feb71c693934d72") 

] } }, { limit: 20, sort: { createdDate: -1 }, projection: {} })

raised 5468

I think this issue is related to what I’m seeing.

Room
.find()
.populate({
  path: 'messages',
  select: 'message createdAt',
  options: {
    sort: { createdAt: -1 },
    limit: 1
  }
})
.exec(cb);
// Room results
[{ 
  _id: 53c04a605574680000405db2,
  messages: []
 },
{ 
  _id: 53c052dad8d226000045366f,
  messages: []
},
{
  _id: 53c05599d8d2260000453673,
  messages:
   [ { _id: 53c055b3d8d2260000453679,
       createdAt: Fri Jul 11 2014 15:22:59 GMT-0600 (MDT),
       message: 'whats up' },
     { _id: 53c055b2d8d2260000453678,
       createdAt: Fri Jul 11 2014 15:22:58 GMT-0600 (MDT),
       message: 'hello' },
     { _id: 53c055b1d8d2260000453677,
       createdAt: Fri Jul 11 2014 15:22:57 GMT-0600 (MDT),
       message: 'hey' } ]
 }]

When I try to populate a document with limit: 1, the last document in the array has seems to have set its limit equal to the number of returned Room documents and the rest of the documents’ message arrays are empty.

Mongoose: 3.8.12 Mongodb: 2.6.3