Re: field inclusion is not allowed inside of $expression

2,759 views
Skip to first unread message

Jenna

unread,
Aug 23, 2012, 3:09:57 PM8/23/12
to mongodb-user
Hello Tim,

Intentional changes were made in rc1. Could you try making the
following changes to $group?

From:
$group : {
_id: {chr: 1, window: 1},
averagePloidy: {$avg: "$calledPloidy"}
}},

To:

{$group : {
_id: {chr: "$chr", window: "$window"},
averagePloidy: {$avg: "$calledPloidy"}
}},

On Aug 17, 4:54 pm, Tim Chiu <timc...@gmail.com> wrote:
> Hi,
>
> We are using the very cool aggregation framework that comes with 2.2, and we noticed that something broke between rc0 and rc1.  In particular, the following command:
>
> db.Collection.aggregate(
>         {$match : {calledPloidy : {$not : {$in : ["N"]}}, sampleName: "XYZ"}},
>     { $project : {
>         chr : 1,
>         begin : 1,
>         end : 1,
>         calledPloidy: 1,
>         window: {$multiply: [{$divide: [{$subtract: ["$begin", {$mod : ["$begin", 100000]}]}, 100000]}, 100000]}
>     }},
>     {$group : {
>                 _id: {chr: 1, window: 1},
>                 averagePloidy: {$avg: "$calledPloidy"}
>         }},
>     { $project : {
>         _id: 0,
>         chr: "$_id.chr",
>         begin: "$_id.window",
>         end: {$add: ["$_id.window", 100000] },
>         calledPloidy: "$averagePloidy",
>         step: {$add: [0, 100000]}
>     }}
>  );
>
> is now returning the following error message:
>
> {
>         "errmsg" : "exception: field inclusion is not allowed inside of $expressions",
>         "code" : 16420,
>         "ok" : 0
>
> }
>
> What's changed and what must I change in the json to make it work with rc1?
>
> Thanks!
> Tim

Kamlesh

unread,
Oct 17, 2012, 3:33:33 PM10/17/12
to mongod...@googlegroups.com
I'm experiencing similar error, with different syntax, my query is:
  1. db.l_01.aggregate(

{$project:{

rid:1,

Site:{$cond:[{'events.et':300},1,0]},

Call:{$cond:[{'events.et':200},1,0]}}},

{$limit:10})


Jenna deBoisblanc

unread,
Oct 19, 2012, 1:16:35 PM10/19/12
to mongod...@googlegroups.com
Hi Kamlesh,

Are you trying to check if events.et is equal to a certain value? If so, the following syntax should work:

{ "_id" : 1, "events" : { "et" : 200 } }
{ "_id" : 2, "events" : { "et" : 300 } }

> db.collection.aggregate( {$project:{ Call:{$cond:[{$eq: ["$events.et", 200]}, 1, 0]}}}, {$limit:10})
{
"result" : [
{
"_id" : 1,
"Call" : 1
},
{
"_id" : 2,
"Call" : 0
}
],
"ok" : 1
}

Kamlesh Gokal

unread,
Oct 19, 2012, 2:29:48 PM10/19/12
to mongod...@googlegroups.com
Yup, i that works perfectly Thanks!.

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Gianfranco

unread,
Oct 30, 2012, 7:01:43 AM10/30/12
to mongod...@googlegroups.com
Hi Jamie,

I couldn't find any errors under:

Perhaps they were corrected by the time we could get back to you.
If not, please let us know by clicking the 'Feedback' button on the top-right hand side.

On Monday, October 22, 2012 11:58:28 PM UTC+1, Jamie Carl wrote:
This just fixed my problem.  I was learning Aggregation from the MongoDB online docs and was banging my head against the wall while this wasn't working.

Might want to get someone to update the online docs because they are now wrong. ;)

Alex Ayerdi

unread,
Jul 1, 2013, 6:20:35 PM7/1/13
to mongod...@googlegroups.com
I have 6 different aggregate functions.  I expanded the first one just for better visual.  I am getting the same error as the original poster.

Should I be changing group's _id parts "cmts" : 1  to be    "cmts" : "$cmts"  ??

