简体   繁体   中英

unable to “skip” mongoose sub-documents

I am using mongoose and node and I am trying to paginate data from sub-documents. I am able to limit subdocuments, but not skip them.

The versions I am using are:

Mongo 3.0.0

Node 0.10.33

Mongoose 3.9.7

The Data

[{ 
    "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 Code

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);
    });

The Result The above code DOES limit the number of "body" sub-documents to 2, but doesn't skip any.

The code above returns this:

{ 
    "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") 
        }
    ]
} 

But it should return this:

{ 
    "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") 
        }
    ]
} 

The solution that I found was to use aggregate and specify the name of the subdocument with $unwind.

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);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM