JJHunter
JJHunter

Reputation: 278

Advanced logical query in Mongo DB

I have a logical expression:

( Ts == <lastTs> && Id > <lastId> ) || Ts > <_lastTs>

Where Ts and Id are the collection's fields and and are variables.

I am using Yii Mongodb suite so I cannot use "$or" operator as it is not implemented.

So I changed that statement into:

!(!( Ts == <lastTs> && Id > <lastId> ) && Ts <= <lastTs> )

Is it possible to run something like this in Mongo?

I tried to construct something but I noticed when I do for example:

"$not": {
  "created_at": {"$lte": ISODate("...")},
  "_id": {"$gte": MongoID(...)}
}

It does not return any result. But when I remove "$not" it returns some data. I am not sure if it's allowed to used $not on field.

Is there a way to run such query in Mongo?

Upvotes: 1

Views: 142

Answers (1)

rodrigovr
rodrigovr

Reputation: 454

You can set a complex criteria using EMongoCriteria::setConditions(). It accepts any kind of array as parameters and EMongoDocument::find() will pass it unchanged to PHP's Mongo extension.

Try this:

$c = new EMongoCriteria;
$c->setConditions([ '$or' => [ 
                    'created_at' => ['$gt' => ... ],
                    '$and' => [
                        '_id' => ['$gt' => ... ],
                        'created_at' => ['$eq' => ...]
                     ]
                   ] 
                ]);
$documents = MyCollection::model()->find($c);

Upvotes: 1

Related Questions