db.modems.aggregate(
         {$match: {"bootfile": /ims/, "cmts": "<someserver>"}}, 
     {$project: {
            "_id": 0, 
            "is_ims": {$cond: [{"e": {$substr: ['$bootfile', 0, 1]}}, 0, 1]}, 
            "cmts": 1}}, 
     {$group:  {
            "_id": {"cmts": 1, "is_ims": 1}, 
            "count": {$sum: 1}}});

db.modems.aggregate({$match: {"bootfile": /ims/, "cmts": "<someserver>"}}, {$project: {"_id": 0, "is_ims": {$cond: [{{$substr: ['$bootfile', 0, 1]}: {$ne: 'd'}}, 0, 1]}, "cmts": 1}}, {$group: {"_id": {"cmts": 1, "is_ims": 1}, "count": {$sum: 1}}});
db.modems.aggregate({$match: {"bootfile": /ims/, "cmts": "<someserver>"}}, {$project: {"_id": 0, "is_ims": {$cond: [{$cmp: [{$substr: ['$bootfile', 0, 1]}, 'e']}, 0, 1]}, "cmts": 1}}, {$group: {"_id": {"cmts": 1, "is_ims": 1}, "count": {$sum: 1}}});
db.modems.aggregate({$match: {"bootfile": /ims/, "cmts": "<someserver>"}}, {$project: {"_id": 0, "is_ims": {$cond: [{0: {$gt: {$cmp: [{$substr: ['$bootfile', 0, 1]}, 'e']}}, 0, 1]}, "cmts": 1}}, {$group: {"_id": {"cmts": 1, "is_ims": 1}, "count": {$sum: 1}}}); }
db.modems.aggregate({$match: {"bootfile": /ims/, "cmts": "<someserver>"}}, {$project: {"_id": 0, "is_ims": {$cond: [{0: {$gt: {$cmp: [{$substr: ['$bootfile', 0, 1]}, 'e']}}}, 0, 1]}, "cmts": 1}}, {$group: {"_id": {"cmts": 1, "is_ims": 1}, "count": {$sum: 1}}}); }
db.modems.aggregate({$match: {"bootfile": /ims/, "cmts": "<someserver>"}}, {$project: {"_id": 0, "is_ims": {$cond: [{4: {$gt: {$cmp: [{$substr: ['$bootfile', 0, 1]}, 'e']}}}, 0, 1]}, "cmts": 1}}, {$group: {"_id": {"cmts": 1, "is_ims": 1}, "count": {$sum: 1}}}); }

Asya Kamsky

unread,
Jul 2, 2013, 12:24:20 AM7/2/13
to mongod...@googlegroups.com
It looks like you are not using $cond expression correctly (or rather its first argument).  You are trying to write <something> $ne <something> and $cond expects three arguments the first one of which evaluates to a boolean.  So maybe {$ne:[ <something>, <somethingelse>]} as described here: http://docs.mongodb.org/manual/reference/aggregation/ne/

Asya

Moshe Shperling

unread,
Jan 13, 2014, 4:37:14 AM1/13/14
to mongod...@googlegroups.com, tim...@gmail.com
hi 
even though it is an old post but i am still having a described issue. This is my group block

{ $group: { _id: {sku:"$SKU",period:30},unitprice:{$sum:"$UnitPrice"},count: { $sum: 1 } } }

the point is that i need to make 3 aggregation with the same skus and therefore sku will not be unique so i want to make it unique by adding another field to it. but then i got this error:
"errmsg" : "exception: field inclusion is not allowed inside of $expressions"

Ger Hartnett

unread,
Jan 13, 2014, 5:14:41 AM1/13/14
to mongod...@googlegroups.com, tim...@gmail.com
Right now you can use a no-op expression to compute what you want.
$group: { _id: {sku:"$SKU", period: {$add:30}}, unitprice : {$sum:"$UnitPrice"}, count: {$sum:1} }

In the next major release there will be a $literal operator which should do what you want.

Hope this helps,
/ger.

Moshe Shperling

unread,
Jan 13, 2014, 5:34:56 AM1/13/14
to mongod...@googlegroups.com, tim...@gmail.com
thanks. it worked for me. 

Ger Hartnett

unread,
Jan 13, 2014, 7:01:12 AM1/13/14
to mongod...@googlegroups.com, tim...@gmail.com
Good news, thanks for letting me know Moshe.
Reply all
Reply to author
Forward
0 new messages