Permalink
Browse files

Allow $meta args in sort() so text search sorting works

  • Loading branch information...
1 parent fdb22f2 commit 86a054aef1eec3701359e18bfcf24d12a320cace @vkarpov15 vkarpov15 committed Apr 1, 2014
Showing with 14 additions and 2 deletions.
  1. +7 −1 lib/mquery.js
  2. +7 −1 test/index.js
View
@@ -1202,6 +1202,12 @@ Query.prototype.sort = function (arg) {
*/
function push (opts, field, value) {
+ if (value && typeof value === 'object' && value.$meta) {
+ var s = opts.sort || (opts.sort = {});
+ s[field] = { $meta : value.$meta };
+ return;
+ }
+
var val = String(value || 1).toLowerCase();
if (!/^(?:ascending|asc|descending|desc|1|-1)$/.test(val)) {
if (utils.isArray(value)) value = '['+value+']';
@@ -1214,7 +1220,7 @@ function push (opts, field, value) {
.replace("ascending", "1")
.replace("desc", "-1")
.replace("descending", "-1");
- s[field] = parseInt(valueStr);
+ s[field] = parseInt(valueStr, 10);
}
/**
View
@@ -1135,9 +1135,15 @@ describe('mquery', function(){
assert.equal(e.message, 'Invalid sort() argument. Must be a string or object.');
})
+ it('handles $meta sort options', function(){
+ var query = mquery();
+ query.sort({ score: { $meta : "textScore" } });
+ assert.deepEqual(query.options.sort, { score : { $meta : "textScore" } });
+ })
+
no('count', 'sort');
})
-
+
function simpleOption (type) {
describe(type, function(){
it('sets the ' + type + ' option', function(){

0 comments on commit 86a054a

Please sign in to comment.