Embed Document Size Limit

I have a userschema for MongooseJSwith an embedded document photoson PhotoSchema:

var UserSchema = new Schema({
    email    : { type : String, index : 'unique', set: toLower }
  , login    : { type : String, index : 'unique' }
  , password : { type : String } 
  , salt     : { type : String }
  , photos   : [ PhotoSchema ]
  , name     : { type : String }
});

When I return one user, how can I limit the number photosin the result set?

Or do I need to get all the photos a user has (even if there is a million)?

+3
source share
1 answer

You cannot retreat from users with a limited number of photos, but you can:

1. First, upload users without photos:

db.users.find( { _id: 1 }, { photos : 0 } );

2. Download only those user photos that you need:

db.users.find({}, {photos:{$slice: [20, 10]}}) // skip 20, limit 10

Documentation

+6
source

All Articles