2015-03-13 37 views
2

我正在使用貓鼬和節點,我試圖從分文檔分頁數據。我可以限制子文檔,但不能跳過它們。無法「跳過」貓鼬子文件

我使用的版本是:

蒙戈3.0.0

節點0.10.33

貓鼬3.9.7

數據

[{ 
    "name" : "Ranger Table", 
    "_id" : ObjectId("550234d3d06039d507d238d8"), 
    "body" : [ 
     { 
      "name" : "Jason", 
      "colour" : "Red", 
      "animal" : "T-rex", 
      "_id" : ObjectId("550234d3d06039d507d238de") 
     }, 
     { 
      "name" : "Billy", 
      "colour" : "Blue", 
      "animal" : "Triceratops", 
      "_id" : ObjectId("550234d3d06039d507d238dd") 
     }, 
     { 
      "name" : "Zach", 
      "colour" : "Black", 
      "animal" : "Mastadon", 
      "_id" : ObjectId("550234d3d06039d507d238dc") 

     }, 
     { 
      "name" : "Tommy", 
      "colour" : "Green", 
      "animal" : "Dragon" 
      "_id" : ObjectId("550234d3d06039d507d238d9") 
     } 
    ] 
},{ 
    "name" : "Bot Table", 
    "_id" : ObjectId("5502d205184cd74033f64e6b"), 
    "body" : [ 
     { 
      "name" : "Optimus", 
      "team" : "Autobots", 
      "class" : "Leader", 
      "_id" : ObjectId("550234d3d06039d507d238d9") 
     }, 
     { 
      "name" : "Bumblebee", 
      "team" : "Autobots", 
      "class" : "Scout", 
      "_id" : ObjectId("550234d3d06039d507d238da") 
     }, 
     { 
      "name" : "Astrotrain", 
      "team" : "Decepticons", 
      "class" : "Transport", 
      "_id" : ObjectId("550234d3d06039d507d238db") 

     } 
    ] 
}] 

The C.賦

var BodySchema = new Schema({random: String},{strict:false}); 

var FeedSchema = new Schema({ 
    name: String, 
    body:[BodySchema] 
}); 

var feed = mongoose.model('Feed', FeedSchema); 

feed.find({_id:'550234d3d06039d507d238d8'}) 
    .populate({ 
     "path":"body", 
     "options":{ 
      limit:2, //This works fine 
      skip:2 //This doesn't work 
     } 
    }) 
    .exec(function(err, result){ 

     if(err){return(res.send(500, err))} 

     res.send(result); 
    }); 

結果 上面的代碼確實限制的「體」的子文件2的數量,但不跳過任何。

以上的回報這個代碼:

{ 
    "name" : "Ranger Table", 
    "_id" : ObjectId("550234d3d06039d507d238d8"), 
    "body" : [ 
     { 
      "name" : "Jason", 
      "colour" : "Red", 
      "animal" : "T-rex", 
      "_id" : ObjectId("550234d3d06039d507d238de") 
     }, 
     { 
      "name" : "Billy", 
      "colour" : "Blue", 
      "animal" : "Triceratops", 
      "_id" : ObjectId("550234d3d06039d507d238dd") 
     } 
    ] 
} 

但它應該返回此:

{ 
    "name" : "Ranger Table", 
    "_id" : ObjectId("550234d3d06039d507d238d8"), 
    "body" : [ 
     { 
      "name" : "Zach", 
      "colour" : "Black", 
      "animal" : "Mastadon", 
      "_id" : ObjectId("550234d3d06039d507d238dc") 

     }, 
     { 
      "name" : "Tommy", 
      "colour" : "Green", 
      "animal" : "Dragon" 
      "_id" : ObjectId("550234d3d06039d507d238d9") 
     } 
    ] 
} 
+1

這些都不是 「子文件」,但 「引用」 的文檔。 Mongoose的'.populate()'方法不是一個「魔術連接」,太多人認爲它是。事實上,這只不過是由於該函數調用的結果而發生的另一個查詢執行,以使「加入」數據的外觀。它不支持您試圖執行的「跳過」語義。 – 2015-03-13 12:58:26

+1

相關的貓鼬問題[**填充選項跳過 - 不工作#2252 **](https://github.com/LearnBoost/mongoose/issues/2252) – chridam 2015-03-13 12:58:31

+0

Chridam:我看到了,但它描述的問題不同於礦。 – PaulParton 2015-03-13 23:42:40

回答

0

,我發現是骨料使用,並指定與$放鬆身心的子文檔的名稱的解決方案。

http://docs.mongodb.org/manual/reference/operator/aggregation/

feed.aggregate([ 
    {'$match':{_id:id('550234d3d06039d507d238d8')}}, 
    {'$unwind':'$body'}, 
    {'$skip':2}, 
    {'$limit':2}, 
], function(err, result){ 

    if(err){return(res.send(500, err))} 

    res.send(result); 

});