I’m working with MongoDB to create a stock ticker app, and I’m trying to figure out how to structure the db + queries. Right now, I have a collection of object that look like this:
{
"id":"mongodb",
"website":"www.mongodb.com",
"history":[
{"price":350, "timestamp":"2020-10-21T13:28:06.419Z"},
{"price":320,"timestamp":"2020-10-21T13:28:06.419Z"},
{"price":310,"timestamp":"2020-10-21T13:28:06.419Z"}
]
}
I have a function that calls an API in bulk and returns ~5k stocks with their id. I want to upsert this array as follows: (1) based on the id, update history.price by pushing a new object with price and timestamp (2) if the id doesn’t exist, create a new document with the rest of the API data (id, website) and write that stocks price to a history array as well as the first entry.
Right now, I’m thinking about bulk.find.upsert, but I can’t figure out how to pass the whole array to this so that it can upsert based off the ID.
Code so far (very basic):
var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { id: array[objId].id } ).upsert().update(
{
$setOnInsert: { Name: array[objId].name, website: array[objId].website .history: [{price: array[objid].price, timestamp: ISO}] },
$set: { history: [{price: array[objId].price, timestamp: ISO}] }
}
);
bulk.execute();