mortiy
mortiy

Reputation: 679

Limiting embedded document size

I have user schema for MongooseJS with embedded document photos by 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 retreive one user, how can I limit quantity of photos in result set?

Or should I retrive all photos that user have (even if there is a million)?

Upvotes: 3

Views: 491

Answers (1)

Andrew Orsich
Andrew Orsich

Reputation: 53675

You can't retreive users with limited quantity of photos, but you can:

1.Load user(s) without photos first:

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

2.Load just user photos thats you need:

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

Documentation

Upvotes: 6

Related Questions