有条件地用猫鼬填充
Conditional populate with mongoose
我遇到了 Mongoose
的问题。我有多个模型:
- 交易
- 钱包
- 用户
- 组织
我正在尝试获取交易。
每个交易包含一个 senderWalletId
和一个 receiverWalletId
,其中有一个 reference
到 Wallet
。
钱包有一个名为 type
的字段可以是 Organization
或 User
和一个名为 owner
的字段包含对 User
或 Organization
。我通过指向同一钱包的 type
属性 值的引用路径实现了这一点,就像这样。
type: {
type: String,
enum: [
'User',
'Organization',
'Moneypot',
],
},
owner: {
type: Schema.Types.ObjectId,
refPath: "type"
}
现在,我想填充 receiverWalletId
和 senderWalletId
的 owner
。如果类型是 Organization
,我想检索 name
、phoneNumber
和 email
。如果类型是 User
,我想检索 firstName
、lastNmae
、email
和 phoneNumber
。
我通过这段代码部分实现了这一点:
.populate({
path: 'senderWalletId',
select: ['owner', 'type'],
match: { type: { $eq: 'User' } },
populate: {
path: 'owner',
select: ['firstName', 'lastName', 'phoneNumber', 'email']
}
})
.populate({
path: 'receiverWalletId',
select: ['owner', 'type'],
match: { type: { $eq: 'User' } },
populate: {
path: 'owner',
select: ['firstName', 'lastName', 'phoneNumber', 'email']
}
})
现在的问题是,当类型等于 organization
时,如何让这些群体检索 name
而不是 firstName
和 lastName
我通过将所有属性放在 select 数组中找到了解决这个问题的方法,如下所示:
.populate({
path: 'senderWalletId',
select: ['owner', 'type'],
match: { type: { $eq: 'User' } },
populate: {
path: 'owner',
select: ['name', 'firstName', 'lastName', 'phoneNumber', 'email']
}
})
.populate({
path: 'receiverWalletId',
select: ['owner', 'type'],
match: { type: { $eq: 'User' } },
populate: {
path: 'owner',
select: ['name', 'firstName', 'lastName', 'phoneNumber', 'email']
}
})
NoSQL 的力量现在开始发挥作用,因为返回的数组包含 firstName
和 lastName
(如果钱包类型是 User
),它包含 name
organization
的情况
我遇到了 Mongoose
的问题。我有多个模型:
- 交易
- 钱包
- 用户
- 组织
我正在尝试获取交易。
每个交易包含一个 senderWalletId
和一个 receiverWalletId
,其中有一个 reference
到 Wallet
。
钱包有一个名为 type
的字段可以是 Organization
或 User
和一个名为 owner
的字段包含对 User
或 Organization
。我通过指向同一钱包的 type
属性 值的引用路径实现了这一点,就像这样。
type: {
type: String,
enum: [
'User',
'Organization',
'Moneypot',
],
},
owner: {
type: Schema.Types.ObjectId,
refPath: "type"
}
现在,我想填充 receiverWalletId
和 senderWalletId
的 owner
。如果类型是 Organization
,我想检索 name
、phoneNumber
和 email
。如果类型是 User
,我想检索 firstName
、lastNmae
、email
和 phoneNumber
。
我通过这段代码部分实现了这一点:
.populate({
path: 'senderWalletId',
select: ['owner', 'type'],
match: { type: { $eq: 'User' } },
populate: {
path: 'owner',
select: ['firstName', 'lastName', 'phoneNumber', 'email']
}
})
.populate({
path: 'receiverWalletId',
select: ['owner', 'type'],
match: { type: { $eq: 'User' } },
populate: {
path: 'owner',
select: ['firstName', 'lastName', 'phoneNumber', 'email']
}
})
现在的问题是,当类型等于 organization
name
而不是 firstName
和 lastName
我通过将所有属性放在 select 数组中找到了解决这个问题的方法,如下所示:
.populate({
path: 'senderWalletId',
select: ['owner', 'type'],
match: { type: { $eq: 'User' } },
populate: {
path: 'owner',
select: ['name', 'firstName', 'lastName', 'phoneNumber', 'email']
}
})
.populate({
path: 'receiverWalletId',
select: ['owner', 'type'],
match: { type: { $eq: 'User' } },
populate: {
path: 'owner',
select: ['name', 'firstName', 'lastName', 'phoneNumber', 'email']
}
})
NoSQL 的力量现在开始发挥作用,因为返回的数组包含 firstName
和 lastName
(如果钱包类型是 User
),它包含 name
organization