按参考文档字段搜索词搜索MongoDB Atlas

仓敷

我知道如何使用MongoDB Atlas搜索来搜索按其名称和给定买家ID以及已知/matched供应商名称的订单,例如:

(使用猫鼬ODM)

    const searchResults = await Order.aggregate([
    {
      $search: {
        text: {
          query: '{search-term}',
          path: 'name',
          fuzzy: {
            maxEdits: 2,
            maxExpansions: 100,
          },
        },
      },
    },
    { $unwind: '$supplier' },
    {
      $lookup: {
        from: 'suppliers',
        localField: 'supplier',
        foreignField: '_id',
        as: 'suppliers',
      },
    },
    {
      $match: {
        buyer: mongoose.Types.ObjectId('5e19a594c86e72017debf9dc'),
        // The search term entered by a user:
        'suppliers.name': 'supplierName',
      },
    },
  ]);

但是,我希望能够利用MongoDB Atlas搜索来:

  • 搜索所有订单,
  • 给予Buyer ID
  • 搜索字词-可能不是完全匹配-是Supplier name
  • 买方和供应商通过参考ID嵌套在订单上:

具有Order架构:

const orderSchema = new mongoose.Schema({
  name: {
    type: String,
    minlength: 2,
    maxlength: 255,
  },
  buyer: { type: ObjectId, ref: 'Buyer' },
  supplier: { type: ObjectId, ref: 'Supplier' },
});

Buyer 模式:

const buyerSchema = new mongoose.Schema({
  name: {
    type: String,
  },
  ...
});

Supplier 模式:

const supplierSchema = new mongoose.Schema({
  name: {
    type: String,
  },
  ...
});
道格

这应该工作使用的化合物文本平等$search

const order = await Order.aggregate([
{ $search: {
    compound: {
        must: [ 
            { text: { query: "SEARCH TERM", path: "name"}}, 
            { equals: { path: "buyer", value: ObjectId("5e19a594c86e72017debf9dc") }} 
        ]
    }
}}])

如果您不希望等分查询影响得分,也可以将其移至filter子句。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在MongoDB Atlas全文搜索中查询词的子集

来自分类Dev

MongoDB与MongoDB Atlas

来自分类Dev

如何设置MongoDB Atlas?

来自分类Dev

MongoDB Atlas项目/集群

来自分类Dev

机器上的 MongoDB 或 MongoDB Atlas

来自分类Dev

MongoDb Atlas全文搜索索引重建,而无需停机

来自分类Dev

将Azure认知搜索连接到MongoDB Cloud Atlas

来自分类Dev

MongoDB Atlas Alert的调试流程

来自分类Dev

mongodb atlas 与 ssl 的连接问题

来自分类Dev

Pymongo KeyError: '$err' MongoDB Atlas

来自分类Dev

在 Vertx 中使用 MongoDB Atlas

来自分类Dev

在 MongoDB Atlas 中添加 userAdmin

来自分类Dev

如何使用复合MongoDB Atlas搜索查询来搜索整数数组?

来自分类Dev

Atlas不会按顺序渲染图像[libGDX]

来自分类Dev

MongoDB Atlas Profiler:什么是“数字收益”?

来自分类Dev

Django无法连接到MongoDB Atlas

来自分类Dev

无法连接到MongoDB Atlas群集:DNSHostNotFound

来自分类Dev

MongoDb Atlas未连接到Heroku

来自分类Dev

Mongoose与MongoDB Atlas返回空数组

来自分类Dev

从 MongoDB Atlas 的集合中获取 NULL 数据

来自分类Dev

更新 mongodb atlas 数据库架构

来自分类Dev

MongoDB Atlas和适用于AWS的MongoDB Atlas之间的区别是什么

来自分类Dev

ElasticSearch:使用匹配的搜索词标记文档

来自分类Dev

按搜索词对多维数组进行排序

来自分类Dev

按分隔的字母搜索词过滤列表

来自分类Dev

使用MongoDB针脚是否还会收取MongoDB Atlas的费用?

来自分类Dev

在Atlas Search的不同字段之间找到完整的术语

来自分类Dev

MongoDB Compass-卡在连接到Atlas上

来自分类Dev

Mongodb + Atlas:“验证失败,身份验证失败。”,代码:8000,