我正在尝试编写一个猫鼬查询,以检索一组资产以及这些资产的最新交易。交易与资产在单独的集合中。
为此,我首先在资产模型中创建了一个虚拟数组,以将资产链接到事务。
schema.virtual('transactions', { ref: 'transaction', localField: '_id', foreignField: '_asset', justOne: false })
然后,我在node.js表达控制器中使用.populate进行查询(请注意,硬编码的“限制:1”有时会变成N):
exports.getList = function (req, res) { Model .find({}, { __v: 0 }) .populate({ path: 'transactions', options: { sort: { 'created_at': -1}, limit: 1}, }) .lean() .exec(function (err, model) { if (err) res.send(err); res.json(model); }); }
在我的测试系统中有3个资产,每个资产都有一堆交易,它会为前两个资产返回旧交易,而对于第三个资产则不返回任何交易。
当我删除“ limit:1”并返回所有交易时,它会正确排序并返回所有三个资产的交易。
我相信我遇到了这个错误:
https://github.com/Automattic/mongoose/issues/4321
关于优雅的解决方法有什么想法吗? 返回所有交易将不是一个可行的长期解决方案。
这确实是一个棘手的问题。这里的基础是,.populate()当然也不是“用虚拟机填充”,并不是为了像您期望的那样工作。
.populate()
本质上,实质上.populate()是向MongoDB发出的另一个查询,以检索相关数据。为此,它基本上发出一个查询,$in其中包含要匹配的目标的所有“相关字段”值。
$in
“问题4321”的核心是,具有这样的选项,"sort"而"limit"需要提供此类$in参数的实际查询实际上是一条.aggregate()语句,该语句将能够 “获取n为每个键分组的最后结果”。这实际上不是Mongoose当前发布给MongoDB的东西,这时n按可用操作对项目进行分组也不现实。
"sort"
"limit"
.aggregate()
n
您可以.aggregate()手动解决该问题,如提供的清单末尾所示,但是当然实际上仅在少数情况下。
// Get latest transactions for each master Transaction.aggregate([ { '$match': { '_asset': { '$in': masters.map(m => m._id) } }}, { '$sort': { '_asset': 1, 'createdAt': -1 } }, { '$group': { '_id': '$_asset', 'amount': { '$first': '$amount' }, 'createdAt': { '$first': '$createdAt' }, 'updatedAt': { '$first': '$updatedAt' }, 'did': { '$first': '$_id' } }}, { '$project': { '_id': '$did', '_asset': '$_id', 'amount': 1, 'createdAt': 1, 'updatedAt': 1 }} ])
这不是很好,也不是一个真正有效的解决方案,但是比其他类似的替代方案要好。我相信,尽管还有更好的方法。
对于您这里的情况,我会怀疑很多类似的情况,由于所产生的数组的潜在大小,您甚至不想在父文档中包括“完整”事务列表作为参考。这种“反模式”通常是“虚拟人群”所使用的,实际上$lookup是要避免的。
$lookup
但是,在您的特定用例 “获取最新交易”中 ,这两个都不是可行的解决方案。因为两者本质上都需要查看“所有”交易,然后才n从中检索结果。
因此,这里的“最新”或实际上是“最新”的情况实际上是退回到“嵌入”(至少是引用)的“有限数量”,以提供可行的解决方案。因此,提案就是要做到这一点,并"recent"在父公司内部保留交易清单。这为您提供了所包含场景中的文档,如下所示:
"recent"
{ "_id" : ObjectId("5959e34adf833e1451a32661"), "__v" : 0, "name" : "One", "recent" : [ ObjectId("5959e34bdf833e1451a32676"), ObjectId("5959e34bdf833e1451a32674"), ObjectId("5959e34bdf833e1451a32672"), ObjectId("5959e34bdf833e1451a32670"), ObjectId("5959e34bdf833e1451a3266e") ] }
在此请注意,这些并非“全部”相关交易,而仅是“最新”交易。关键是仅保留一个适合此目的的“小”列表。
这样,您可以直接查询“父级”,而仅查询$slice包含在"recent"项中的数组。在清单中,我这样做是:
$slice
Master.find().select({ 'recent': { '$slice': 1 } })
这将从阵列中返回“最新”条目,而无需对服务器进行任何其他查询。在这种情况下,它是“最新的”,因为相反,我们在写入"transactions"包含所有内容的集合的同时,将项目“前置”到了该数组中:
"transactions"
Transaction.create({ _asset: master._id, amount: data.amount }) .then(transaction => Master.update( { _id: transaction._asset }, { "$push": { "recent": { "$each": [transaction._id], "$position": 0, "$slice": 5 } }} )
$push父$position级数组中的关键元素在数组的开头用前缀“ preprend”进行了修改,因此“第一”项始终是要添加的与父级相关的“最新”事务。
$push
$position
然后在$slice这里使用修饰符将"recent"数组保持在限制n项目数量。因此,当添加较新的项目时,“最旧的”项目将“从”列表中删除。
然后,这里还有另一个实际目的,即在分页方案中列出“主”和“事务”时,第一个请求可以"recent"直接使用该数组。然后,对新“页面”的其他请求可以简单地通过过滤掉"recent"数组中包含的项目,$nin并使用常规.skip()和/ .limit()或备用“范围”分页实践来检索结果的每个“页面”。
$nin
.skip()
.limit()
为了完整地演示所有概念,请参见下面的清单和所有生成的结果。
演示清单:
const async = require('async'), mongoose = require('mongoose'), Schema = mongoose.Schema; mongoose.set('debug',true); mongoose.Promise = global.Promise; mongoose.connect('mongodb://localhost/prepend'); const transactionSchema = new Schema({ _asset: { type: Schema.Types.ObjectId, ref: 'Master' }, amount: Number },{ timestamps: { createdAt: 'createdAt' } }); const Transaction = mongoose.model('Transaction', transactionSchema); const masterSchema = new Schema({ name: String, recent: [{ type: Schema.Types.ObjectId, ref: 'Transaction' }] }); masterSchema.virtual('transactions', { ref: 'Transaction', localField: '_id', foreignField: '_asset', justOne: false }); const Master = mongoose.model('Master', masterSchema); function log(data) { console.log(JSON.stringify(data, undefined, 2)) } async.series( [ // Clean data (callback) => async.each(mongoose.models,(model,callback) => model.remove({},callback),callback), // Create Masters (callback) => Master.insertMany(['One','Two'].map( name => ({ name })),callback), // Add 10 transactions to each master (callback) => async.each(['One','Two'],(name,callback) => async.eachSeries( Array.apply(null,Array(10)).map((e,i) => ({ name, amount: i+1 })), (data,callback) => { Master.findOne({ name: data.name }) .then(master => Transaction.create({ _asset: master._id, amount: data.amount }) ) .then(transaction => Master.update( { _id: transaction._asset }, { "$push": { "recent": { "$each": [transaction._id], "$position": 0, "$slice": 5 } }} ) ) .then(res => callback()) .catch(callback) }, callback ), callback), // Show populated recent 1 entry only (callback) => Master.find().select({ 'recent': { '$slice': 1 } }) .populate('recent').exec((err,results) => { if (err) callback(err); log(results); callback(); }), // Populate recent - page 1 then fetch next page (callback) => async.waterfall( [ (callback) => Master.findOne({ name: 'One' }).populate('recent') .lean() .exec((err,master) => { if (err) callback(err); log(master); callback(null,{ _asset: master._id, exclude: master.recent.map( r => r._id ) }); }), (options,callback) => Transaction.find({ _asset: options._asset, _id: { '$nin': options.exclude } }).sort({ 'createdAt': -1 }).limit(5) .exec((err,transactions) => { if (err) callback(err); log(transactions) callback(); }) ], callback ), // Issue 4321 - Fix - Manual populate with aggregate (callback) => Master.find().select('-recent').exec() .then(masters => { // Get latest transactions for each master Transaction.aggregate([ { '$match': { '_asset': { '$in': masters.map(m => m._id) } }}, { '$sort': { '_asset': 1, 'createdAt': -1 } }, { '$group': { '_id': '$_asset', 'amount': { '$first': '$amount' }, 'createdAt': { '$first': '$createdAt' }, 'updatedAt': { '$first': '$updatedAt' }, 'did': { '$first': '$_id' } }}, { '$project': { '_id': '$did', '_asset': '$_id', 'amount': 1, 'createdAt': 1, 'updatedAt': 1 }} ]).exec((err,transactions) => { // Map latest transactions to master masters = masters.map( m => Object.assign( m.toObject(), { transactions: transactions.filter( t => t._asset.toHexString() === m._id.toHexString() ) } ) ); log(masters); callback(); }) }).catch(callback) ], (err) => { if (err) throw err; mongoose.disconnect(); } );
示范输出
Mongoose: transactions.remove({}, {}) Mongoose: masters.remove({}, {}) Mongoose: masters.insertMany([ { __v: 0, name: 'One', _id: 5959e34adf833e1451a32661, recent: [] }, { __v: 0, name: 'Two', _id: 5959e34adf833e1451a32662, recent: [] } ], null) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:14 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:14 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 1, _id: ObjectId("5959e34adf833e1451a32663"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 1, _id: ObjectId("5959e34adf833e1451a32664"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34adf833e1451a32664") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34adf833e1451a32663") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 2, _id: ObjectId("5959e34bdf833e1451a32665"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 2, _id: ObjectId("5959e34bdf833e1451a32666"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32666") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32665") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 3, _id: ObjectId("5959e34bdf833e1451a32667"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 3, _id: ObjectId("5959e34bdf833e1451a32668"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32668") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32667") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 4, _id: ObjectId("5959e34bdf833e1451a32669"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 4, _id: ObjectId("5959e34bdf833e1451a3266a"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a3266a") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32669") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 5, _id: ObjectId("5959e34bdf833e1451a3266b"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 5, _id: ObjectId("5959e34bdf833e1451a3266c"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a3266c") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a3266b") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 6, _id: ObjectId("5959e34bdf833e1451a3266d"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 6, _id: ObjectId("5959e34bdf833e1451a3266e"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a3266e") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a3266d") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 7, _id: ObjectId("5959e34bdf833e1451a3266f"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 7, _id: ObjectId("5959e34bdf833e1451a32670"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32670") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a3266f") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 8, _id: ObjectId("5959e34bdf833e1451a32671"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 8, _id: ObjectId("5959e34bdf833e1451a32672"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32672") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32671") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 9, _id: ObjectId("5959e34bdf833e1451a32673"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 9, _id: ObjectId("5959e34bdf833e1451a32674"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32674") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32673") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: masters.findOne({ name: 'Two' }, { fields: {} }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32662"), amount: 10, _id: ObjectId("5959e34bdf833e1451a32675"), __v: 0 }) Mongoose: transactions.insert({ updatedAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), createdAt: new Date("Mon, 03 Jul 2017 06:25:15 GMT"), _asset: ObjectId("5959e34adf833e1451a32661"), amount: 10, _id: ObjectId("5959e34bdf833e1451a32676"), __v: 0 }) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32661") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32676") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.update({ _id: ObjectId("5959e34adf833e1451a32662") }, { '$push': { recent: { '$each': [ ObjectId("5959e34bdf833e1451a32675") ], '$slice': 5, '$position': 0 } } }, {}) Mongoose: masters.find({}, { fields: { recent: { '$slice': 1 } } }) Mongoose: transactions.find({ _id: { '$in': [ ObjectId("5959e34bdf833e1451a32676"), ObjectId("5959e34bdf833e1451a32675") ] } }, { fields: {} }) [ { "_id": "5959e34adf833e1451a32661", "__v": 0, "name": "One", "recent": [ { "_id": "5959e34bdf833e1451a32676", "updatedAt": "2017-07-03T06:25:15.282Z", "createdAt": "2017-07-03T06:25:15.282Z", "_asset": "5959e34adf833e1451a32661", "amount": 10, "__v": 0 } ] }, { "_id": "5959e34adf833e1451a32662", "__v": 0, "name": "Two", "recent": [ { "_id": "5959e34bdf833e1451a32675", "updatedAt": "2017-07-03T06:25:15.280Z", "createdAt": "2017-07-03T06:25:15.280Z", "_asset": "5959e34adf833e1451a32662", "amount": 10, "__v": 0 } ] } ] Mongoose: masters.findOne({ name: 'One' }, { fields: {} }) Mongoose: transactions.find({ _id: { '$in': [ ObjectId("5959e34bdf833e1451a32676"), ObjectId("5959e34bdf833e1451a32674"), ObjectId("5959e34bdf833e1451a32672"), ObjectId("5959e34bdf833e1451a32670"), ObjectId("5959e34bdf833e1451a3266e") ] } }, { fields: {} }) { "_id": "5959e34adf833e1451a32661", "__v": 0, "name": "One", "recent": [ { "_id": "5959e34bdf833e1451a32676", "updatedAt": "2017-07-03T06:25:15.282Z", "createdAt": "2017-07-03T06:25:15.282Z", "_asset": "5959e34adf833e1451a32661", "amount": 10, "__v": 0 }, { "_id": "5959e34bdf833e1451a32674", "updatedAt": "2017-07-03T06:25:15.264Z", "createdAt": "2017-07-03T06:25:15.264Z", "_asset": "5959e34adf833e1451a32661", "amount": 9, "__v": 0 }, { "_id": "5959e34bdf833e1451a32672", "updatedAt": "2017-07-03T06:25:15.216Z", "createdAt": "2017-07-03T06:25:15.216Z", "_asset": "5959e34adf833e1451a32661", "amount": 8, "__v": 0 }, { "_id": "5959e34bdf833e1451a32670", "updatedAt": "2017-07-03T06:25:15.195Z", "createdAt": "2017-07-03T06:25:15.195Z", "_asset": "5959e34adf833e1451a32661", "amount": 7, "__v": 0 }, { "_id": "5959e34bdf833e1451a3266e", "updatedAt": "2017-07-03T06:25:15.180Z", "createdAt": "2017-07-03T06:25:15.180Z", "_asset": "5959e34adf833e1451a32661", "amount": 6, "__v": 0 } ] } Mongoose: transactions.find({ _id: { '$nin': [ ObjectId("5959e34bdf833e1451a32676"), ObjectId("5959e34bdf833e1451a32674"), ObjectId("5959e34bdf833e1451a32672"), ObjectId("5959e34bdf833e1451a32670"), ObjectId("5959e34bdf833e1451a3266e") ] }, _asset: ObjectId("5959e34adf833e1451a32661") }, { sort: { createdAt: -1 }, limit: 5, fields: {} }) [ { "_id": "5959e34bdf833e1451a3266c", "updatedAt": "2017-07-03T06:25:15.164Z", "createdAt": "2017-07-03T06:25:15.164Z", "_asset": "5959e34adf833e1451a32661", "amount": 5, "__v": 0 }, { "_id": "5959e34bdf833e1451a3266a", "updatedAt": "2017-07-03T06:25:15.135Z", "createdAt": "2017-07-03T06:25:15.135Z", "_asset": "5959e34adf833e1451a32661", "amount": 4, "__v": 0 }, { "_id": "5959e34bdf833e1451a32668", "updatedAt": "2017-07-03T06:25:15.080Z", "createdAt": "2017-07-03T06:25:15.080Z", "_asset": "5959e34adf833e1451a32661", "amount": 3, "__v": 0 }, { "_id": "5959e34bdf833e1451a32666", "updatedAt": "2017-07-03T06:25:15.039Z", "createdAt": "2017-07-03T06:25:15.039Z", "_asset": "5959e34adf833e1451a32661", "amount": 2, "__v": 0 }, { "_id": "5959e34adf833e1451a32664", "updatedAt": "2017-07-03T06:25:15.009Z", "createdAt": "2017-07-03T06:25:15.009Z", "_asset": "5959e34adf833e1451a32661", "amount": 1, "__v": 0 } ] Mongoose: masters.find({}, { fields: { recent: 0 } }) Mongoose: transactions.aggregate([ { '$match': { _asset: { '$in': [ 5959e34adf833e1451a32661, 5959e34adf833e1451a32662 ] } } }, { '$sort': { _asset: 1, createdAt: -1 } }, { '$group': { _id: '$_asset', amount: { '$first': '$amount' }, createdAt: { '$first': '$createdAt' }, updatedAt: { '$first': '$updatedAt' }, did: { '$first': '$_id' } } }, { '$project': { _id: '$did', _asset: '$_id', amount: 1, createdAt: 1, updatedAt: 1 } } ], {}) [ { "_id": "5959e34adf833e1451a32661", "__v": 0, "name": "One", "transactions": [ { "amount": 10, "createdAt": "2017-07-03T06:25:15.282Z", "updatedAt": "2017-07-03T06:25:15.282Z", "_id": "5959e34bdf833e1451a32676", "_asset": "5959e34adf833e1451a32661" } ] }, { "_id": "5959e34adf833e1451a32662", "__v": 0, "name": "Two", "transactions": [ { "amount": 10, "createdAt": "2017-07-03T06:25:15.280Z", "updatedAt": "2017-07-03T06:25:15.280Z", "_id": "5959e34bdf833e1451a32675", "_asset": "5959e34adf833e1451a32662" } ] } ]