0

So I have data which has null or no document for certain data points. Here is some set of commands demonstrating data

db.perfR.find({platform:"mac", "filename" : "f1.json"},{filename:1, platform:1, int_secs:1, version:1, _id:0})

{ "filename" : "f1.json", "platform" : "mac", "version" : "1.4.14", "int_secs" : 15 }

Indicates only one entry for "Mac"

db.perfR.find({platform:"win", "filename" : "f1.json"},{filename:1, platform:1, int_secs:1, version:1, _id:0}).count()

0

Indicates no entry for Win

Here is the aggregation command

db.perfR.aggregate([ {$sort:{version: -1 }},{ $group:{ _id: {platform:"win", "filename" : "f1.json", version:"$version"}, avgSecs: {$avg:"$int_secs"} } }])

And output

{ "_id" : { "platform" : "win", "filename" : "f1.json", "version" : "1.4.13" }, "avgSecs" : 759 }
{ "_id" : { "platform" : "win", "filename" : "f1.json", "version" : "1.4.14" }, "avgSecs" : 415.46153846153845 }
{ "_id" : { "platform" : "win", "filename" : "f1.json", "version" : "3.0.0_dev" }, "avgSecs" : 563.8333333333334 }

I am quiet confused where does Mongodb get all that data from and how to restrict it to look for values that are present.

Note: There are other entries for other versions, which I have not shown here for brevity.

Any pointers to improve the aggregation command would be useful

1 Answer 1

1

You are providing string literals in your group _id specification:

_id: {platform:"win", "filename" : "f1.json", version:"$version"}

"win" and "f1.json" are just string literals. Only one field from documents is used in _id - it's "$version". So you got three groups for three different version values which you have in your documents.

Correct _id definition would be

_id: {platform:"$platform", filename : "$filename", version:"$version"}

That will group documents by three fields.

NOTE: if you want to filter collection by some field, you should use $match stage for that.

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.