【发布时间】:2020-03-28 20:10:20
【问题描述】:
我正在为一个我不知道如何真实的查询而苦苦挣扎......我假装做一个搜索输入,在那里我想找到一个客户并从这个客户那里获得我需要的所有信息。 我有 3 个收藏。
客户集合
{
_id: '5e3376249110c43528f31101'
name: 'Jhon',
rate: '5e790595b0d727313cb56d3b',
}
费率收集
{
_id: '5e790595b0d727313cb56d3b'
title: 'Rate for sellers'
margin: 10,
}
配置集合
{
_id: '5e7276249110c43528f21402'
idRate: '5e790595b0d727313cb56d3b'
maring: 8,
}
我正在执行的查询是...(我只收到来自我的应用的 searchTerm)
clienteModel.aggregate([
//in the first stage i find the client by searchTerm that contains the Name;
{
$match: {
$or: [
{ name: { '$regex': req.body.searchTerm, '$options': 'i' } },
]
}
},
//in the second stage i would like to receive only the Rate that match with the client.rate
{
$lookup: {
from: "rates",
as: "rate",
pipeline: [
{
$match: {
_id: $rate
}
}
]
}
}
//in the third stage, i would like to bring the configs that match with client.rate
{
$lookup: {
from: "configs",
as: "config",
pipeline: [
{
$match: {
idRate: $rate
}
}
]
}
}
])
但是这个查询不起作用,它说 $rate 没有定义...我想我不知道它是如何工作的,我该如何声明 $rate?我认为如果 cliente.rate 是 clientModel 的属性,它是定义的......请帮助
【问题讨论】:
标签: mongodb