Hello, I am new to MongoDB. I have installed MongoDB Compass on my local desktop for learning purposes.
I have data which has scripcode, timestamp and close prices. I want to create a new field as per “row_number over partition by scripcode order by timestamp” in SQL language.
Even I tried writing a simple moving average calculation for 2 periods.
I understand that the data is not being partitioned correctly at the scripcode level.
Refer the below code which is not providing expected results (Attached MongoDB Compass pipeline image as well).
{
‘$setWindowFields’: {
‘partitionBy’: ‘S_scripcode’,
‘sortBy’: {
‘T_timestamp’: 1
},
‘output’: {
‘docNo’: {
‘$documentNumber’: {}
},
‘sma’: {
‘$avg’: ‘$C_close’,
‘window’: {
‘documents’: [
-2, 0
]
}
}
}
}
}
Kindly assist.