Kyte
Kyte

Reputation: 834

MongoDB equivelant to SQL "NOT LIKE" query

I have the following SQL statement

SELECT Currency, TransTime, AuthTime FROM MONIES WHERE Currency not like 'USD%';

If the SQL statement were simply a "LIKE" query, it would yield the following MongoDB statement

db.MONIES.find({
        "Currency": "USD%"
    }, {
        "Currency": 1,
        "TransTime": 1,
        "AuthTime": 1
});

Is this the proper query for a "NOT LIKE" equivalent? I think this is correct per the documentation for the 'not' operator, but I'd like to double check with Stackoverflow

db.MONIES.find({
        "Currency": { $not: { $like: 'USD%' } }
    }, {
        "Currency": 1,
        "TransTime": 1,
        "AuthTime": 1
});

Upvotes: 0

Views: 2364

Answers (1)

Asya Kamsky
Asya Kamsky

Reputation: 42352

The page you link actually has an example of what you want to do:

db.inventory.find( { item: { $not: /^p.*/ } } )

There is no $like operator, but MongoDB supports regular expressions.

Upvotes: 2

Related Questions