Mongo文本搜索使用AND操作多个单词部分执行

时间:2017-08-21 11:10:24

标签: string mongodb search text

        <?xml version='1.0' encoding='utf-8'?>
    <widget id="io.ionic.starter" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
        <feature name="Camera">
            <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
        </feature>
        <feature name="Device">
            <param name="android-package" value="org.apache.cordova.device.Device" />
        </feature>
        <feature name="SplashScreen">
            <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
            <param name="onload" value="true" />
        </feature>
        <feature name="StatusBar">
            <param name="android-package" value="org.apache.cordova.statusbar.StatusBar" />
            <param name="onload" value="true" />
        </feature>
        <feature name="Whitelist">
            <param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
            <param name="onload" value="true" />
        </feature>
        <feature name="Keyboard">
            <param name="android-package" value="io.ionic.keyboard.IonicKeyboard" />
            <param name="onload" value="true" />
        </feature>
        <name>MyApp</name>
        <description>An awesome Ionic/Cordova app.</description>
        <author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
        <content src="index.html" />
        <access origin="*" />
        <allow-navigation href="http://ionic.local/*" />
        <allow-intent href="http://*/*" />
        <allow-intent href="https://*/*" />
        <allow-intent href="tel:*" />
        <allow-intent href="sms:*" />
        <allow-intent href="mailto:*" />
        <allow-intent href="geo:*" />
        <allow-intent href="market:*" />
        <icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" />
        <icon density="mdpi" src="resources/android/icon/drawable-mdpi-icon.png" />
        <icon density="hdpi" src="resources/android/icon/drawable-hdpi-icon.png" />
        <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi-icon.png" />
        <icon density="xxhdpi" src="resources/android/icon/drawable-xxhdpi-icon.png" />
        <icon density="xxxhdpi" src="resources/android/icon/drawable-xxxhdpi-icon.png" />
        <splash density="land-ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" />
        <splash density="land-mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" />
        <splash density="land-hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" />
        <splash density="land-xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" />
        <splash density="land-xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" />
        <splash density="land-xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" />
        <splash density="port-ldpi" src="resources/android/splash/drawable-port-ldpi-screen.png" />
        <splash density="port-mdpi" src="resources/android/splash/drawable-port-mdpi-screen.png" />
        <splash density="port-hdpi" src="resources/android/splash/drawable-port-hdpi-screen.png" />
        <splash density="port-xhdpi" src="resources/android/splash/drawable-port-xhdpi-screen.png" />
        <splash density="port-xxhdpi" src="resources/android/splash/drawable-port-xxhdpi-screen.png" />
        <splash density="port-xxxhdpi" src="resources/android/splash/drawable-port-xxxhdpi-screen.png" />
        <preference name="loglevel" value="DEBUG" />
        <preference name="webviewbounce" value="false" />
        <preference name="UIWebViewBounce" value="false" />
        <preference name="DisallowOverscroll" value="true" />
        <preference name="android-minSdkVersion" value="16" />
        <preference name="BackupWebStorage" value="none" />
        <preference name="SplashMaintainAspectRatio" value="true" />
        <preference name="FadeSplashScreenDuration" value="300" />
        <preference name="SplashShowOnlyFirstTime" value="false" />
        <preference name="SplashScreen" value="screen" />
        <preference name="AutoHideSplashScreen" value="true" />
        <preference name="SplashScreenDelay" value="3000" />
    </widget>

问题:

{
    TypeList" : [ 
        {
            "TypeName" : "Carrier"
        },
        {
            "TypeName" : "Not a Channel Member"
        },
        {
            "TypeName" : "Service Provider"
        }
    ]
}

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

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

但我无法得到我的结果。

我在查询中需要做些哪些更改。 请帮帮我。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

{
    "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