mongodb - 填充后的条件查询mongo db不返回数据
问题描述
我是 mongo 和 node 的新手,我在过滤时遇到了问题。
我有一个客户架构和钱包架构。当我插入一个新客户时,它正在为该客户填充一个钱包。这两个模型的架构如下。
客户模型.js
var Schema = mongoose.Schema;
const Genders = Object.freeze({
Male: 'male',
Female: 'female',
Other: 'other',
});
var CustomerSchema = new Schema({
reg_date: { type: Date, default: Date.now },
first_name: String,
last_name: String,
gender: {
type: String,
enum: Object.values(Genders),
},
wallet_balance: { type: Number, default: 0 },
status:{type:Boolean,default:true},
wallet:{type:mongoose.Schema.Types.ObjectId,ref:'Wallet'},
customer_rank: String
});
module.exports = mongoose.model('Customer', CustomerSchema);
Wallet.model.js
var Schema = mongoose.Schema;
var TransactionSchema = new Schema({
reason: String,
deposit_by: Number,
transaction_type: String,
transacted_balnace:Number
})
var WalletSchema = new Schema({
user_id:String,
transaction_log: [TransactionSchema],
balance: { type: Number, default: 0 },
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now }
});
WalletSchema.plugin(uniqueValidator);
module.exports = mongoose.model('Wallet', WalletSchema);
我想根据原因获取客户详细信息。所以,代码如下。
CustomerModel.find({}, { "password": 0 }).populate({
path: 'wallet',
match: { reason: { $in: [ "service charge" ] } },
select: 'transaction_log'
}).exec().then(data => {
if (data) {
res.status(200).send({ status: true, data: data })
}
else {
res.send({ status: false, data: [] })
}
})
它没有归还钱包,但是如果我删除匹配属性它工作正常。如果我得到解决方案将非常有帮助。提前致谢。
解决方案
推荐阅读
- ruby-on-rails - 在入口脚本中捆绑安装和纱线安装 - Docker
- php - AJAX 将值从 js 发送到 php
- php - 如何使用 php 删除阿拉伯语文本中的特殊字符(多余的空格)?
- python - 无法在 Jupyter 笔记本中使用预装的 python 包
- python - 在 python 中解析汤
- r - 如何在类网络对象的**超图**中转换矩阵
- react-native - 将 Skype for business 应用程序与 React Native 移动应用程序深度链接
- python - Django - 如何使用 JQuery Ajax 插入多个表单数据
- python - 如何小写我的部分网址地址的第一个字母?
- python - 如何在 Python 3 中扩展 datetime.date?