Comparing version 2.0.0 to 2.1.0
@@ -756,2 +756,32 @@ 'use strict'; | ||
/** | ||
* Get distinct | ||
* | ||
* @param {String} field for distinct | ||
* @param {Object} query - query to filter the results | ||
* @see http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#distinct | ||
* @api public | ||
*/ | ||
Model.distinct = function (field, query) { | ||
// collection, model | ||
return new Query(this._collection(), this).distinct(field, query); | ||
}; | ||
/** | ||
* Aggregation query | ||
* | ||
* @param {String} pipeline aggregation pipeline | ||
* @param {Object} options - Options to be passed to aggregation pipeline | ||
* @see http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#distinct | ||
* @api public | ||
*/ | ||
Model.aggregate = function (pipeline) { | ||
return new Query(this._collection(), this).aggregate(pipeline); | ||
}; | ||
/** | ||
* Find all documents in a collection | ||
@@ -775,6 +805,3 @@ * | ||
Model.findOne = function (query) { | ||
return this.find(query) | ||
.then(function (docs) { | ||
return docs[0]; | ||
}); | ||
return new Query(this._collection(), this).findOne(query); | ||
}; | ||
@@ -791,3 +818,3 @@ | ||
Model.findById = function (id) { | ||
return this.findOne({ _id: id }); | ||
return new Query(this._collection(), this).findById(id); | ||
}; | ||
@@ -794,0 +821,0 @@ |
106
lib/query.js
@@ -66,2 +66,6 @@ 'use strict'; | ||
if (is.array(value)) { | ||
value = { $in: value }; | ||
} | ||
this.query[key] = value; | ||
@@ -116,5 +120,3 @@ } | ||
}); | ||
} | ||
if (is.object(key)) { | ||
} else if (is.object(key)) { | ||
let fields = key; | ||
@@ -153,5 +155,3 @@ let keys = Object.keys(fields); | ||
}); | ||
} | ||
if (is.object(key)) { | ||
} else if (is.object(key)) { | ||
let fields = key; | ||
@@ -190,4 +190,44 @@ let keys = Object.keys(fields); | ||
/** | ||
* Get distinct | ||
* | ||
* @param {String} field for distinct | ||
* @param {Object} query - query to filter the results | ||
* @see http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#distinct | ||
* @api public | ||
*/ | ||
Query.prototype.distinct = function (field, query) { | ||
let self = this; | ||
this.where(query); | ||
return this.collection.then(function (collection) { | ||
return collection.distinct(field, self.query); | ||
}); | ||
}; | ||
/** | ||
* Aggregation query | ||
* | ||
* @param {String} pipeline aggregation pipeline | ||
* @param {Object} options - Options to be passed to aggregation pipeline | ||
* @see http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#aggregate | ||
* @api public | ||
*/ | ||
Query.prototype.aggregate = function (pipeline) { | ||
return this.collection.then(function (collection) { | ||
let cursor = collection.aggregate(pipeline, { cursor: { batchSize: 1 } }); | ||
return cursor | ||
.toArray() | ||
.then(function (docs) { | ||
cursor.close(); | ||
return docs; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Set query limit | ||
@@ -324,5 +364,4 @@ * | ||
let Model = this.model; | ||
let self = this; | ||
this.where(query); | ||
query = assign({}, this.query, query); | ||
@@ -336,4 +375,16 @@ // query options | ||
// ensure _id is ObjectId | ||
if (this.query._id) { | ||
this.query._id = toObjectId(this.query._id); | ||
if (query._id && !is.object(query._id)) { | ||
if (typeof query._id === 'object') { | ||
if (query._id.$in) { | ||
let convertedIds = []; | ||
query._id.$in.forEach(function (id) { | ||
convertedIds.push(toObjectId(id)); | ||
}); | ||
query._id.$in = convertedIds; | ||
} | ||
} else { | ||
query._id = toObjectId(query._id); | ||
} | ||
} | ||
@@ -344,3 +395,3 @@ | ||
.then(function (collection) { | ||
let cursor = collection.find(self.query, options); | ||
let cursor = collection.find(query, options); | ||
@@ -358,14 +409,19 @@ return cursor | ||
let childModel = options.populate[key]; | ||
let value = doc[key]; | ||
let idsArray = doc[key]; | ||
if (Array.isArray(value)) { | ||
value = Promise.map(value, function (id) { | ||
return childModel.findById(id); | ||
let promise = childModel.findById(idsArray); | ||
return promise.then(function (subdocs) { | ||
// reorder the received documents as ordered in the IDs Array | ||
let orderedDocuments = idsArray.slice(); | ||
subdocs.map(doc => { | ||
let id = toObjectId(doc.get('_id')); | ||
for (let index in idsArray) { | ||
if (idsArray[index].equals && idsArray[index].equals(id)) { | ||
orderedDocuments[index] = doc; | ||
} | ||
} | ||
}); | ||
} else { | ||
value = childModel.findById(value); | ||
} | ||
return value.then(function (value) { | ||
doc[key] = value; | ||
doc[key] = orderedDocuments; | ||
}); | ||
@@ -397,5 +453,5 @@ }).then(function () { | ||
/** | ||
* Find a document by ID | ||
* Find documents by ID | ||
* | ||
* @param {ObjectID} id - document id | ||
* @param {ObjectID} id - document id or Array of document ids | ||
* @api public | ||
@@ -405,3 +461,7 @@ */ | ||
Query.prototype.findById = function (id) { | ||
return this.findOne({ _id: id }); | ||
if (Array.isArray(id)) { | ||
let ids = id.map(id => toObjectId(id)); | ||
return this.find({ _id: { $in: ids } }); | ||
} | ||
return this.findOne({ _id: toObjectId(id) }); | ||
}; | ||
@@ -408,0 +468,0 @@ |
{ | ||
"name": "mongorito", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "ES6 generator-based MongoDB ODM. It rocks.", | ||
@@ -5,0 +5,0 @@ "author": "Vadim Demedes <vdemedes@gmail.com>", |
32566
1154
5