Re: MongoDB in throwing 'object is already an operator expression' Error

From: Wan Bachtiar <wan.bachtiar@xxxxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Mon, 9 May 2016 21:29:50 -0700 (PDT)
Why ads?


MongoResultException: localhost:27017: this object is already an operator 
expression, and can’t be used as a document expression (at ‘0’)

Hi, 

The error message is due to a missing extra array wrap before $subtract 
<https://docs.mongodb.com/manual/reference/operator/aggregation/subtract/>. 
This is because $subtract itself is document. For example via the mongo 
shell <https://docs.mongodb.com/manual/mongo/>:

"$gte":[ { '$subtract':[20,10] }, 10]},

Or, as your example PHP code, it should be :

$query = array(
           array('$redact' =>
             array('$cond' => 
               array("if" => 
                 array('$gte' => 
                   array(
                     array('$subtract' => 
                       array(20, 10)),
                       10
                     )
                   ),
                 "then" => '$$KEEP',
                 "else" => '$$PRUNE'
               )
             )
           )
         );
$results = $collection->aggregate($query);

Best regards, 

Wan. 


-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/983416ca-ef2c-49db-9abc-328665b75ce4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?