I have the following collection, and a mongoose query to concatArrays if there is no document with same exchange name, It should update if there is, but it only keeps concatenating instead of updating. I have studied the mongo, and mongoose docs, including different SO answers that did similar things. How might i achieve that with the logic i have below, also varied solutions are welcome.
const MarketsSchema = new Schema({
currency_id: String,
markets: [
{
exchange: "huobipro"
volume: "1406604450.62"
},
{
exchange: "binance"
volume: "1306345450.62"
}
]
});
const top_xchanges = [
{
exchange: 'huobipro',
num_trades: '0',
volume: '17251384.69',
volume_change: '-12490211.18',
volume_change_pct: '-0.4200'
},
{
exchange: 'cryptocom',
num_trades: '0',
volume: '20104559.36',
volume_change: '-17388578.10',
volume_change_pct: '-0.4638'
}
]
for(let market of top_xchanges) {
await ExchangeMarkets.updateOne({currency_id: coin },
[{$set:{
markets:{
$cond: [
{$in:[market.exchange, "$markets"]},
{
map: {
input: "$markets",
as: "$markt",
in: {
$cond: {
if: {$in: [market.exchange, "$markt"] },
then: {
exchange: market.exchange,
volume: market.volume
},
else: "$markt"
}
}
}
},
{$concatArrays:["$markets",[{exchange: market.exchange, volume:market.volume }]]}
]
}
}}]
);
}