0

I have a schema like this.

peopleSchema = Schema({
    followerRefs: {
        type: [
            { type: Schema.Types.ObjectId, ref: 'peoples'}
        ],
        select: false
    },
    followRefs: {
        type: [
                { type: Schema.Types.ObjectId, ref: 'peoples'}
            ],
            select: false
        }
    });

Every time, I just want to select part of the followerRefs or followRefs. For example, I want to implement a paging, so I just want to select first 20 in the followRefs, or first 21 ~ 40 in the followRefs.

So, are there any way to get part of the followerRefs with select all of the list?

It seems that I didn't explain my question clearly. I assume that there are over one million entity in the followerRefs in database, and I just want to get the first 20 of them, which mean I just want to get the index of 0~19 of them. So I don't want to load all of the one million entity into the memory.

So I'm wondering whether there are any way to get the first 20 entity without load all of them?

Xiangyu.Wu
  • 459
  • 1
  • 5
  • 18
  • is this different than what was asked and answered here: http://stackoverflow.com/questions/5539955/how-to-paginate-with-mongoose-in-node-js – Ryan Quinn Nov 09 '14 at 16:02
  • It is a little different. In that question, the author want to find many different documents and limit the number of the documents. But in my case, I have already gotten the one people document, and I want to limit the list attribute of the people document. Thanks anyway! – Xiangyu.Wu Nov 09 '14 at 16:14

1 Answers1

0

If it's just a list of names, I don't think you should handle the sorting via mongoose nor the backend. But it depends on the size of data still.

Via javascript(frontend) I would sort the list first depending on what category, then I'd sort it. Sorting with plain javascript could be complicated and I'm not an expert with it, since I use plugins. XD

So here's a link with a link with an answer about javascript sorting. Sorting an array of JavaScript objects

Since you are asking about parts of the array, just use javascript slice() function.

var myList = ["Follower1", "Follower2", "Follower3", ...]; //pretend it goes til 20
var page1 = myList.slice(0, 19);
Community
  • 1
  • 1
  • Oh sorry, I didn't explain my question clearly. I assume that in the followerRefs, there may be over 1 million entity in database. And I don't want to load all of them into memory. I just want to get the first 20 people, which mean that I want to get the index of 0 to 19, and I need not to sort the list. Thank you anyway! – Xiangyu.Wu Nov 09 '14 at 16:17