【问题标题】:Text Search with exact string in mongoDb在 mongoDb 中使用精确字符串进行文本搜索
【发布时间】:2020-12-09 03:31:10
【问题描述】:

我的 mongo 文档如下所示:

我需要使用 DEC19061936uuuu 进行搜索。 我试过了

db.getCollection('FSL_EventStatusService').find( { $text: { $search: "DEC19061936uuuu" } } )
db.getCollection('FSL_EventStatusService').find( { $text: { $search: "\"DEC19061936uuuu\""  } } )

没有给出结果。每次我获取 0 条记录。 如何使用部分文本搜索进行搜索?这将与 sql 的 like 运算符相同。

{
    "_id" : ObjectId("5f3d802666556e3f88f16f65"),
    "Timestamp" : ISODate("2020-08-19T19:40:20.886Z"),
    "Level" : "Information",
    "MessageTemplate" : "{Type}{FeatureTeamName}{ApplicationName}{MethodName}{EntityType}{EntityValue}{ElapsedMilliseconds}{Exception}{Hostname}{CorrelationId}{Message}{CustomException}{AdditionalInfo}{Timestamp}",
    "RenderedMessage" : "\"input\"\"FSL\"\"EventStatusService\"\"RabbitMQServices.PushToRMQ\"\"ASN\"\"DEC19061936uuuu\"0null\"W1022MQ8Y2\"\"229fe501-8fb4-4b64-b7f2-1ec3a41eda34\"\"[\\\"Pushed message {\\\\\"KeyName\\\\\":\\\\\"ASN\\\\\",\\\\\"KeyValue\\\\\":[\\\\\"DEC19061936uuuu\\\\\"],\\\\\"DataSource\\\\\":\\\\\"OFS\\\\\",\\\\\"Region\\\\\":\\\\\"EMEA\\\\\",\\\\\"EventType\\\\\":\\\\\"2TRELIEF\\\\\",\\\\\"MessageType\\\\\":\\\\\"2TRELIEF\\\\\",\\\\\"EventTimestamp\\\\\":\\\\\"2020-08-19T14:40:08.321-05:00\\\\\",\\\\\"Attributes\\\\\":[{\\\\\"AttributeName\\\\\":\\\\\"REQUEST_NUM\\\\\",\\\\\"AttributeValue\\\\\":null},{\\\\\"AttributeName\\\\\":\\\\\"TraceID\\\\\",\\\\\"AttributeValue\\\\\":\\\\\"229fe501-8fb4-4b64-b7f2-1ec3a41eda34\\\\\"}]} successfully to Q.FSL.ORCHESTRATION.FSL\\\"]\"nullnull08/19/2020 19:36:03",
    "Properties" : {
        "Type" : "input",
        "FeatureTeamName" : "FSL",
        "ApplicationName" : "EventStatusService",
        "MethodName" : "RabbitMQServices.PushToRMQ",
        "EntityType" : "ASN",
        "EntityValue" : "DEC19061936uuuu",
        "ElapsedMilliseconds" : 0,
        "Exception" : null,
        "Hostname" : "W1022MQ8Y2",
        "CorrelationId" : "229fe501-8fb4-4b64-b7f2-1ec3a41eda34",
        "Message" : "[\"Pushed message {\\\"KeyName\\\":\\\"ASN\\\",\\\"KeyValue\\\":[\\\"DEC19061936uuuu\\\"],\\\"DataSource\\\":\\\"OFS\\\",\\\"Region\\\":\\\"EMEA\\\",\\\"EventType\\\":\\\"2TRELIEF\\\",\\\"MessageType\\\":\\\"2TRELIEF\\\",\\\"EventTimestamp\\\":\\\"2020-08-19T14:40:08.321-05:00\\\",\\\"Attributes\\\":[{\\\"AttributeName\\\":\\\"REQUEST_NUM\\\",\\\"AttributeValue\\\":null},{\\\"AttributeName\\\":\\\"TraceID\\\",\\\"AttributeValue\\\":\\\"229fe501-8fb4-4b64-b7f2-1ec3a41eda34\\\"}]} successfully to Q.FSL.ORCHESTRATION.FSL\"]",
        "CustomException" : null,
        "AdditionalInfo" : null,
        "Timestamp" : "2020-08-19T19:36:03.3161314Z"
    },
    "UtcTimestamp" : "2020-08-19 19:40:20Z"
}

【问题讨论】:

  • 如果将正在搜索的字段中的所有标点符号替换为空格会怎样?
  • RenderMessage 字段是否有文本索引。

标签: mongodb mongodb-query robo3t


【解决方案1】:

您需要在您的收藏中创建一个文本索引,以便在进行文本搜索时找到结果。应该类似如下。

db.getCollection('FSL_EventStatusService').createIndex(
{"RenderedMessage":"text",
"Properties.EntityValue":"text"}
)

现在触发您的查询。 More.

但如果您打算对特定字段进行部分搜索,则无需创建文本索引。您可以像往常一样触发查询并使用正则表达式查看结果。 More

db.getCollection('FSL_EventStatusService').find({'RenderedMessage': /DEC19061936/  } )

db.getCollection('FSL_EventStatusService').find({'Properties.EntityValue': {    $regex: 'DEC19061936'   }  } )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2018-12-13
    • 2014-11-19
    • 2017-10-02
    • 1970-01-01
    • 2012-09-05
    相关资源
    最近更新 更多