Questions tagged [aggregation-framework]

The MongoDB Aggregation Framework provides a means to reshape and aggregate data in MongoDB 2.2+.

The MongoDB Aggregation Framework provides a means to reshape and aggregate data in MongoDB 2.2+.

If you're familiar with SQL, the Aggregation Framework provides similar functionality to GROUP BY and related SQL operators, as well as simple forms of "self joins". Additionally, the Aggregation Framework provides projection capabilities to reshape the returned data. Using the projections in the Aggregation Framework you can add computed fields, create new virtual sub-objects, and extract sub-fields into the top-level of results.

Other options for aggregating data in MongoDB include:

Documentation

Related Tags

12273 questions
534
votes
12 answers

Update MongoDB field using value of another field

In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like: UPDATE Person SET Name = FirstName + ' ' + LastName And the MongoDB pseudo-code would be: db.person.update(…
Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
476
votes
19 answers

Retrieve only the queried element in an object array in MongoDB collection

Suppose you have the following documents in my collection: { "_id":ObjectId("562e7c594c12942f08fe4192"), "shapes":[ { "shape":"square", "color":"blue" }, { "shape":"circle", …
Sebtm
  • 7,002
  • 8
  • 29
  • 32
333
votes
9 answers

MongoDB SELECT COUNT GROUP BY

I am playing around with MongoDB trying to figure out how to do a simple SELECT province, COUNT(*) FROM contest GROUP BY province But I can't seem to figure it out using the aggregate function. I can do it using some really weird group…
Steven
  • 13,250
  • 33
  • 95
  • 147
309
votes
11 answers

MongoDB: Combine data from multiple collections into one..how?

How can I (in MongoDB) combine data from multiple collections into one collection? Can I use map-reduce and if so then how? I would greatly appreciate some example as I am a novice.
user697697
  • 3,327
  • 3
  • 18
  • 12
228
votes
10 answers

Find duplicate records in MongoDB

How would I find duplicate fields in a mongo collection. I'd like to check if any of the "name" fields are duplicates. { "name" : "ksqn291", "__v" : 0, "_id" : ObjectId("540f346c3e7fc1054ffa7086"), "channel" : "Sales" } Many thanks!
Chris
  • 3,004
  • 3
  • 21
  • 26
187
votes
4 answers

mongodb group values by multiple fields

For example, I have these documents: { "addr": "address1", "book": "book1" }, { "addr": "address2", "book": "book1" }, { "addr": "address1", "book": "book5" }, { "addr": "address3", "book": "book9" }, { "addr": "address2", …
fervid
  • 2,033
  • 3
  • 13
  • 13
177
votes
6 answers

Include all existing fields and add new fields to document

I would like to define a $project aggregation stage where I can instruct it to add a new field and include all existing fields, without having to list all the existing fields. My document looks like this, with many fields: { obj: { …
samuelluis
  • 1,771
  • 2
  • 11
  • 3
165
votes
10 answers

mongodb count num of distinct values per field/key

Is there a query for calculating how many distinct values a field contains in DB. f.e I have a field for country and there are 8 types of country values (spain, england, france, etc...) If someone adds more documents with a new country I would like…
Liatz
  • 4,997
  • 7
  • 28
  • 33
151
votes
3 answers

Mongodb Explain for Aggregation framework

Is there an explain function for the Aggregation framework in MongoDB? I can't see it in the documentation. If not is there some other way to check, how a query performs within the aggregation framework? I know with find you just do…
SCB
  • 3,034
  • 2
  • 25
  • 24
148
votes
8 answers

$lookup on ObjectId's in an array

What's the syntax for doing a $lookup on a field that is an array of ObjectIds rather than just a single ObjectId? Example Order Document: { _id: ObjectId("..."), products: [ ObjectId("...."), ObjectId("..
Jason Lin
  • 1,957
  • 3
  • 16
  • 18
142
votes
5 answers

What's the $unwind operator in MongoDB?

This is my first day with MongoDB so please go easy with me :) I can't understand the $unwind operator, maybe because English is not my native language. db.article.aggregate( { $project : { author : 1 , title : 1 , tags :…
gremo
  • 47,186
  • 75
  • 257
  • 421
136
votes
3 answers

How to filter array in subdocument with MongoDB

I have array in subdocument like this { "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 1 }, { "a" : 2 }, { "a" : 3 }, { …
chenka
  • 1,463
  • 2
  • 10
  • 5
129
votes
3 answers

MongoDB aggregation framework match OR

Is it possible to do an OR in the $match? I mean something like this: db.articles.aggregate( { $or: [ $match : { author : "dave" }, $match : { author : "john" }] } );
Gergely Németh
  • 1,325
  • 2
  • 9
  • 7
116
votes
11 answers

Does MongoDB's $in clause guarantee order

When using MongoDB's $in clause, does the order of the returned documents always correspond to the order of the array argument?
105
votes
3 answers

Return only matched sub-document elements within a nested array

The main collection is retailer, which contains an array for stores. Each store contains an array of offers (you can buy in this store). This offers array has an array of sizes. (See example below) Now I try to find all offers, which are available…
Vico
  • 1,269
  • 2
  • 12
  • 16
1
2 3
99 100