简体   繁体   English

如何在猫鼬中对合并结果进行分页?

[英]How do I paginate combined results in mongoose?

I have a User model, and two different associated models Profile , WorkProfile . 我有一个User模型,以及两个不同的关联模型ProfileWorkProfile The profile model contains general info about the user line name, email, home address etc, workprofile contains details like current office address, office phone, office email etc The user model stores the objectID of these two and refers to them with profile and workprofile keys. profile模型包含有关用户行名称,电子邮件,家庭住址等的workprofile信息,工作workprofile包含当前办公地址,办公电话,办公室电子邮件等详细信息。用户模型存储这两个objectID并通过profile和工作workprofile密钥进行workprofile

Now what I am trying to build is an advanced search on user model based on the fields in these associated models. 现在,我要构建的是基于这些关联模型中的字段的用户模型高级搜索。 One way I see is to query them separately and then use _.concat to join the results. 我看到的一种方法是分别查询它们,然后使用_.concat结果。 But the problem with that is I can't implement skip or limit on the final result to get proper pagination. 但是,这样做的问题是我无法对最终结果实施skiplimit以获得正确的分页。 What would be the best way to do this with proper pagination? 分页正确的最佳方法是什么?

Instead of using ref on profile and workprofile You can add it to user like bellow, this allows for using find method with profile.name , etc. 您可以像波纹管一样将其添加到用户,而不是在配置文件和工作配置文件上使用ref ,这允许将find方法与profile.name等一起使用。

var ProfileSchema = mongoose.Schema({
    name: String
});

var WorkProfileSchema = mongoose.Schema({
    email: String
});

var UserSchema = mongoose.Schema({
    profile: ProfileSchema,
    work_profile: WorkProfileSchema 
});    

var User = mongoose.model('User', UserSchema);

var user = new User({
    profile: {name: 'Molda'},
    work_profile: { email: 'my@email.com'}
});

user.save(function(err, u) {

    User.findOne({'profile.name':'Molda'},function(err, user) {

        console.log('user', user);

    })

});

or even use simple objects 甚至使用简单的对象

var UserSchema = mongoose.Schema({
    profile: {
        name: String
    },
    work_profile: {
        email: String
    }
});

This might not be always good idea but it's possible. 这可能并不总是一个好主意,但是有可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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