0
votes

I tried the query below but for some reason I get a Unknown server error occurred when processing this request. I am a bit new to mongodb but when reading the documentation for Mongodb shell and CosmosDB, all the operators listed seem supported and this look syntactically correct.

db.getCollection('transactions').aggregate(
    [
        {
            $match:{
                bdr_date: 20181031, 
                make: { $exists: false }, 
                shard_key: '1249_2018-10', 
                time: { $gt: 2344 }
            }
        },
        {
            $group: { 
                total_sales: { 
                    $sum: { 
                        $multiply: [
                            "$sales",
                            "$units"
                        ]
                    }
                }
            }
        } 
    ]
)

My CosmosDB uses MongoAPI and contains one collection named transactions with documents that have many keys but here are the important ones:

{
    "bdr_date" : INT(32),
    "time" : INT(32),
    "sales" : Double,
    "units" : INT(32),
    "shard_key" : String
}

There is also a key called { "make": INT(32) } in some documents where we had a script that filled data holes. I only wanted documents with real data so I filtered out documents with the "make" key. bdr_date is in yyyymmdd format and time is in HHMM format.

The error code doesn't really say much else except for the unknown server error exception so I'm pretty stumped on what exactly I am doing wrong. Is there something simple that I am missing?

If you need more information, I'll do my best to accommodate. I appreciate the help!

1
Send an email to [email protected] with your Cosmos DB Account name, any time ranges the errors happened with, and (even better) a simple repro. For 500 errors, it often requires an investigation on the backend. - Chris Anderson-MSFT
thanks. got a response quickly! - Aaron Arima

1 Answers

0
votes

After sending an email to [email protected], apparently there is some bug which will be fixed in the future that currently makes Cosmos return the wrong error message.

In my case, it ended up being something simple. I just forgot to put _id key into the $group operator. For my case, making _id: null fixed the issue.

db.getCollection('transactions').aggregate(
    [
        {
            $match:{
                bdr_date: 20181031, 
                make: { $exists: false }, 
                shard_key: '1249_2018-10', 
                time: { $gt: 2344 }
            }
        },
        {
            $group: { 
                _id: null,
                total_sales: { 
                    $sum: { 
                        $multiply: [
                            "$sales",
                            "$units"
                        ]
                    }
                }
            }
        } 
    ]
)