简体   繁体   中英

MeteorJS & MongoDB: How to get next 10 entries

I have a database like this :

{
    "_id" : "xFZtChfKTf3GLxFEg",
    "category" : "pranks",
    "date" :new Date(),
    "rate" : {
    "up" : 0,
        "down" : 0
},
    "user" : "User_1",
    "vTitle" : "Kissing Prank - How to Kiss ANY Girl in 10 SECONDS - Kissing Strangers/Funny Videos/Best Pranks 2014",
    "v_id" : "Fa1agPyuRRM",
    "views" : 0
},
{
    "_id" : "RB2uwCfsjujFwFpZe",
    "category" : "pranks",
    "date" :new Date(),
    "rate" : {
    "up" : 0,
        "down" : 0
},
    "user" : "User_1",
    "vTitle" : "Dropping Guns in the Hood (PRANKS GONE WRONG) - Pranks in the Hood - Funny Videos - Best Pranks 2014",
    "v_id" : "K1SksoAHIa0",
    "views" : 0
},
{
    "_id" : "3CvrFtYo4wWE5Coj7",
    "category" : "pranks",
    "date" :new Date(),
    "rate" : {
    "up" : 0,
        "down" : 0
},
    "user" : "User_1",
    "vTitle" : "TOP Pranks 2014 - Pranks in the Hood - Pranks Gone Wrong - Funny Pranks 2014 - PRANK COMPILATION",
    "v_id" : "oEgXOhxXvsc",
    "views" : 0
},
{
    "_id" : "doiA7EPkwCe5meyJ7",
    "category" : "pranks",
    "date" :new Date(),
    "rate" : {
    "up" : 0,
        "down" : 0
},
    "user" : "User_1",
    "vTitle" : "Top 5 Pranks of 2014",
    "v_id" : "A9w72vSuPAQ",
    "views" : 0
},
{
    "_id" : "8oK2JxqJfEkzceWHB",
    "category" : "pranks",
    "date" :new Date(),
    "rate" : {
    "up" : 0,
        "down" : 0
},
    "user" : "User_1",
    "vTitle" : "Friday The 13th Scare Prank",
    "v_id" : "6m4isWlUlRE",
    "views" : 0
},
{
    "_id" : "5NwM2fbnifKejgSct",
    "category" : "pranks",
    "date" :new Date(),
    "rate" : {
    "up" : 0,
        "down" : 0
},
    "user" : "User_1",
    "vTitle" : "7 SUPER EASY PRANKS - HOW TO PRANK",
    "v_id" : "RckNziU2JEk",
    "views" : 0
},
{
    "_id" : "x5QqJu2e54kjFpfkz",
    "category" : "pranks",
    "date" :new Date(),
    "rate" : {
    "up" : 0,
        "down" : 0
},
    "user" : "User_1",
    "vTitle" : "Orphanage Robbery Prank!!",
    "v_id" : "dBfVwjRuwxk",
    "views" : 0
},

If I have an "_id" : "3CvrFtYo4wWE5Coj7", how can I get next 10 & previews 10 entries by date starting from "_id" : "3CvrFtYo4wWE5Coj7"?

Say if I have 500 entries before "_id" : "3CvrFtYo4wWE5Coj7" and 500 after.

Edit: All i know is the entry ID from the iron:router parameter "id = this.params._id" I have to find then the entry and get "that entry + next 10" or "that entry + prev 10".

When you get a random document, why can't you look at it's date and then query MongoDB for the next 10 documents that are older/younger (depending in which direction you want to go)?

Collection.find({$and[{date:{$gte:{mydocument.date}}},{_id:{$ne:mydocument._id}}]},{sort:{date:1}}).limit(10)

This would imply that you subsribed to all documents in your collection, which of course might not be the best thing to do considering the size of your collection. Instead you might want to change the subscribe/publish to only publish the documents you actually want.

you can use MongoDB skip and limit.

// Set some default value for the session variables

Session.setDefault("limitCount",10);
Session.setDefault("skipCount",0);

// subscribe to the post in client-side using the session variables.

Tracker.autorun(function () {
  Meteor.subscribe("posts", Session.get("limitCount"),Session.get("skipCount"));
});

// Publish using the limit and skip values.

Meteor.publish("posts",function (limitCount,skipCount) {
  return Posts.find({user_id: this.userId},{
    sort: {date: -1},
    limit: limitCount,skip: skipCount
  });
});

In template events, you can increase the Session variable values to get desired number of posts.

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