【问题标题】:Mongo text search with AND operation for multiple words partially enered对部分输入的多个单词进行 AND 操作的 Mongodb 文本搜索
【发布时间】:2018-01-29 10:33:18
【问题描述】:
{
    TypeList" : [ 
        {
            "TypeName" : "Carrier"
        },
        {
            "TypeName" : "Not a Channel Member"
        },
        {
            "TypeName" : "Service Provider"
        }
    ]
}

问题:

db.supplies.find("text", {search:"\"chann\" \"mem\""})

对于上述查询,我​​想显示:

{
    TypeName" : "Not a Channel Member"
}

但我无法得到我的结果。

我必须在 query 中做哪些更改。 请帮帮我。

【问题讨论】:

    标签: string mongodb search text


    【解决方案1】:

    以下查询将返回您想要的结果。

    db.supplies.aggregate([
       {$unwind:"$TypeList"},
       {$match:{"TypeList.TypeName":{$regex:/.*chann.*mem.*/,$options:"i"}}},
       {$project:{_id:0, TypeName:"$TypeList.TypeName"}}
    ])
    

    【讨论】:

    • 如果我的集合包含多个对象、列表和字段,那么查询将是什么?...请帮助我。
    【解决方案2】:

    如果你能接受得到这样的输出:

    {
        "TypeList" : [ 
            {
                "TypeName" : "Not a Channel Member"
            }
        ]
    }
    

    然后您可以使用聚合框架来解决问题,该框架通常通过运行以下查询来提高性能:

    db.supplies.find(
    {
        "TypeList.TypeName": /chann.*mem/i
    },
    { // project the list in the following way
        "_id": 0, // do not include the "_id" field in the output
        "TypeList": { // only include the items from the TypeList array...
            $elemMatch: { //... where
                "TypeName": /chann.*mem/i // the "TypeName" field matches the regular expression
            }
       }
    })
    

    另见此链接:Retrieve only the queried element in an object array in MongoDB collection

    【讨论】:

    • 对象列表很好...如果我的集合包含多个对象、列表和字段,那么查询将是什么?...请帮助我。
    • 请更具体一些,并给我们一些示例数据,所需的输入和输出。
    • { "supplyId" : "26", "OrderId" : "46573", "status" : "Open", "statusId" : 201, "dateInitiated" : ISODate("2017-08- 18T05:04:03.271Z")、“orderTitle”:“订单标题”、“storeList”:[{“storeId”:402、“storeName”:“Banglore”}]、“channelMemberTypeList”:[{“channelMemberTypeId”: 502,"channelMemberTypeName" : "分销商" }] "createDate" : ISODate("2017-08-18T05:05:50.647Z"), }
    • { "supplyId" : "26", "OrderId" : "46573", "status" : "Open", "statusId" : 201, "date" : ISODate("2017-08- 18T05:04:03.271Z"),“orderTitle”:“订单标题”,“storeList”:[{“storeId”:402,“storeName”:“Banglore”}],“typeList”:[{“typeId”: 502,"typeName" : "Distributor" }] }..我的收藏是这样的...搜索值可以是supplyid或者status或者date或者storeName或者typeName或者可以组合也可以输入(26 Distrib---partial word) ...然后我想要显示包含“26”和“Distributor”值的文档。所有字段都是可搜索字段。
    • 请更新您的问题以反映所有这些,并具体说明您希望获得什么作为输出。给我们一些具体的例子——你的例子越好,我们的答案就越好。
    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 2020-07-15
    • 2022-12-22
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多