MongoDB is a NoSQL database with the intent of storing billions of records. It offers the aggregation framework as a tool to manipulate and query records. This tag is intended to be used with question specific to this aggregation framework, since aggregation is too general.
Questions tagged [mongodb-aggregation]
147 questions
5
votes
2 answers
Finding most commonly used word in a string field throughout a collection
Let's say I have a Mongo collection similar to the following:
[
{ "foo": "bar baz boo" },
{ "foo": "bar baz" },
{ "foo": "boo baz" }
]
Is it possible to determine which words appear most often within the foo field (ideally with a count)?
For…

user1381745
- 3,850
- 2
- 21
- 35
5
votes
1 answer
Mongodb WeekofMonth?
I am stuck with achieving weekofMonth instead of WeekofYear. Can somebody guide me on how to get this right?
db.activity.aggregate([
{
$group:{
_id: {
week: { $week: "$createdAt" },
month: {…

Ayyappa A
- 657
- 3
- 8
- 24
4
votes
1 answer
Mongodb - how to use an aggregate text search with a find query
I'm using the aggregate method in Mongodb against a text search. I've tried various way with this and still can't find the correct way to filter my results. I've setup an index and it works fine with just the $text search and also works fine with…

darylhedley
- 258
- 1
- 8
- 23
4
votes
1 answer
Mongoose extract first subarray element after filtering
I have array in subdocument like this
{
"_id" : ObjectId("512e28984815cbfcb21646a7"),
"descDay" : [
{
"language" : "en",
"desc": "day description"
},
{
"language" : "es",
…

irokhes
- 1,643
- 1
- 14
- 20
4
votes
0 answers
Spring mongodb: Aggregation with lookup, projection and slice
I have a collection ref consisting of
{
"_id": "refId"
"ref": "itemId"
}
and a collection item
{
"_id": "itemId"
"field1": ...
"array": [
{
"field2": "a"
},
{
"field2": "b"
}
]
}
Here i need to 'join' a.k.a…

Bondax
- 3,143
- 28
- 35
4
votes
2 answers
How to get sum of differences of two fields in MongoDB?
I already have a solution but I am looking at a solution which does all the job on MongoServer (Because I think it'd be faster and less memory consuming)
I have a Class Method like:
function getTotalOutstandingAmount(){
$outstandingAmount = 0;
…

enemetch
- 492
- 2
- 8
- 21
3
votes
1 answer
Querying with joins & filters in Mongoose
I'm new to Mongodb & using it in web application that I'm building using MEAN stack. My goal is to query two tables by joining them and applying a filter condition on them. For eg: I have two tables - Bike-BikeID,Registration No.,Make,Model &…

Rajkumar Kaliyaperumal
- 956
- 2
- 9
- 16
3
votes
1 answer
Save a subset of MongoDB(3.0) collection to another collection in Python
I found this answer - Answer link
db.full_set.aggregate([ { $match: { date: "20120105" } }, { $out: "subset" } ]);
I want do same thing but with first 15000 documents in collection, I couldn't find how to apply limit to such query (I tried using…

Darpan
- 5,623
- 3
- 48
- 80
3
votes
0 answers
perform atomic aggregate read and update in mongo?
I have some documents with below format
{
"count_used" : Int64,
"last_used" : Date,
"enabled" : Boolean,
"data" : String
}
I use the last_used field to sort on within an aggregate query so the last used document is served…

Flo Woo
- 949
- 1
- 12
- 26
3
votes
1 answer
MongoDB avoid duplicates using $addToSet in aggregation pipeline
there is aggregation pipeline:
db.getCollection('yourCollection').aggregate(
{
$unwind: {
path: "$dates",
includeArrayIndex: "idx"
}
},
{
$project: {
_id: 0,
dates:…

corry
- 1,457
- 7
- 32
- 63
3
votes
1 answer
Mongo does not return documents in aggregation using $gte and $date
In the MongoDB (v3.2.8) I have some bucketed values stored in a time sequence. I'm currently trying to aggregate data out of these buckets using the Morphia framework (v1.1.0), limiting the documents by the date of the document (a field, see sample…
user1230680
3
votes
1 answer
MongoDb exclude null in aggregation $project
I have a data collection in MongoDb with the shape:
[{ "_id": "1",
"Sq1" : 5,
"Sq1comment" : "In general you aaaaaa.",
"Sq2" : 8,
"S2comment" : null,
"Sq3" : 5,
"Sq3comment" : "A person bbbbb."
},
{ "_id": "2",
"Sq1" : 4,
…

Albert
- 81
- 4
3
votes
1 answer
Query for latest version of a document by date in mongoDB
I am trying to find a mongoDB script which will look at a collection where there are multiple records of the same document and only provide me with the latest version of each document as a result set.
I cannot explain it in English any better than…

Matt Lightbourn
- 597
- 3
- 20
3
votes
1 answer
MongoDB different result of aggregation
I have this query:
db.collection.aggregate([
{
$group: {
_id: "$PATIENT_ID",
MASS: { $max: "$max_field" }
}
}
]);
The result of this query is:
{ "_id" : "TCGA-A2-A0YD", "mass" : 0.994683301742671 }
{ "_id" :…

mydb
- 45
- 1
- 9
3
votes
1 answer
MongoDB, PyMongo - aggregate with find conditions
I have > 8000 records in my DB and this is one of them :
{
"_id" : ObjectId("57599c498c39598eafb781b9"),
"_class" : "vn.cdt.entity.db.AccessLog",
"url" : "/shop/huenguyenshop/browse",
"ip" : "10.0.0.238",
"sessionId" :…

Phan Duc
- 141
- 1
- 8