MongoDB 的聚合 $search 未提取任何结果

MongoDB's aggregate $search is not pulling any results

所以我正在开发一个 express/mongoose/mongoDB 应用程序,它要求用户查询数据库以根据 3 个字段查找其他用户,但出于某种原因,none 我制作的文档用于测试结果,目的被取消了。即使我使查询参数与正在搜索的字段完全匹配。我正在搜索的字段是:

我为我的用户准备了以下型号(当然是经过修整的)。

const BlockedUser = require("./blockeduser");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema({
  email: {
    type: String,
    required: [true, "Please provide a valid email"],
    unique: true,
    trim: true,
    lowercase: true
  },
  password: {
    type: String,
    minlength: 10,
    trim: true,
    select: false
  },
  username: {
    type: String,
    required: [true, "Please provide a username"],
    unique: true,
    minlength: 3,
    maxlength: 255
  },
  firstName: {
    type: String,
    required: [true, "Please provide a first name"],
    minlength: 3,
    maxlength: 255
  },
  lastName: {
    type: String,
    required: [true, "Please provide a first name"],
    minlength: 3,
    maxlength: 255
  },
  blockList: {// External collection of a list of blocked users
    type: Schema.Types.ObjectId,
    ref: 'BlockedUser'
  }
});

const User = mongoose.model("User", UserSchema);

module.exports = User;

然后在 Express 应用中使用猫鼬进行以下查询。

const mongoose = require("mongoose");
const User = require("../models/user");

router.get("/profiles", async (req, res, next) => {
  const users = await User.aggregate([
    {
      $search: {
        "text": {
          "query": req.query.q,
          "path": ["username", "firstName", "lastName"],
          "fuzzy": {}
        }
      }
    },
    {
      $match: {
        _id: {$ne: mongoose.Types.ObjectId(req.user.id)} // Makes sure the user is not pulling themselves as a result
      }
    },
    { // Populates blockList
      $lookup: {
        from: "blockedusers",
        let: {
          "theId": "$blockList"
        },
        pipeline: [
          {
            $match: {
              $expr: {
                $eq: ["$_id", "$$theId"]
              }
            }
          }
        ],
        as: "blockList"
      }
    },
    {
      $match: {
        "blockList.blockedUsers": {
          $ne: mongoose.Types.ObjectId(req.user.id) // Makes sure the user is not on the queried blocked user list
        }
      }
    },
    {
      $project: {
        _id: 0,
        email: 0
      }
    }
  ]);

  res.status(200).json({
    status: "success",
    data: users
  });
});

最后是我发送的查询 URL。

http://127.0.0.1:1337/profiles?q=willow

为了完整起见,这是我要提取的文档。

{
  username: "Willow",
  firstName: "Jennifer",
  lastName: "Jones",
  email: "email@example.com",
  password: "examplepass1234" // This is hashed don't worry :P
}

我是否遗漏了阻止我的应用程序检索结果的内容?我也知道 $text 搜索也可用,但我目前正在免费 MongoDB 层上进行测试和构建,每次我尝试使用 $text 搜索时,我都会收到错误提示“$text 不适用于此”地图集层'。很想弄清楚这一点,因为这是拥有功能原型的最后步骤之一哈哈!

感谢任何有见识的人。这对我来说意义重大!

所以不久前这里发布了一个答案,我相信用户删除了它,但他们提到了一些让我思考的事情(我认为他们的名字是 Eol,所以谢谢 Eol!!!)。他们提到设置 'index',然后谈到在查找查询中使用 $text。从那时起,我进入了我的 mongoose 模型并将我查询的 3 个字段的索引设置为 index: "text",通过注册一个新的测试用户创建了一个新文档并且 BOOM 它成功了!我只需要在 3 个字段上设置索引,仅此而已。除此之外,我确实必须 'build' 我在 mongoDB atlas 仪表板中的搜索索引。

因此,如果您遇到同样的问题,请记住在您尝试搜索的字段上设置索引。就我而言,我需要它看起来像:

const BlockedUser = require("./blockeduser");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema({
  email: {
    type: String,
    required: [true, "Please provide a valid email"],
    unique: true,
    trim: true,
    lowercase: true
  },
  password: {
    type: String,
    minlength: 10,
    trim: true,
    select: false
  },
  username: {
    type: String,
    required: [true, "Please provide a username"],
    unique: true,
    minlength: 3,
    maxlength: 255,
    index: "text" // <----------- MAKE SURE to add this to allow you to search this field!
  },
  firstName: {
    type: String,
    required: [true, "Please provide a first name"],
    minlength: 3,
    maxlength: 255,
    index: "text" // <----------- MAKE SURE to add this to allow you to search this field!
  },
  lastName: {
    type: String,
    required: [true, "Please provide a first name"],
    minlength: 3,
    maxlength: 255,
    index: "text" // <----------- MAKE SURE to add this to allow you to search this field!
  },
  blockList: {// External collection of a list of blocked users
    type: Schema.Types.ObjectId,
    ref: 'BlockedUser'
  }
});

该查询被单独放置并且运行良好。

鉴于我对 MongoDB 了解不多(还),我猜你需要使用索引字段,在某种程度上,'categorize' 你将要使用的字段正在查询一些 MongoDB 类别。再一次,这是我能解释我所知道的一点点的最好方式,但希望这对将来的人有所帮助!