문제

I have pipeline many level in, Now I would like to compare two fields like follow:

db.collection.aggregate({ ...},
{$match:{firstfield:{$gte:"$secondfield"}}})

How to write this query?

도움이 되었습니까?

해결책

Close but the actual invocation is slightly different:

db.collection.aggregate([
    { "$project": {
        "firstfield": 1,
        "secondfield": 1,
        "difference": { "$gte": [ "$firstfield", "$secondfield" ] }
    }},
    { "$match": { "difference": true } }
])

So a bit different to the $match invocation, the $project ( or possibly $group ) form allow for a true|false value to be returned on the field comparison.

This allows you to filter with the $match phase later in the pipeline in order to "include|exclude" results based on the logical condition that was projected.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top