edwardhotchkiss / mongoose-paginate

Mongoose.js (Node.js & MongoDB) Document Query Pagination
MIT License
984 stars 216 forks source link

Query with reference #53

Closed molerat619 closed 9 years ago

molerat619 commented 9 years ago

Hi,

when I do the following:

Group.paginate({destination_country: "some-country-id-1234"}) ...

I get no results back, even though there should be some. Using {} as query gives me all Groups, so the rest of the paginate-function is okay.

Group is a model with destination_country as a reference to the Country Model (which also works fine with all my other needs).

niftylettuce commented 9 years ago

@molerat619 please share your full schema in here for the models

molerat619 commented 9 years ago
var GroupSchema = new Schema({
    name: {type: String, unique: true, required: true, uppercase: true},
    ...
    program: {type: Schema.ObjectId, ref: 'Program'},
    departure_country: {type: Schema.ObjectId, ref: 'Country'},
    destination_country: {type: Schema.ObjectId, ref: 'Country'},
    airline: {type: Schema.ObjectId, ref: 'Airline'},
    ...
});

var CountrySchema = new Schema({
    name: {type: String, required: true, unique: true, uppercase: true},
    code: {type: String, required: true, unique: true, uppercase: true},
    ...
})
molerat619 commented 9 years ago

I got it working now, but I ask myself, if somehow I can do Group.paginate({destination_country.name: "Germany"}) or so. That would be more interesting.

edwardhotchkiss commented 9 years ago

Please feel free to drop a gist or a repo/files/anything so that I can understand your issue better. Thanks!

yogiswar1987 commented 9 years ago

Could you please give us the solution i am stuck at similar place to sort/search in referenced objects

edwardhotchkiss commented 9 years ago

Please give a schema.

yogiswar1987 commented 9 years ago

This is my user schema var userSchema = mongoose.Schema({id:{type:String,index:true},group:{type: mongoose.Schema.Types.ObjectId, ref: 'group' ,default:null}}).plugin(mongoosePaginate);

this is my group schema var groupSchema =mongoose.Schema({name:{type:String,index:true},parentGroupId:mongoose.Schema.Types.ObjectId},{id:false,toJSON:{virtuals:true}}).plugin(mongoosePaginate);

for pagination of user i am using below code in which sort param is like {id:-1}

db.user.paginate(f,{page:pageNo,limit:count,columns:db.sim.projection,populate:[db.user.groupPopulation],sortBy:sortParam})

I want to sort user using group.name in the paginate

yogiswar1987 commented 9 years ago

or even search which looke like

f["$text"] = { $search :searchQ};

is not searching in populated froupname