MongoDB Aggregation Pipeline Multiple Groups Complicating Pipeline
By : UmAir KhAnn
Date : March 29 2020, 07:55 AM
To fix the issue you can do If I understand the question correctly, this is what you're looking for. The key concept is that you can construct compound _id's from multiple fields. code :
db.collection.aggregate(
[
{$match: {cdr3_seq_aa_len: {$gt: 3}}},
{$group:
{
_id: {donor: "$donor", cdr3_seq_aa: "$cdr3_seq_aa"},
donor_cdr3_seq_aa_count: {$sum: 1},
cdr3_seq_aa_len: {$first: "$cdr3_seq_aa_len"}
}
},
{$group:
{
_id: {donor: "$_id.donor", len: "$cdr3_seq_aa_len"},
num_strings_with_this_length: {$sum: 1},
total_doc_count_by_length:
{$sum: "$donor_cdr3_seq_aa_count"}
}
}
])
|
MongoDB $geoNear aggregation pipeline (using query option and using $match pipeline operation) giving different no of re
By : Arthur Milliken
Date : March 29 2020, 07:55 AM
wish helps you Few assumptions:- 1. Assume there are 300 records that match based on the location.
|
Using $sum and $avg with $addFields in MongoDB Aggregation
By : user1416825
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I realize now that while one can do this, all that's really happening here is that one is getting the average of a sum of one value. Because there's no $grouping going on, just one document, not all, is being read, and the average ($avg) of the $sum of that value from that one document is being outputted. And of course, considering it's only one value, the $avg of the $sum is always going to be the same as the original value itself. code :
value: 10
averageOfValue: 10
|
Add field to documents after $sort aggregation pipeline which include its index in sorted list using MongoDb aggregation
By : sunrise3333
Date : March 29 2020, 07:55 AM
Any of those help One of the workaround for this situation is to convert your all documents into one single array and hence resolve the index of the document using this array with help of $unwind and finally project the data with fields as required. code :
db.collection.aggregate([
{ $sort: { points: 1 } },
{
$group: {
_id: 1,
register: { $push: { _id: "$_id", name: "$name", points: "$points" } }
}
},
{ $unwind: { path: "$register", includeArrayIndex: "order" } },
{ $match: { "register.name": "x4" } },
{
$project: {
_id: "$register._id",
name: "$register.name",
points: "$register.points",
order: 1
}
}
]);
|
$addFields in mongoDB aggregation pipeline
By : mistvan22
Date : March 29 2020, 07:55 AM
it fixes the issue As noted by tom slabbaert, $regexMatch is available only on MongoDB 4.2.x, also, your regex is not valid (should either be Test_ or Test_.*). However, the syntax you are using will only produce customField: true (or false), not a count. Counts are aggregate functions and therefore need to operate on a group of documents. An aggregation pipeline operates on each single document unless you use a grouping stage like $group, $bucket or $count. code :
[
{ $match: { 'type': { $regex: /Test_.*/ } } },
{ $count: 'customField' }
]
|