使用 mongoDB 聚合按文本搜索

发布于 2025-01-13 20:47:36 字数 1396 浏览 1 评论 0原文

我正在尝试对 MongoDB Compass 中名为 DAFacility 的集合进行文本搜索:

_id:62170597b3fa8994a0d9a0c8
author:"User"
organizationName:"TSTT"
eventName:"Facility Assessment After Disaster"
eventDate:2022-02-01T00:00:00.000+00:00
area:"Siparia"
disasterNature:"Earthquake"
threatLevel:"High"
surroundingDamage:"Cracked Foundations and Roads"
facilityName:"Point Lisas Main Facility"
facLocation:Array
facStatus:"Operable"
operEqu:23
inoperEqu:7
facilityDamage:"Cracked Walls and Floors"
facImage:Array
__v:0

我正在尝试搜索字段 facilityDamage ,我可以在其中从整个数据中搜索一个单词条目(例如搜索单词 "Walls" 并显示整个条目)

我尝试在 mongoDB 数据聚合选项中执行它,模板为:

[
    {
        '$search': {
            'index': 'string', 
            'text': {
                'query': 'string', 
                'path': 'string'
            }
        }
    }
]

我已阅读该文档,这让我更加困惑至于里面有什么索引、查询和路径。 你们能告诉我索引、查询和路径中包含哪些变量吗?

每当我在节点中使用它时,它都会返回一个空数组:

exports.DAEquipment_damage_search = (req, res) => {
  DAEquipment.aggregate([
    [
      {
        '$match': {
        '$or': [
           {'facilityDamage':{ '$regex':'.*' + req.body.facilityDamage + '.*','$options': 'i' } }
        ]
    }
    }
    }
  ]
  ]).then((DAEquipment) => {
    res.send(DAEquipment);
    console.log(DAEquipment);
  })
  .catch((e) => {
    res.send(e);
  });
};

I am trying to do a text search of the collection called DAFacility in MongoDB Compass:

_id:62170597b3fa8994a0d9a0c8
author:"User"
organizationName:"TSTT"
eventName:"Facility Assessment After Disaster"
eventDate:2022-02-01T00:00:00.000+00:00
area:"Siparia"
disasterNature:"Earthquake"
threatLevel:"High"
surroundingDamage:"Cracked Foundations and Roads"
facilityName:"Point Lisas Main Facility"
facLocation:Array
facStatus:"Operable"
operEqu:23
inoperEqu:7
facilityDamage:"Cracked Walls and Floors"
facImage:Array
__v:0

I am trying to search the field facilityDamage where i can search maybe one word from the entire data entry (eg searching the word "Walls" and having the entire entry shows up)

I am trying to perform it within mongoDB data aggregation option with the template being:

[
    {
        '$search': {
            'index': 'string', 
            'text': {
                'query': 'string', 
                'path': 'string'
            }
        }
    }
]

I have read the document which got me more confused as to what goes in the index, query and path.
can you all advise me as to what variables goes into index, query and path.

Whenever i use it within node it returns an empty array:

exports.DAEquipment_damage_search = (req, res) => {
  DAEquipment.aggregate([
    [
      {
        '$match': {
        '$or': [
           {'facilityDamage':{ '$regex':'.*' + req.body.facilityDamage + '.*','$options': 'i' } }
        ]
    }
    }
    }
  ]
  ]).then((DAEquipment) => {
    res.send(DAEquipment);
    console.log(DAEquipment);
  })
  .catch((e) => {
    res.send(e);
  });
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2025-01-20 20:47:36
await DAFacility.aggregate([
{
    $match: {
        $or: [
           {"facilityDamage":{ $regex:'.*' + searchText + '.*',$options: 'i' } },
        ]
    }
},
])

愿它能帮助你

await DAFacility.aggregate([
{
    $match: {
        $or: [
           {"facilityDamage":{ $regex:'.*' + searchText + '.*',$options: 'i' } },
        ]
    }
},
])

May it will help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文