[SERVER-7491] Can't use subfields of composite _id in $group _id Created: 27/Oct/12  Updated: 11/Jul/16  Resolved: 05/Nov/12

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 2.2.0
Fix Version/s: 2.2.2, 2.3.1

Type: Bug Priority: Major - P3
Reporter: Maciej Gajewski Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 12.04, installed from 10gen ppa


Issue Links:
Duplicate
is duplicated by SERVER-7805 Unable to access _id subdocuments in ... Closed
Operating System: ALL
Participants:

 Description   

This works as expected:

> db.b.insert({k:{a:2,b:1},v:567})
> db.b.insert({k:{a:1,b:1},v:111})
> db.b.insert({k:{a:1,b:2},v:12})
> db.b.insert({k:{a:2,b:2},v:22})
> db.b.aggregate({$group:{_id:'$k.a', s:{$sum:'$v'}}})
{
        "result" : [
                {
                        "_id" : 1,
                        "s" : 123
                },
                {
                        "_id" : 2,
                        "s" : 589
                }
        ],
        "ok" : 1
}

But when using _id instead of k, it doesn't:

> db.a.insert({_id:{a:1,b:1},v:123})
> db.a.insert({_id:{a:1,b:2},v:345})
> db.a.insert({_id:{a:2,b:2},v:222})
> db.a.insert({_id:{a:2,b:1},v:567})
> db.a.aggregate({$group:{_id:'$_id.a', s:{$sum:'$v'}}})
{ "result" : [ { "_id" : null, "s" : 1257 } ], "ok" : 1 }



 Comments   
Comment by auto [ 06/Nov/12 ]

Author:

{u'date': u'2012-10-29T18:33:34Z', u'email': u'mathias@10gen.com', u'name': u'Mathias Stearn'}

Message: SERVER-7491: Correctly generate projections for subfields of _id

This requires special casing due to SERVER-7502
Branch: v2.2
https://github.com/mongodb/mongo/commit/910a15e0698cac4a078c608d6f5c03f44e5f9e18

Comment by auto [ 05/Nov/12 ]

Author:

{u'date': u'2012-10-29T18:33:34Z', u'email': u'mathias@10gen.com', u'name': u'Mathias Stearn'}

Message: SERVER-7491: Correctly generate projections for subfields of _id

This requires special casing due to SERVER-7502
Branch: master
https://github.com/mongodb/mongo/commit/509199b32dc34f302e1d188a9b35bf7e3a4bd017

Comment by Mathias Stearn [ 29/Oct/12 ]

I see the issue. You can work around it by explicitly including _id in a $project before the $group or using the full _id in the $group.

> db.a.aggregate({$group:{_id:'$_id.a', s:{$sum:'$v'}}})
{ "result" : [ { "_id" : null, "s" : 1257 } ], "ok" : 1 }
> db.a.aggregate({$project:{_id:1, v:1}}, {$group:{_id:'$_id.a', s:{$sum:'$v'}}})
{
	"result" : [
		{
			"_id" : 1,
			"s" : 468
		},
		{
			"_id" : 2,
			"s" : 789
		},
		{
			"_id" : null,
			"s" : 0
		}
	],
	"ok" : 1
}

The issue is that we don't fetch the _id from the database is it isn't being used. I think the logic ignored the subfield of _id case.

BTW, unrelated to this issue, but it is often a bad idea to use a multi-field key as the _id. Not all drivers maintain order of fields and

{a:1, b:1}

!=

{b:1, a:1}

in mongodb so you have to be very careful that you don't end up with duplicate documents. Note that single key objects don't have this issue, so {_id: {color:'red'}} is ok.

Generated at Thu Feb 08 03:14:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.