1

I need to be able to parse deeply nested string value to be able to find an amount greater than in my request query. I fount that $expr operator is supposed to help in that, but when i try to implement it it gives me

"message" : "unknown operator: $expr",

here is my code:

db.OrderAttributes.find({
    InfluencedAttributes: {$elemMatch: {
        LexisAttributes:{$elemMatch: {
            $expr: { $gt: [ {$toDouble: "$MyAttributes.MyField"}, 500]}
        }}
    }}
}

the value I'm trying to compare is here

InfluencedAttributes.0.LexisAttributes.0.MyAttributes.MyField = "20000"

trimmed version of the lookup document:

db.OrderAttributes.insertOne({
    "InfluencedAttributes" : [
        {
            "LexisAttributes" : [
                {
                    "MyAttributes" : {
                        "MyValue" : "20000"
                    }
                },
                {
                    ...
                },
                {
                    ...
                }
            ]
        }
    ]
}})
2
  • Hi, could you add a sample document? Commented Feb 3, 2021 at 21:29
  • @HanielBaez added to the question Commented Feb 3, 2021 at 21:50

1 Answer 1

1

The problem is that $expr is not a valid query for $elemMatch, but you still have two way to achive the desire result:

First option:

db.OrderAttributes.find({"$expr": { "$gt": [ { "$toDouble": "$InfluencedAttributes.0.LexisAttributes.0.MyAttributes.MyField"}, 500 ] } } );

Performance: COLLSCAN for a collection scan.

Second option:

db.OrderAttributes.find({ "InfluencedAttributes.0.LexisAttributes.0.MyAttributes.MyField": {$regex: /^([5-9]\d{2,}|\d{4,})/ } } );

Performance: IXSCAN for scanning index keys.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.