In my Mongo Schema, I have defined the following structure:
var OrderSchema = new Schema({
postcode: String,
...
status: {
last_check: { type: Date },
date: Date,
code: String,
postnum: String,
text: String,
class: String
},
});
I am using a function to save data (the old method is commented out):
function save_order(data) {
// var order = new Order(data);
// var upsertData = order.toObject();
// delete upsertData._id;
// Order.findOneAndUpdate({postcode: order.postcode}, upsertData, {upsert: true}, function (err) {
Order.update({postcode: data.postcode}, {$set:data}, function (err) {
if (err) console.log('err');
});
}
An example of data passed to the function is shown below:
{ postcode: 'BV123456789BY',
status:
{ last_check: 1413539153572,
code: '06',
postnum: '247431',
date: Thu Oct 16 2014 09:52:00 GMT+0300 (Восточная Европа (лето)),
text: '06. Поступило в участок обработки почты (247431) Светлогорск - 1' } }
The function to set status.class works well and does not overwrite the existing status values:
function setByOrderId(id, data) {
// data = {'status.class': 'danger'}
Order.update({_id: id}, {$set: data}, function (err) {
if (err) console.log('err');
});
}
However, the problem arises when updating the status as the value of status.class disappears. How can I update the status without overwriting status.code? Thank you.