Navigation Menu

Skip to content

Commit

Permalink
Added aggregate()
Browse files Browse the repository at this point in the history
  • Loading branch information
Irrelon committed Apr 22, 2013
1 parent be39f6c commit 2b84436
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
55 changes: 35 additions & 20 deletions index.js
Expand Up @@ -136,7 +136,7 @@ var Monge = IgeEventingClass.extend({
var self = this;

if (!obj) { obj = {}; }
this._convertIds(obj);
this._convertIds(obj, options);

this.client.collection(collection, function (err, tempCollection) {
if (!err) {
Expand All @@ -146,7 +146,7 @@ var Monge = IgeEventingClass.extend({

// Callback the result
if (typeof(cb) === 'function') {
docs = docs !== undefined ? docs[0]._id : null;
docs = docs !== undefined ? String(docs[0]._id) : null;
cb(err, docs);
}
});
Expand Down Expand Up @@ -175,8 +175,8 @@ var Monge = IgeEventingClass.extend({

if (!searchObj) { searchObj = {}; }
if (!updateObj) { updateObj = {}; }
this._convertIds(searchObj);
this._convertIds(updateObj);
this._convertIds(searchObj, options);
this._convertIds(updateObj, options);

// Set some options defaults
if (options.safe === undefined) { options.safe = true; }
Expand Down Expand Up @@ -238,7 +238,7 @@ var Monge = IgeEventingClass.extend({

// Callback the result
if (typeof(cb) === 'function') {
docs !== undefined ? docs[0]._id : null;
docs !== undefined ? String(docs[0]._id) : null;
cb(err, docs);
}
});
Expand Down Expand Up @@ -276,7 +276,7 @@ var Monge = IgeEventingClass.extend({
}

if (!obj) { obj = {}; }
this._convertIds(obj);
this._convertIds(obj, options);

this.client.collection(collection, function (err, tempCollection) {
if (!err) {
Expand Down Expand Up @@ -365,7 +365,7 @@ var Monge = IgeEventingClass.extend({
distinct: function (collection, key, obj, options, cb) {
if (!obj) { obj = {}; }
if (!options) { options = {}; }
this._convertIds(obj);
this._convertIds(obj, options);

this.client.command({"distinct": collection, "key": key, "query": obj || {}}, options, function (err, dataArr) {
var data;
Expand Down Expand Up @@ -404,8 +404,8 @@ var Monge = IgeEventingClass.extend({

if (!searchObj) { searchObj = {}; }
if (!unSetObj) { unSetObj = {}; }
this._convertIds(searchObj);
this._convertIds(unSetObj);
this._convertIds(searchObj, options);
this._convertIds(unSetObj, options);

this.client.collection(collection, function (err, tempCollection) {
if (!err) {
Expand Down Expand Up @@ -452,8 +452,8 @@ var Monge = IgeEventingClass.extend({

if (!searchObj) { searchObj = {}; }
if (!updateObj) { updateObj = {}; }
this._convertIds(searchObj);
this._convertIds(updateObj);
this._convertIds(searchObj, options);
this._convertIds(updateObj, options);

this.client.collection(collection, function (err, tempCollection) {
if (!err) {
Expand Down Expand Up @@ -500,8 +500,8 @@ var Monge = IgeEventingClass.extend({

if (!searchObj) { searchObj = {}; }
if (!updateObj) { updateObj = {}; }
this._convertIds(searchObj);
this._convertIds(updateObj);
this._convertIds(searchObj, options);
this._convertIds(updateObj, options);

this.client.collection(collection, function (err, tempCollection) {
if (!err) {
Expand Down Expand Up @@ -538,7 +538,7 @@ var Monge = IgeEventingClass.extend({
if (options.single === undefined) { options.single = false; }

if (!obj) { obj = {}; }
this._convertIds(obj);
this._convertIds(obj, options);

this.client.collection(collection, function (err, tempCollection) {
if (!err) {
Expand All @@ -558,6 +558,19 @@ var Monge = IgeEventingClass.extend({
}
});
},

aggregate: function (collection, pipeline, options, cb) {
this.client.collection(collection, function (err, tempCollection) {
if (!err) {
// Got the collection
tempCollection.aggregate(pipeline, cb);
} else {
if (typeof(cb) === 'function') {
cb(err);
}
}
});
},

/**
* Converts a string into a new database ObjectID.
Expand All @@ -575,26 +588,28 @@ var Monge = IgeEventingClass.extend({
/**
* A private method for converting _id key/values to string and mongo ID objects.
* @param obj
* @param {String=} toType Sets the type to convert to, either 'string' or 'id'.
* @param {Object} options
* @private
*/
_convertIds: function (obj, toType) {
if (this._options.autoConvertIds) {
_convertIds: function (obj, options) {
options = options || {};

if (options.autoConvertIds || this._options.autoConvertIds) {
if (obj) {
if (obj instanceof Array) {
// Loop the objects in the array recursively
for (var i in obj) {
this._convertIds(obj[i]);
this._convertIds(obj[i], options);
}
} else {
if (obj._id) {
if (typeof(obj._id) === 'string') {
if (toType !== 'string') {
if (options.toType !== 'string') {
// Convert to a mongo id
obj._id = new this.client.bson_serializer.ObjectID(obj._id);
}
} else {
if (toType !== 'id') {
if (options.toType !== 'id') {
obj._id = String(obj._id);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"name": "monge",
"description": "An easy to use library for accessing and working with MongoDB.",
"keywords": ["mongodb", "mongo", "driver", "db", "easy"],
"version": "1.1.7",
"version": "1.2.1",
"repository": {
"type": "git",
"url": "https://github.com/coolbloke1324/monge"
Expand Down

0 comments on commit 2b84436

Please sign in to comment.