3

我有嵌入式文档的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 }
});

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

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

4

1 回答 1

6

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

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

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

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

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

文档

于 2011-06-15T09:45:43.437 回答