【发布时间】:2020-06-04 10:33:27
【问题描述】:
var query = req.params.query;
const messages = await Message.aggregate([
{
$lookup: {
from: 'users',
localField: 'reference_id',
foreignField: '_id',
as: 'users'
}
},
{'text': {'$regex': `.*${query}.*`}}
])
$lookup 为我提供了正确的信息,但 $regex 无法正常工作当添加 {'text': {'$regex': .*${query}.*}} 告诉我在 $lookup 我有数据之后,参数必须是聚合管道运算符
{
"response": {
"messages": [
{
"_id": "5e4a8e640b92852e110af62f",
"text": "Gsshha",
"reference_id": null,
"date": 1581944420262,
"users": [
{
"_id": "5e4a8d2d3952132a08ae5764",
"phone": "1111111111",
"first_name": "Test1",
"last_name": "Test1",
"timestamp": 1581944109860
}
]
},
{
"_id": "5e4ce3a4f55dd93292cbe149",
"unreads": [
"text": "Hi",
"reference_id": null,
"date": 1582097316013,
"users": [
{
"_id": "5e4a8d2d3952132a08ae5764",
"phone": "1111111111",
"first_name": "Test1",
"last_name": "Test1",
"timestamp": 1581944109860
}
]
}
]
}
}
我想要实现的目标是从 $lookup 中获取所有值,并从 text 字段、first_name 字段和 last_name 字段中获取搜索字符串如何做到这一点
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework