限制嵌入式文档大小

mor*_*tiy 3 mongoose mongodb node.js

我有嵌入式文档的user架构:MongooseJSphotosPhotoSchema

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 }
});
Run Code Online (Sandbox Code Playgroud)

当我检索一个时user,我如何限制photos结果集的数量?

或者我应该检索用户拥有的所有照片(即使有一百万张)?

And*_*ich 6

您无法使用有限数量的照片检索用户,但您可以:

1.首先没有照片的用户负载:

db.users.find( { _id: 1 }, { photos : 0 } );
Run Code Online (Sandbox Code Playgroud)

2.加载您需要的用户照片:

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

文档