我在NodeJS中使用mongoose,我有两个主要模型,交易和发票,两者都可以在平衡模型中,模型定义如下:
const BalanceSchema = new Schema({
transactions: [{ type: Schema.Types.ObjectId, ref: 'Transaction'}],
invoices: [{ type: Schema.Types.ObjectId, ref: 'Invoice'}],
references: [filesDescription],
});
const TransactionSchema = new Schema({
paybook: PaybookTransaction,
});
我想获得所有交易,并知道他们是否有平衡参考,除了咨询后我的结果:
[
{
_id: 'transaction_id'
paybook: {}
balance: 'balance_id'
}
]
这可能吗,或者我需要用js吗?
更新1
示例数据:
我有数据表事务:
{
"_id" : ObjectId("5988b1f62442837f5b8b911d"),
"updatedAt" : ISODate("2017-08-09T19:47:14.657Z"),
"createdAt" : ISODate("2017-08-09T19:47:14.657Z"),
"paybook" : {
"id_account" : "59763bc0244283dd798b4a57",
"id_account_type" : "520d3aa93b8e778e0d000000",
"id_credential" : "597a2fda0c212aa7648b58d3",
},
}
表格平衡数据如:
{
"_id" : ObjectId("598ba359ff32954ffb22da5a"),
"updatedAt" : ISODate("2017-08-10T01:43:31.668Z"),
"createdAt" : ISODate("2017-08-10T00:05:45.491Z"),
"references" : [],
"invoices" : [],
"transactions" : [
ObjectId("5988b1f62442837f5b8b911d")
]
}
我想获得所有在Balance中有引用的Transactions。就像我在json参考中给你看的那样。