user3235105
user3235105

Reputation: 73

how we can implement "not like" with $not in mongodb with multiline search

below query works fine to word within whole sentence.

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

)

but problem is that i have an below array

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

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

            )
            )
            )

when i encode it to pass it to mongo db to get result then json will change as

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

generate json string will quoted that why result not getting so how we can solve this problem.

Upvotes: 1

Views: 271

Answers (1)

Puneet Singh
Puneet Singh

Reputation: 3543

Create mongodb regex variables and then pass those variables in query.

$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
                    )

            )
            )
            )

Or you can use this to create regex

$paddress = new MongoDB\BSON\Regex ('/kendriya/');

as mentioned here http://php.net/manual/en/class.mongodb-bson-regex.php

Upvotes: 2

Related Questions