【问题标题】:MongoDB Full Text Search in an Array of objects对象数组中的 MongoDB 全文搜索
【发布时间】:2022-01-04 17:27:32
【问题描述】:

我有一个这样的架构:

const AyoSchema = new Schema({
images: Array, 

Images 是一个数组,其中对象以这种格式存储:

{
id: a uuid here, 
name: a string here,
url: a url here, 
topic: a string here 
}

我想做的是, 我想在图像数组中搜索所有对象的名称属性而不涉及大量索引工作,

我该怎么办?

【问题讨论】:

    标签: arrays mongodb mongoose full-text-search


    【解决方案1】:

    有几种方法可以处理它。如果你只想返回匹配的文档,那就有点复杂了。

    我假设您只想返回匹配的项目。为此,您需要使用聚合管道,特别是 $unwind 和 $match 运算符。

    Check out a live demo here

    考虑以下几点:

    数据库

    [
      {
        _id: ObjectId("111111111111111111111111"),
        images: [
          {
            id: ObjectId("123123224454323123121314"),
            name: "foo",
            url: "cdn.domain.com/images/foo",
            topic: "lorem ipsum"
          },
          {
            id: ObjectId("222123224454323123121314"),
            name: "bar",
            url: "cdn.domain.com/images/bar",
            topic: "lorem ipsum"
          },
          {
            id: ObjectId("333323224454323123121314"),
            name: "baz",
            url: "cdn.domain.com/images/baz",
            topic: "lorem ipsum"
          }
        ]
      },
      {
        _id: ObjectId("222222222222222222222222"),
        images: [
          {
            id: ObjectId("888823224454323123121314"),
            name: "text",
            url: "cdn.domain.com/images/text",
            topic: "lorem ipsum"
          },
          {
            id: ObjectId("999993224454323123121314"),
            name: "foo",
            url: "cdn.domain.com/images/pic",
            topic: "lorem ipsum"
          }
        ]
      }
    ]
    

    查询

    db.collection.aggregate([
      {
        $unwind: "$images"
      },
      {
        $match: {
          "images.name": "foo" // <-- replace "foo" with your query
        }
      }
    ])
    

    结果

    [
      {
        "_id": ObjectId("111111111111111111111111"),
        "images": {
          "id": ObjectId("123123224454323123121314"),
          "name": "foo",
          "topic": "lorem ipsum",
          "url": "cdn.domain.com/images/foo"
        }
      },
      {
        "_id": ObjectId("222222222222222222222222"),
        "images": {
          "id": ObjectId("999993224454323123121314"),
          "name": "foo",
          "topic": "lorem ipsum",
          "url": "cdn.domain.com/images/pic"
        }
      }
    ]
    

    更新

    包括正则表达式。

    Live demo

    查询

    db.collection.aggregate([
      {
        $unwind: "$images"
      },
      {
        $match: {
          "images.name": {
            "$regex": "fo*"
          }
        }
      }
    ])
    

    【讨论】:

    • 还有,我如何对正则表达式做同样的事情?
    • 为什么需要使用正则表达式?此查询基于匹配的全文。
    • 我需要正则表达式,因为 MongoDB 全文搜索不支持自动完成。阅读第二步:stackoverflow.com/questions/29892947/…
    • 请查看我的答案的更新。
    猜你喜欢
    • 2013-07-23
    • 2015-06-06
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    相关资源
    最近更新 更多