hoe*_*kma 3 mongoose mongodb node.js mongodb-query
我正在尝试编写一个mongoose查询,该查询检索一组资产以及这些资产的最新事务.交易与资产分开收集.
为此,首先我在资产模型中创建了一个虚拟阵列,以将资产链接到事务.
schema.virtual('transactions', {
ref: 'transaction',
localField: '_id',
foreignField: '_asset',
justOne: false
})
Run Code Online (Sandbox Code Playgroud)
然后我在node.js表达控制器中使用.populate进行查询(注意硬编码的"limit: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);
});
}
Run Code Online (Sandbox Code Playgroud)
我的测试系统中有3个资产,每个资产都有一堆交易,它返回前两个资产的旧交易,而第三个资产则没有.
当我删除"limit:1"并返回所有交易时,它会正确排序并返回所有三个资产的交易.
我相信我遇到了这个错误:
https://github.com/Automattic/mongoose/issues/4321
关于优雅的解决方法的任何想法?
退回所有交易将不是一个可行的长期解决方案.
对于 2021 年阅读本文的人,mongoose 5.12.3可以在模型中完成以下操作:
schema.virtual('transactions', {
ref: 'transaction',
localField: '_id',
foreignField: '_asset',
justOne: false,
options: { sort: { 'createdAt': -1}, limit: 1},
})
Run Code Online (Sandbox Code Playgroud)
然后在控制器中:
exports.getList = function (req, res) {
Model
.find({})
.populate({
path: 'transactions',
})
.lean()
.exec(function (err, model) {
if (err)
res.send(err);
res.json(model);
});
}
Run Code Online (Sandbox Code Playgroud)
这确实是一个卷曲的问题.这里的基础是,.populate()当然不是"填充虚拟",不是像你期望的那样工作.
本质上.populate()是发给MongoDB的另一个查询,用于检索相关数据.为此,它基本上发出一个查询,$in其中包含要匹配的目标的所有"相关字段"值.
What is at the core of the "issue 4321" is that with such options as "sort" and "limit" the actual query that needs to be provided with such an $in argument is in fact an .aggregate() statement that would be able to "get the last n results grouped for each key". This is not actually what mongoose presently issues to MongoDB, nor is it really practical to group n items by available operations at this time.
You can work around that using .aggregate() manually, as demonstrated at the end of the provided listing, but of course only practically in a limited number of cases.
// 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
}}
])
Run Code Online (Sandbox Code Playgroud)
It's not great and not a really performant solution, but better than other similar alternatives. There is I believe though a still better way.
For your case here, and I would suspect many similar cases, you don't want to include the "full" transactions list within the parent documents even as references due to the potential size of the arrays produced. This sort of "anti-pattern" is generally what "virtual populate" and indeed $lookup are designed to avoid.
In your particular use case of "get the latest transaction" though, neither of those are a viable solution. Since both would essentially require looking at "all" transactions and then only retrieving n results from them.
因此,这里的"最新"或实际上"最近"的情况实际上回落到"嵌入"(至少是参考)"有限数量"以便提供可行的解决方案.所以提案就是这样做,并"recent"在父本身内保留一份交易清单.这为您提供了包含的场景中的文档,如下所示:
{
"_id" : ObjectId("5959e34adf833e1451a32661"),
"__v" : 0,
"name" : "One",
"recent" : [
ObjectId("5959e34bdf833e1451a32676"),
ObjectId("5959e34bdf833e1451a32674"),
ObjectId("5959e34bdf833e1451a32672"),
ObjectId("5959e34bdf833e1451a32670"),
ObjectId("5959e34bdf833e1451a3266e")
]
}
Run Code Online (Sandbox Code Playgroud)
注意到这些并非"全部"相关交易,而只是"最新"交易.关键是只保留适合目的的"小"列表.
通过这种方式,您可以直接查询"父",并直接查询项目所$slice包含的数组"recent".在列表中我这样做:
Master.find().select({ 'recent': { '$slice': 1 } })
Run Code Online (Sandbox Code Playgroud)
That returns the "latest" entry from the array without any other query to the server. It's the "latest" in this case, because instead we "prepend" items to this array at the same time as we write to the "transactions" collection, which has everything:
Transaction.create({ _asset: master._id, amount: data.amount })
.then(transaction =>
Master.update(
{ _id: transaction._asset },
{ "$push": {
"recent": {
"$each": [transaction._id],
"$position": 0,
"$slice": 5
}
}}
)
Run Code Online (Sandbox Code Playgroud)
The key element there is the $push to the array in the parent which is modified with the $position to "preprend" at the beginning of the array so the "first" item is always the "latest" transaction to be added related to the parent.
Then the $slice modifier is used here to keep the "recent" array to a limit of n items only. So the "oldest" items will be "pushed off" the list as newer items are added.
There is then in addition another practical purpose here that when listing the "master" and "transactions" in a paging scenario, the first request can use the "recent" array directly. Then additional request for new "pages", can simply filter out the items contained within the "recent" array via $nin and use the regular .skip() and .limit() or alternate "range" paging practice to retrieve each "page" of results.
To demonstrate all concepts in full, see the listing and all the generated results below.
Demonstration listing:
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();
}
);
Run Code Online (Sandbox Code Playgroud)
Demonstration Output
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"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1442 次 |
| 最近记录: |