Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
add search(), include() and exclude() methods, fix #37
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemedes committed May 20, 2015
1 parent bf26f3b commit b9680b1
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 14 deletions.
12 changes: 11 additions & 1 deletion lib/mongorito.js
Expand Up @@ -648,6 +648,16 @@ var Model = (function () {
return new Query(this._collection, this).remove(query); // collection, model
};

/**
* Drop collection
*
* @api public
*/

Model.drop = function* drop() {
yield this._collection.drop();
};

/**
* Set up an index
*
Expand Down Expand Up @@ -762,7 +772,7 @@ var Model = (function () {
// Setting up functions that have
// the same implementation
// and act as a bridge to Query
const methods = ['where', 'limit', 'skip', 'sort', 'exists', 'lt', 'lte', 'gt', 'gte', 'in', 'nin', 'and', 'or', 'ne', 'nor', 'populate', 'matches', 'match'];
const methods = ['where', 'limit', 'skip', 'sort', 'exists', 'lt', 'lte', 'gt', 'gte', 'in', 'nin', 'and', 'or', 'ne', 'nor', 'populate', 'matches', 'match', 'include', 'exclude', 'search'];

methods.forEach(function (method) {
Model[method] = function () {
Expand Down
115 changes: 110 additions & 5 deletions lib/query.js
Expand Up @@ -6,6 +6,7 @@

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

const assign = Object.assign || require('object.assign');
const is = require('is_js');

const isObjectID = require('./util').isObjectID;
Expand All @@ -21,7 +22,11 @@ var Query = (function () {
this.collection = collection;
this.model = model;
this.query = {};
this.options = { populate: {} };
this.options = {
populate: {},
sort: {},
fields: {}
};
this.lastKey = key;
}

Expand Down Expand Up @@ -94,6 +99,97 @@ var Query = (function () {
return this.matches.apply(this, arguments);
};

/**
* Include fields in a result
*
* @param {String} key
* @param {Mixed} value
* @api public
*/

Query.prototype.include = function include(key, value) {
var _this2 = this;

if (is.array(key)) {
var fields = key;

fields.forEach(function (key) {
_this2.include(key);
});
}

if (is.object(key)) {
(function () {
var fields = key;
var keys = Object.keys(fields);

keys.forEach(function (key) {
_this2.include(key, fields[key]);
});
})();
}

if (is.string(key)) {
this.options.fields[key] = value == undefined ? 1 : value;
}

return this;
};

/**
* Exclude fields from result
*
* @param {String} key
* @param {String} value
* @api public
*/

Query.prototype.exclude = function exclude(key, value) {
var _this3 = this;

if (is.array(key)) {
var fields = key;

fields.forEach(function (key) {
_this3.exclude(key);
});
}

if (is.object(key)) {
(function () {
var fields = key;
var keys = Object.keys(fields);

keys.forEach(function (key) {
_this3.exclude(key, fields[key]);
});
})();
}

if (is.string(key)) {
this.options.fields[key] = value == undefined ? 0 : value;
}

return this;
};

/**
* Search using text index
*
* @param {String} text
* @api public
*/

Query.prototype.search = function search(text) {
this.where({
'$text': {
'$search': text
}
});

return this;
};

/**
* Set query limit
*
Expand Down Expand Up @@ -129,8 +225,14 @@ var Query = (function () {
* @api public
*/

Query.prototype.sort = function sort(_sort) {
this.options.sort = _sort;
Query.prototype.sort = function sort(key, value) {
if (is.object(key)) {
assign(this.options.sort, key);
}

if (is.string(key) && value) {
this.options.sort[key] = value;
}

return this;
};
Expand Down Expand Up @@ -207,15 +309,18 @@ var Query = (function () {
* @api public
*/

Query.prototype.find = function* find(query) {
Query.prototype.find = function* find(query, options) {
this.where(query);

var model = this.model;
var options = this.options;

// query options
options = assign({}, this.options, options);

// fields to populate
var populate = Object.keys(options.populate);

// find
var docs = yield this.collection.find(this.query, options);

var i = 0;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -9,6 +9,7 @@
"koa-compose": "^2.3.0",
"lodash.result": "^3.1.2",
"monk": "^1.0.1",
"object.assign": "^2.0.1",
"pluralize": "^1.1.2"
},
"devDependencies": {
Expand Down
16 changes: 15 additions & 1 deletion src/mongorito.js
Expand Up @@ -699,6 +699,17 @@ class Model {
static remove (query) {
return new Query(this._collection, this).remove(query); // collection, model
}


/**
* Drop collection
*
* @api public
*/

static * drop () {
yield this._collection.drop();
}


/**
Expand Down Expand Up @@ -746,7 +757,10 @@ const methods = [
'nor',
'populate',
'matches',
'match'
'match',
'include',
'exclude',
'search'
];

methods.forEach(method => {
Expand Down
114 changes: 107 additions & 7 deletions src/query.js
Expand Up @@ -2,6 +2,7 @@
* Dependencies
*/

const assign = Object.assign || require('object.assign');
const is = require('is_js');

const isObjectID = require('./util').isObjectID;
Expand All @@ -16,7 +17,11 @@ class Query {
this.collection = collection;
this.model = model;
this.query = {};
this.options = { populate: {} };
this.options = {
populate: {},
sort: {},
fields: {}
};
this.lastKey = key;
}

Expand Down Expand Up @@ -86,6 +91,92 @@ class Query {
match () {
return this.matches.apply(this, arguments);
}


/**
* Include fields in a result
*
* @param {String} key
* @param {Mixed} value
* @api public
*/

include (key, value) {
if (is.array(key)) {
let fields = key;

fields.forEach(key => {
this.include(key);
});
}

if (is.object(key)) {
let fields = key;
let keys = Object.keys(fields);

keys.forEach(key => {
this.include(key, fields[key]);
});
}

if (is.string(key)) {
this.options.fields[key] = value == undefined ? 1 : value;
}

return this;
}


/**
* Exclude fields from result
*
* @param {String} key
* @param {String} value
* @api public
*/

exclude (key, value) {
if (is.array(key)) {
let fields = key;

fields.forEach(key => {
this.exclude(key);
});
}

if (is.object(key)) {
let fields = key;
let keys = Object.keys(fields);

keys.forEach(key => {
this.exclude(key, fields[key]);
});
}

if (is.string(key)) {
this.options.fields[key] = value == undefined ? 0 : value;
}

return this;
}


/**
* Search using text index
*
* @param {String} text
* @api public
*/

search (text) {
this.where({
'$text': {
'$search': text
}
});

return this;
}


/**
Expand Down Expand Up @@ -125,8 +216,14 @@ class Query {
* @api public
*/

sort (sort) {
this.options.sort = sort;
sort (key, value) {
if (is.object(key)) {
assign(this.options.sort, key);
}

if (is.string(key) && value) {
this.options.sort[key] = value;
}

return this;
}
Expand Down Expand Up @@ -207,17 +304,20 @@ class Query {
* @api public
*/

* find (query) {
* find (query, options) {
this.where(query);

let model = this.model;
let options = this.options;

// query options
options = assign({}, this.options, options);

// fields to populate
let populate = Object.keys(options.populate);


// find
let docs = yield this.collection.find(this.query, options);

let i = 0;
let doc;

Expand Down

0 comments on commit b9680b1

Please sign in to comment.