MongoDB query results always in alphabetical order instead of specified order in query

Ok, I ran the following aggregation query on a MongoDB database. The important bit is the last stage, the "$project" stage, which specifies the order of the output.
-------------------------------------
db.transactions.aggregate([
{
$lookup:
{
from: 'clients',
localField: 'client',
foreignField: '_id',
as: 'clients_docs'
}
},
{
$match:
{
clients_docs: { $ne: [] }
}
},
{
$addFields:
{
clients_docs:
{
$arrayElemAt: ["$clients_docs", 0]
}
}
},
{
$match:
{
$and:
[
{ "updated_at": { $gte: ISODate("2022-01-01") } },
{ "updated_at": { $lte: ISODate("2022-01-31") } },

]
,"status": { $eq: "PAID" }
,"clients_docs.is_send_client": { $eq: true }
}
},
{
$group:
{
_id:
{
year_month: { $dateToString: { "date": "$updated_at", "format": "%Y-%m" } }
,client_name: "$clients_docs.client_name"
,client_label: "$clients_docs.client_label"
,client_code: "$clients_docs.client_code"
,client_country: "$clients_docs.client_country"
,base_curr: "$clients_docs.client_base_currency"
,inv_curr: "$clients_docs.client_invoice_currency"
,dest_curr: "$store.destination_currency"
}
,total_vol: { $sum: "$USD_Value" }
,total_tran: { $sum: 1 }
}
},
{
$project:
{
_id: 0
,period: "$_id.year_month"
,clnt_name: "$_id.client_name"
,clnt_label: "$_id.client_label"
,clnt_code: "$_id.client_code"
,clnt_country: "$_id.client_country"
,base_currency: "$_id.base_curr"
,inv_currency: "$_id.inv_curr"
,dest_currency: "$_id.dest_curr"
,volume_usd: "$total_vol"
,tran_count: "$total_tran"
}
},
{
$limit: 10
}
])
-------------------------------------------
The output I get in other software, like NoSQLBooster or Studio3T is:

{
"period" : "2022-01",
"clnt_name" : "client A",
"clnt_label" : "client A",
"clnt_code" : 1000,
"clnt_country" : "AU",
"base_currency" : "AUD",
"inv_currency" : "AUD",
"dest_currency" : "MXN",
"volume_usd" : 22442.29,
"tran_count" : 13
},

But in DataGrip I get:

{
"base_currency": "AUD",
"clnt_code": 1000,
"clnt_country": "AU",
"clnt_label": "client A",
"clnt_name": "client A",
"dest_currency": "MXN",
"inv_currency": "AUD",
"period": "2022-01",
"tran_count": 13,
"volume_usd": 22442.29
},

So a different order than what I specified, and it appears to be in alphabetical order. It's only DataGrip that is generating the output as such.

Is there a setting in DataGrip that needs to be changed to not have the fields sorted in alphabetical order?

 

(I also submitted a support ticket for this, but no solution yet).

2
2 comments

Datagrip for MongoDB is near perfect. It's not completely perfect because of this problem.

 
Normal text
 
0

Please sign in to leave a comment.