[SERVER-9030] Aggregation using $match and $project Created: 20/Mar/13  Updated: 10/Dec/14  Resolved: 20/Mar/13

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 2.2.3
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Kurt Agius Assignee: Michael Grundy
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

I have the following following collection:

> db.ysTest.aggregate([{$project:{_id:1,unitStatus:1}}]);
{
	"result" : [
		{
			"_id" : ObjectId("514309f3e18aa7d14100217a"),
			"unitStatus" : "es_pws"
		},
		{
			"_id" : ObjectId("514309f3e18aa7d141002816"),
			"unitStatus" : "es_run"
		},
		{
			"_id" : ObjectId("514309f0e18aa7d14100021e")
		}
	],
	"ok" : 1
}

I try to run an aggregate to match a particular unitStatus, and project every record basically with just the _id and unitStatus (not other fields).
note: I'm using aggregate because this is part of a more complicated aggregate... (i know this can be done with a find)

I expected to have only 1 document as a result but i get everything. Am i doing something wrong ? or is this a bug?

> db.ysTest.aggregate([{
... $match: {
...   unitStatus: {$exists: true, $nin: ["es_pws", "es_stl"]}
... },
... $project: {_id: 1,unitStatus:1},
... }]);
{
	"result" : [
		{
			"_id" : ObjectId("514309f3e18aa7d14100217a"),
			"unitStatus" : "es_pws"
		},
		{
			"_id" : ObjectId("514309f3e18aa7d141002816"),
			"unitStatus" : "es_run"
		},
		{
			"_id" : ObjectId("514309f0e18aa7d14100021e")
		}
	],
	"ok" : 1
}



 Comments   
Comment by Kurt Agius [ 20/Mar/13 ]

Hi Michael,

Thanks, apparently the examples i saw here you put the pipline in an Array were for the nodejs driver.
Also, I will use stackoverflow next time.

Comment by Michael Grundy [ 20/Mar/13 ]

Hi Kurt -

You don't need to pass the pipeline as an array []. You'll get the expected results with this:

db.test.aggregate(
 {$match: {unitStatus: {$exists:true, $nin: ["es_pws", "es_stl"]}} },
 {$project:{_id: 1,unitStatus:1}}
);

In the future, I'd recommend posting a question like this on one of our free support channels. Filing tickets under the SERVER project is not the best way to get visibility for issues you encounter, as we use this project for tracking bugs and feature requests. If you have a support contract with 10gen, then filing commercial support tickets is your best option. If not, the 10gen development and support teams actively monitor posts to stackoverflow (http://stackoverflow.com/questions/tagged/mongodb) and to the mongo google group (https://groups.google.com/forum/?fromgroups#!forum/mongodb-user)

Thanks!
Mike

Comment by Kurt Agius [ 20/Mar/13 ]

Thanks for the very detailed explanation will definitely keep this in mind for later.
Hopefully someone replies about my actual issue.
(maybe we should clear these comments because they are technically out of subject).

Comment by Tad Marshall [ 20/Mar/13 ]

The two formatting "macros" that we use most often are "code" and "noformat". Each of these should be on a separate line and enclosed in wavy braces like {code} and {noformat}. In the previous sentence, I 'escaped" the wavy braces with backslash to keep them from being interpreted by the Jira display code. You use them in pairs; one before the text you want to keep from being treated as normal text by Jira and one after.

{code} will display the entire quoted block while {noformat} will create a fixed size block with a scrollbar. {code} is best for short snippets, {noformat} is best for longer ones. Both macros will preserve indentation, which is pretty helpful in JSON output and in JavaScript code excerpts.

{code} example

{
    "name" : "value"
}

{noformat} example

{
    "name" : "value"
}

You should be able to edit your own comments after posting them, but you may not be able to change the title and description.

Comment by Kurt Agius [ 20/Mar/13 ]

Thanks, could i have done that myself ? (especially for formatting commands)

Comment by Tad Marshall [ 20/Mar/13 ]

I updated the title and added formatting commands to the description.

Comment by Kurt Agius [ 20/Mar/13 ]

P.S the title is wrong. I meant to write "using $match and $project" but i can't edit the title.
(maybe someone can edit it or i will close and re-open it again)

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