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
                    
                    32
                    
            votes
                
                3 answers
            
        Is it possible to do a $lookup aggregation between two databases in Mongodb?
I'm trying to do something like this: 
use user; 
db.user.aggregate([
    {
      $lookup:
        {
          from: "organization.organization",
          localField: "organizationId",
          foreignField: "uuid",
          as: "user_org"
     …
         
    
    
        Alexander Suraphel
        
- 10,103
- 10
- 55
- 90
                    26
                    
            votes
                
                3 answers
            
        group in mongo excluding null values
I have mongo query which does the group operation on the documents.
I have almost got the expected results except that I want to refine the results without empty or null values.
Currently my query looks like…
         
    
    
        starkk92
        
- 5,754
- 9
- 43
- 59
                    19
                    
            votes
                
                1 answer
            
        MongoDB - Rewind an $unwind nested array after $lookup using $group
MongoDB aggregation gets exponentially complicated by the minute!
I am in so far as to $unwind a nested array and then perform a $lookup by the _id of each object from the unwinded nested array. My final attempt is to reverse the unwinding with…
         
    
    
        Sun Lee
        
- 879
- 2
- 8
- 17
                    16
                    
            votes
                
                2 answers
            
        MongoDB aggregate: $lookup by _id, then get first element value
When I do $lookup, in my case the foreignField:"_id", I get the found element in an array. Here is one document from the output after $lookup has been done to retrieve fromUser and toUser from the users collection:
{ 
    _id : { from :…
         
    
    
        Granga
        
- 1,612
- 1
- 14
- 20
                    12
                    
            votes
                
                3 answers
            
        MongoDB get first and last document in aggregate query
How can I get first and last document based on time field.
I can use $group and get $first and $last document, but I don't need grouping here, just get first and last full document. Maybe I could use slice? This query doesn't work:
{
  "aggregate":…
         
    
    
        Michał Jurczuk
        
- 3,728
- 4
- 32
- 56
                    12
                    
            votes
                
                2 answers
            
        How to apply condition on $lookup result in mongoDB?
With the reference of my previous question, I have a question about $lookup with add some conditions. You may get enough reference about question from below link description.
Photo:
{_id: 1, photo_name: '1.jpg', photo_description: 'description 1',…
         
    
    
        Dipak
        
- 2,248
- 4
- 22
- 55
                    11
                    
            votes
                
                1 answer
            
        mongoose sum a value across all documents
I am looking to count all columns with the name amount in the documents that match my query,
 tickets.count({time: {$gte: a}, time: {$lte: tomorrow}}).then(function (numTickets) {
How can I get the total result of the document column called…
         
    
    
        maria
        
- 207
- 5
- 22
- 56
                    10
                    
            votes
                
                5 answers
            
        How to use a for loop in the mongodb shell?
How can i use a for loop in the mongo db shell?
My attemps are stucking at this point:
for (var i = 0; i <= 6; i=i+0.12){
var n = i + 0.12;
db.test.aggregate(
    { $sort: {'deviation': -1}},
    { $unwind: '$foo' },
    { $match: { 'foo.km': {$gt:…
         
    
    
        albert
        
- 163
- 2
- 3
- 12
                    8
                    
            votes
                
                1 answer
            
        mongodb group and subgroup counts
I have the following document type
{
    "_id" : "-fA2845ORqeyMUItKXfqZw",
    "user" : "553247ffdc8a4ade4bb09c5e",
    "state" : 2,
    "metadata" : {
        "language" : "en-US"
    },
    "pipeline" : {
        "api" : "http://localhost:4000",
 …
         
    
    
        khinester
        
- 3,398
- 9
- 45
- 88
                    8
                    
            votes
                
                1 answer
            
        mongodb - Find count of values in other collection
I want to find how many "taskId" of below collectionOne is present in collectionTwo.
Here, "taskId" in below collectionOne is embedded inside a array in a document.
Also, Let me know the different approaches to do this (if possible).…
         
    
    
        Uday Singh
        
- 83
- 1
- 4
                    6
                    
            votes
                
                1 answer
            
        Mongo DB distinct values group within array of objects
let say I have a document model like below
{
"_id" : "QggaecdDWkZzMmmM8",
"features" : [ 
    {
        "language" : "en",
        "values" : [ 
            {
                "name" : "feature 1",
                "values" : [ 
                   …
         
    
    
        Abdul Hameed
        
- 1,008
- 1
- 15
- 35
                    6
                    
            votes
                
                3 answers
            
        Converting mongo array to object with key-value pair
I have a mongo document that contains an array of Strings, and I need to convert this particular array of strings into an array of object containing a key-value pair. Below is my curent appraoch to it.
{
    "_id" :…
         
    
    
        blueren
        
- 2,730
- 4
- 30
- 47
                    6
                    
            votes
                
                1 answer
            
        Use array first field in mongo aggregate $lookup query to match a document
I want to use my array field 0th value to find a match in sale document using Mongo aggregate $lookup query. Here is my query:
db.products.aggregate([ 
{ 
    $match : { _id:ObjectId("57c6957fb190ecc02e8b456b")  }  
},
{
    $lookup : {
        from…
         
    
    
        Anurag pareek
        
- 1,382
- 1
- 10
- 21
                    5
                    
            votes
                
                1 answer
            
        MongoDB -- Find duplicate documents by multiple keys
I have a collection with documents that look like the following: 
{
        "_id" : ObjectId("55b377cb66b393427367c3e2"),
        "comment" : "This is a comment",
        "url_key" : "55b377cb66b393427367c3df", //This is an ObjectId from another…
         
    
    
        gleb1783
        
- 451
- 1
- 6
- 18
                    5
                    
            votes
                
                1 answer
            
        Reshape MongoDB aggregation result to field value documents
I have a data collection containing data in the following shape:
[ {_id: "1",
   aa1: 45, aa2: 56,
   bb1: 90, bb2: 78,
   cc1: 34, cc2: 98 },
  {_id: "2",
   aa1: 76, aa2: 56,
   bb1: 45, bb2: 67,
   cc1: 75, cc2: 87 } ]
on this data I perform a…
         
    
    
        Gerrie van Wyk
        
- 679
- 8
- 27