aggregation - $let and $add: "$add only supports numeric or date types, not Array"

1,989 views
Skip to first unread message

Paulina Budzoń

unread,
Jun 9, 2014, 8:25:02 AM6/9/14
to mongod...@googlegroups.com
Hi all!

I'm trying to aggregate some data and I run into a problem that seems really weird to me.
Consider the following queries:

#1
db.test.aggregate( {
    $project: {
        a: {
            $let: {
                vars: {var1: 1},
                in: {$add: '$$var1'}
            }
        }
    }
})
This returns the documents with value of a as: "a" : 1 as expected.

#2
db.test.aggregate( {
    $project: {
        a: {
            $let: {
                vars: {var1: 1},
                in: {$add: ['$$var1','$$var1']}
            }
        }
    }
})
This returns the documents with value of a as: "a" : 2 as well as expected.

So, logically, the following should also work, just like the above, and return a = 2

db.test.aggregate( {
    $project: {
        a: {
            $let: {
                vars: {var1: [1,1]},
                in: {$add: '$$var1'}
            }
        }
    }
})

But that would be too beautiful and doesn't work, returning an exception $add only supports numeric or date types, not Array

Can anyone explain to me what's the difference between example #2 and #3 - since technically, they should be evaluated to the same code?

Asya Kamsky

unread,
Jun 9, 2014, 10:22:07 PM6/9/14
to mongodb-user
Think of it this way - array as an argument to $add lists the user
entities that should be added - here you are passing it only one
entity and it's not a numeric type.

Maybe that should work anyway, but sadly at the moment, it does not.

Can you describe the problem you are trying to solve with this
technique? Maybe there is another way to get there?

Asya
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> http://www.mongodb.org/about/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...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/872036bf-d828-43b7-a08b-b970c77e07fd%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Paulina Budzoń

unread,
Jun 10, 2014, 5:21:21 AM6/10/14
to mongod...@googlegroups.com
Well, from the examples #1 and #2, $add seems to accept either a single numeric parameter or an array of numeric parameters. In example #3, that's what it should get - $var1 is set to [1,1] in the let statement, so example #2 and #3 should be equivalent :(

I did manage to solve my issue a different way, I was just curious why this does not seem to work.
Reply all
Reply to author
Forward
0 new messages