【问题标题】:Limiting embedded document size限制嵌入文档大小
【发布时间】:2011-09-15 09:26:49
【问题描述】:

我有 MongooseJS 的 user 架构,嵌入文档 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 }
});

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

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

【问题讨论】:

    标签: mongodb node.js mongoose


    【解决方案1】:

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

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

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

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

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

    Documentation

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2012-01-17
      • 1970-01-01
      相关资源
      最近更新 更多