I have a User
model, and two different associated models Profile
, WorkProfile
. 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.
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. But the problem with that is I can't implement skip
or limit
on the final result to get proper pagination. 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.
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.
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.