- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试编写一个 mongoose 查询来检索一组 Assets 以及这些 Assets 的最新交易。交易与 Assets 位于不同的集合中。
为此,我首先在 Assets 模型中创建了一个虚拟数组,以将 Assets 与交易联系起来。
schema.virtual('transactions', {
ref: 'transaction',
localField: '_id',
foreignField: '_asset',
justOne: false
})
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);
});
}
最佳答案
这确实是一个棘手的问题。这里的基础知识是 .populate()
并且当然不是“填充虚拟”,其设计目的不是像您在这里期望的那样工作。
问题说明
本质上 .populate()
本质上是向 MongoDB 发出的另一个查询以检索相关数据。为此,它基本上使用 $in
发出一个查询,其中包含要匹配的目标的所有“相关字段”值。
"issue 4321" 的核心是,使用诸如 "sort"
和 "limit"
之类的选项,需要提供此类 $in
参数的实际查询实际上是一个 .aggregate()
语句,该语句能够“获得为每个分组的最后 n
结果 key ”。这实际上并不是 mongoose 目前向 MongoDB 发出的问题,目前通过可用操作对 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
旨在避免的。
n
结果。
"recent"
交易列表。这为您提供了包含的场景中的文档,如下所示:
{
"_id" : ObjectId("5959e34adf833e1451a32661"),
"__v" : 0,
"name" : "One",
"recent" : [
ObjectId("5959e34bdf833e1451a32676"),
ObjectId("5959e34bdf833e1451a32674"),
ObjectId("5959e34bdf833e1451a32672"),
ObjectId("5959e34bdf833e1451a32670"),
ObjectId("5959e34bdf833e1451a3266e")
]
}
$slice
项的数组中取出
"recent"
。在 list 中,我这样做:
Master.find().select({ 'recent': { '$slice': 1 } })
"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”,因此“第一”项始终是要添加的与父相关的“最新”事务。
$slice
修饰符来将
"recent"
数组保持在
n
项的限制范围内。因此,当添加新项目时,“最旧”项目将被“从列表中删除”。
"recent"
数组。然后对新“页面”的额外请求,可以简单地通过
"recent"
过滤掉包含在
$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"
}
]
}
]
关于node.js - Mongoose 用排序和限制填充虚拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877203/
padding:initial 比 padding:0 有什么优势吗?示例: textarea { padding: 0; } Hello, world! 最佳答案 它们的意思是一
我尝试通过按钮填充 JList,然后在先前填充的 Jlist 上使用 DoubleClick 填充 JTextField。 代码: private void extractUsedVariables
我正在尝试做 var width = ($(this).width() + $(this).css('padding-left') + $(this).css('padding-right' ));
我在导航中添加了悬停效果,遗憾的是悬停也影响了上面的文字。如何在不影响文本位置的情况下向导航添加悬停? 可悲的是,我找不到解决这个问题的方法。 HTML 模板:http://projects.help
我是 F# 初学者,下面代码中的 %-5s 和 %5s 有什么作用?我认为它提供了空间填充,但我不确定它是如何填充的? printfn "%-5s %5s" "a" "b" 当我尝试 prin
我需要选择带狗的用户(带 type 等于“狗”的宠物) var User = Waterline.Collection.extend({ identity: 'user', attribute
我一直在尝试让 Excel 在一组列上应用公式,然后将模式扩展到整个行集。 这导致了以下代码: For i = 0 To avgsheetNames.Count - 1 If Contains(CSt
随着 Flutter 2.0 的发布,FlatButton已被替换为 TextButton . 因此,填充属性不再直接可用,而是作为 ButtonStyle属性(property)。 我的问题是,我该
这似乎是一个简单的问题,但我已经尝试了一个小时,似乎无法弄清楚。 我要做的就是用 Canvas 填充 MainWindow。我找不到任何允许这样做的属性,我能想到的唯一方法是设置 Canvas.Wid
这是a website具有移动 View 。 网站宽度为 640 像素,但 iPhone 以 678 像素渲染文档。在 Android 中看起来很棒。 我添加了视口(viewport)元: 主体 C
我正在使用 GridBagLayout到(当前)显示两行。我知道这种布局对于这项任务来说太过分了,但我正在努力学习如何使用它。问题是我已将两个面板添加到两个单独的行中,并且内容周围存在巨大差距(请参见
我有以下代码已传递给我并创建多边形: var map; function initialize() { var myLatlng = new google.maps.LatLng(-36.4
我在 Jpanel 中有一些项目,然后将其推到顶部并用作基本搜索引擎的工具栏。我遇到一个问题,因为没有足够的空间,所以我的最后一个组合框没有显示。但是,左侧有很多空白空间,我需要移动所有内容来填充 J
我创建了带有阈值的二进制图像。如下图所示如何改变白色形状的颜色以使其可索引? 到目前为止,这是我的代码: void threshold() { cv::Mat src_8uc3_img = c
我有一个 JTable,我想知道是否有更好的方法来填充它,这是我的代码: //Metodo para llenar un jtable con datos de la base public stat
我想要做的是裁剪一个卷以删除所有不相关的数据。例如,假设我有一个 100x100x100 的体积,其中填充了 0,但其中的 50x50x50 体积则填充了 1。如何从原始体积中获得裁剪后的 50x50
因此,我正在创建一种对一组数字进行洗牌的方法,其想法是创建这些数字的总体。因此,我创建了一个循环,对数字进行洗牌,然后将其添加到数组列表中,但是经过一些调试语句后,我发现它确实对数字进行洗牌,但只将最
假设我有这两个类: public class A where T : IEntityWithID, new() { private static EntityInfo entityInfo =
我正在尝试添加用户输入的两个大整数作为字符串。当两个输入字符串的长度不同时,我尝试用零填充较短的数字,但它不起作用。因此,如果我输入 456 和 7,它会给出 3,前面有一些随机字符。感谢您的任何建议
这是我将内容打印到表格 View 的代码 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: Index
我是一名优秀的程序员,十分优秀!