【发布时间】:2020-10-18 08:06:19
【问题描述】:
我正在尝试使用聚合搜索文本,我有以下代码。这是我的聚合查询,但不起作用。我希望首先搜索文本,如果找到包含该词的帖子以查找用户,然后通过 user.name 和主题进行查询。
const posts = await Post.aggregate([
{
$match: {
$text: {
$search: postWords ? `${postWords}` : /.*/,
$caseSensitive: false,
},
},
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "user",
},
},
{
$unwind: "$user",
},
{
$match: {
$and: [
{
topic: {
$regex: postTopic ? postTopic : /.*/,
$options: "i",
$exists: true,
},
},
{
"user.name": {
$regex: postName ? postName : /.*/,
$options: "i",
$exists: true,
},
},
{ text: { $regex: postWords ? postWords : /.*/, $options: "i" } },
],
},
},
{
$skip: req.params.page ? (req.params.page - 1) * 10 : 0,
},
{
$limit: 11,
},
{
$project: {
"user.password": 0,
"user.active": 0,
"user.email": 0,
"user.temporaryToken": 0,
},
},
]);
【问题讨论】:
标签: mongodb mongoose mongodb-query aggregation-framework