0

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".

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
em.rexhepi
  • 289
  • 1
  • 5
  • 17
  • you can use the document created date to sort and subscribe to only first 10 documents. To load more documents, you can increase the limit count of the subscription. You typically want load more functionality, right? – Rajanand02 Nov 11 '14 at 11:46
  • @Rajanand02 Thank you for your response. I did that in another case. This case is where I show the post say it is in the middle of the posts. If i subscribe to the posts by date i get first 10 of 1000. If it is the 569th post how can i get 569+10 ? To create a plylist with the next 10 posts. – em.rexhepi Nov 11 '14 at 11:54
  • You can use MongoDB skip and limit to achieve this. Check the gist https://gist.github.com/rajanand02/b8451b71c51e3618a216 – Rajanand02 Nov 11 '14 at 12:19
  • @Rajanand02 Thank you again I'll have a look at this and will reply if I found an answer or not. – em.rexhepi Nov 11 '14 at 12:41
  • Well @Rajanand02 I don't really know that the entry is in the 569 place all I have and I know is the _id of the entry. I get the _id from the iron:router link as a parametter "id = this.params._id" I have to find then the entry and get "that entry + next 10" – em.rexhepi Nov 11 '14 at 12:46

2 Answers2

1

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.

Jamgold
  • 1,716
  • 1
  • 14
  • 18
  • Yes I'm doing subscribe/publish. Thank you for your time. But this is giving me an error : " While Building the application: server/publications.js:16:31: Unexpected token [" Why is ti ? – em.rexhepi Nov 12 '14 at 14:21
  • I forgot a : between the $and and the [. Try Collection.find({$and:[{_id:{$ne: doc._id}},{date:{$gte:doc.date}}]},{sort:{date:1},limit:10}) – Jamgold Nov 13 '14 at 05:51
  • Thank you sir now it works without any error. But it does not return anything. Videos.find().fetch() return: Array [ ] <- It returns an empty array – em.rexhepi Nov 13 '14 at 09:46
  • On the client or on the server? Does your project use autopublish? What do you publish on the server? – Jamgold Nov 13 '14 at 16:33
  • Here, I put together a gist how it worked for me: https://gist.github.com/jamgold/8dae67dca257107b14c6 – Jamgold Nov 13 '14 at 16:36
  • On server side forgot to comment again just removed {_id:{$ne:mydocument._id}} now works like a charm. Thank you very much – em.rexhepi Nov 13 '14 at 16:46
0

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.

user3374348
  • 4,101
  • 15
  • 29
Rajanand02
  • 1,303
  • 13
  • 19
  • I see this but first. I dont want to search by userID. I want to find the entry by document ID and then find the next 10 entries sorted by date. 10 entries that are older then the entry i just found by id. – em.rexhepi Nov 11 '14 at 12:58
  • You can send document id as a argument. – Rajanand02 Nov 11 '14 at 13:06
  • Yes but then it will output only the document that matches the document id. My struggle is not how to get the document with the document _id but how to get also the next 10. Without knowing the skipCount. – em.rexhepi Nov 11 '14 at 13:13
  • on what basis you want next 10?you can't sort based on the MongoDB id since it is random. you can use time to get next and previous 10 documents. – Rajanand02 Nov 11 '14 at 16:18
  • I want to get the time from the document by id then get the next 10 posts and 10 prev posts – em.rexhepi Nov 11 '14 at 16:20