$match stage is not giving any output in MongoDB aggregation
I need to print the documents with more than the average of one field in the same collection.
In the aggregation pipeline, I am not getting the avg_pmnt parameter available to $match
stage (next stage)
For the following code
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
])
I am getting the following output:
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 14191 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 32642 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 33348 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 45864 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 82261 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 7565 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 44895 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 19502 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 47924 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 49524 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 50219 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 1491 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 17876 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 34638 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 101245 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 85411 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 11044 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 83598 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 6067 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 14571 }
Type "it"
for more
But I want to filter them
so, I tried this:
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: {AMNT: {$gt: "$avg_pmnt"} }}
])
For which I am not getting any documents (empty set).
I also tried
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: {AMNT: {$gt: 1} }}
])
for which I am getting all the documents as the condition satisfies for every document.
mongodb aggregation-framework
add a comment |
I need to print the documents with more than the average of one field in the same collection.
In the aggregation pipeline, I am not getting the avg_pmnt parameter available to $match
stage (next stage)
For the following code
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
])
I am getting the following output:
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 14191 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 32642 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 33348 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 45864 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 82261 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 7565 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 44895 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 19502 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 47924 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 49524 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 50219 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 1491 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 17876 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 34638 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 101245 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 85411 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 11044 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 83598 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 6067 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 14571 }
Type "it"
for more
But I want to filter them
so, I tried this:
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: {AMNT: {$gt: "$avg_pmnt"} }}
])
For which I am not getting any documents (empty set).
I also tried
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: {AMNT: {$gt: 1} }}
])
for which I am getting all the documents as the condition satisfies for every document.
mongodb aggregation-framework
add a comment |
I need to print the documents with more than the average of one field in the same collection.
In the aggregation pipeline, I am not getting the avg_pmnt parameter available to $match
stage (next stage)
For the following code
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
])
I am getting the following output:
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 14191 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 32642 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 33348 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 45864 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 82261 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 7565 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 44895 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 19502 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 47924 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 49524 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 50219 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 1491 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 17876 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 34638 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 101245 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 85411 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 11044 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 83598 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 6067 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 14571 }
Type "it"
for more
But I want to filter them
so, I tried this:
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: {AMNT: {$gt: "$avg_pmnt"} }}
])
For which I am not getting any documents (empty set).
I also tried
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: {AMNT: {$gt: 1} }}
])
for which I am getting all the documents as the condition satisfies for every document.
mongodb aggregation-framework
I need to print the documents with more than the average of one field in the same collection.
In the aggregation pipeline, I am not getting the avg_pmnt parameter available to $match
stage (next stage)
For the following code
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
])
I am getting the following output:
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 14191 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 32642 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 33348 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 45864 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 82261 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 7565 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 44895 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 19502 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 47924 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 49524 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 50219 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 1491 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 17876 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 34638 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 101245 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 85411 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 11044 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 83598 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 6067 }
{ "_id" : 1, "avg_pmnt" : 32431.65567765568, "AMNT" : 14571 }
Type "it"
for more
But I want to filter them
so, I tried this:
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: {AMNT: {$gt: "$avg_pmnt"} }}
])
For which I am not getting any documents (empty set).
I also tried
db.payments.aggregate([
{$group: {
"_id":1,
"avg_pmnt": {$avg: "$AMOUNT"},
"AMNT": {$push: "$AMOUNT"}
} },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: {AMNT: {$gt: 1} }}
])
for which I am getting all the documents as the condition satisfies for every document.
mongodb aggregation-framework
mongodb aggregation-framework
edited 19 hours ago
mickl
11.4k51535
11.4k51535
asked 19 hours ago
Anjan Kailash
287
287
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
you can also use $redact
to compare within the same document fields and filter the documents
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$redact :{$cond : [{$gt: ["$AMNT", "$avg_pmnt" ]},"$$DESCEND","$$PRUNE"]}}
])
add a comment |
You should use $expr as you're trying to compare two fields inside of $match
stage
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: { $expr: { $gt: [ "$AMNT", "$avg_pmnt" ] } } }
])
Thank you. Actually, I am new to MongoDB.
– Anjan Kailash
19 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943019%2fmatch-stage-is-not-giving-any-output-in-mongodb-aggregation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can also use $redact
to compare within the same document fields and filter the documents
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$redact :{$cond : [{$gt: ["$AMNT", "$avg_pmnt" ]},"$$DESCEND","$$PRUNE"]}}
])
add a comment |
you can also use $redact
to compare within the same document fields and filter the documents
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$redact :{$cond : [{$gt: ["$AMNT", "$avg_pmnt" ]},"$$DESCEND","$$PRUNE"]}}
])
add a comment |
you can also use $redact
to compare within the same document fields and filter the documents
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$redact :{$cond : [{$gt: ["$AMNT", "$avg_pmnt" ]},"$$DESCEND","$$PRUNE"]}}
])
you can also use $redact
to compare within the same document fields and filter the documents
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$redact :{$cond : [{$gt: ["$AMNT", "$avg_pmnt" ]},"$$DESCEND","$$PRUNE"]}}
])
answered 13 hours ago
Saravana
7,53421633
7,53421633
add a comment |
add a comment |
You should use $expr as you're trying to compare two fields inside of $match
stage
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: { $expr: { $gt: [ "$AMNT", "$avg_pmnt" ] } } }
])
Thank you. Actually, I am new to MongoDB.
– Anjan Kailash
19 hours ago
add a comment |
You should use $expr as you're trying to compare two fields inside of $match
stage
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: { $expr: { $gt: [ "$AMNT", "$avg_pmnt" ] } } }
])
Thank you. Actually, I am new to MongoDB.
– Anjan Kailash
19 hours ago
add a comment |
You should use $expr as you're trying to compare two fields inside of $match
stage
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: { $expr: { $gt: [ "$AMNT", "$avg_pmnt" ] } } }
])
You should use $expr as you're trying to compare two fields inside of $match
stage
db.payments.aggregate([
{$group: { "_id":1, "avg_pmnt": {$avg: "$AMOUNT"}, "AMNT": {$push: "$AMOUNT"} } },
{$unwind: "$AMNT"},
{$project: {avg_pmnt: "$avg_pmnt", AMNT: "$AMNT"}},
{$match: { $expr: { $gt: [ "$AMNT", "$avg_pmnt" ] } } }
])
answered 19 hours ago
mickl
11.4k51535
11.4k51535
Thank you. Actually, I am new to MongoDB.
– Anjan Kailash
19 hours ago
add a comment |
Thank you. Actually, I am new to MongoDB.
– Anjan Kailash
19 hours ago
Thank you. Actually, I am new to MongoDB.
– Anjan Kailash
19 hours ago
Thank you. Actually, I am new to MongoDB.
– Anjan Kailash
19 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943019%2fmatch-stage-is-not-giving-any-output-in-mongodb-aggregation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown