mongodb - Mongoose: sort query results by least occurences of word in array -


i have collection of videos :

var videoschema = new mongoose.schema({   url: {     type: string, required : true   },   orderby: {     type: number   },   views: [{     type: string   }] }); 

'orderby' lets me define order in serve videos

'views' list of usernames, have watched video.

i want keep users watching again same video until have watched videos in collection. keep names of users have watched video inside 'views'.

now query videos using 'sort'.

return this.find(videoquery) .skip(offset) .limit(count || 10) // @todo 10 : param .sort({views: {$meta: username}, 'orderby': -1}) .exec(); 

and error message :

can't canonicalize query: badvalue bad sort specification 

can ?

may wrong mongo docs. when specify $meta in sort need include in projectedfieldname.

https://docs.mongodb.com/manual/reference/operator/projection/meta/#proj._s_meta

so should looks this

return this.find(videoquery, { views: { $meta: 'textscore' } }) .skip(offset) .limit(count || 10) // @todo 10 : param .sort({views: {$meta: textscore}, 'orderby': -1}) .exec();

updated seems $meta accepts 'textscore' @pierre mentioned.

so suggestion use underscore or native js sort data when response comes db. less efficient , may need adjustments, work definitely.

hope helps.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -