I’m trying to search the order_id in my MongoDB atlas collection , I tried many steps but I’m unable to find the correct solution.
I have a separate API which has data of all the orders and it goes on increasing as new orders arrive, I just want to add only new orders to my MongoDB collection , but when I execute the following code it appends it in the collection without checking that it already exists or not.
let parsed_response = JSON.parse(res);
var l = parsed_response.length;
for (var i = 0; i < l; i++) {
collection.insertOne({
"order_id": parsed_response[i].id,
"order_number": `${parsed_response[i].number}`,
"name": parsed_response[i].shipping.first_name + " " + parsed_response[i].shipping.last_name,
"address_line_1": parsed_response[i].shipping.address_1,
"address_line_2": parsed_response[i].shipping.address_2,
"date": parsed_response[i].shipping.date,
"city": parsed_response[i].shipping.city,
"postcode": parsed_response[i].shipping.postcode,
"state": parsed_response[i].shipping.state,
"country": parsed_response[i].shipping.country,
"ordered_item_id": parsed_response[i].line_items[0].id,
"ordered_item_name": parsed_response[i].line_items[0].name,
"ordered_item_quantity": parsed_response[i].line_items[0].quantity,
"payment_method": parsed_response[i].payment_method_title
}, () => {
console.log("Added to database");
});
}
I tried doing the search with
findOne({ "order_id": parsed_response[i].id })
but i am totally lost. Please help