2016-10-15 39 views
1

以下のクエリは、文全体の中の単語に対してうまく機能します。
マルチン検索でmongodbに "not like"を実装する方法

db.candidate.find({ 
$and : [ 
{ permanent_address: { $not: /kendriya/ } } , 
{ current_address: { $not: /India/ } } 
] 
} 

しかし問題は iは以下アレイ

Array 
(
    [$and] => Array 
    (
     [0] => Array 
      (
       [permanent_address] => Array 
        (
         [$not] => /kendriya/ 
        ) 

      ) 
     [1] => Array 
      (
       [current_address] => Array 
        (
         [$not] => /India/ 
        ) 

      ) 
      ) 
      ) 

それは結果を得るためにDBをMongoのためにそれを渡すためにエンコードするときに、JSONは ように変更されるを有することです

$and : 
[ 
{ permanent_address: { $not: "/kendriya/" } } , 
{ current_address: { $not: "/India/" } } 
] 
} 
] 

jsonの文字列を生成すると、その結果が得られない理由がわかります この問題を解決する方法。

答えて

2

mongodbの正規表現変数を作成し、それらの変数をクエリに渡します。

$paddress = new MongoRegex("/kendriya/"); 
$caddress = new MongoRegex("/India/"); 

Array 
(
    [$and] => Array 
    (
     [0] => Array 
      (
       [permanent_address] => Array 
        (
         [$not] => $paddress 
        ) 

      ) 
     [1] => Array 
      (
       [current_address] => Array 
        (
         [$not] => $caddress 
        ) 

      ) 
      ) 
      ) 

それとも、正規表現を作成する

$ paddress =新しいのMongoDB \ BSON \正規表現( '/ kendriya /')これを使用することができます。

ここに述べたようにhttp://php.net/manual/en/class.mongodb-bson-regex.php

+0

こんにちは先生が、$の正規表現と一緒に動作するものではありませんか$? – user3235105

+0

はいmongodb $ regex関数は$ notで動作しません。そのため、正規表現オブジェクトを生成するために言語正規表現機能を使用する必要があります。 MongoRegexは機能しませんか? –

+0

こんにちは...この問題を解決するために使用言語正規表現の例を挙げてください – user3235105

関連する問題