I have an event object which is fetched from MongoDB via Mongoose plug in. Have done some of the operation in this object and finally I want to save this object in another collection with same structure.
For this I need to delete all properties starts from '_' or '__' like '_id' and '__v'. I have written code like:
console.log(event);
console.log(delete event._id);
console.log(event);
Line 1 and 3 prints same value with _id property. Same thing works fine if I delete some other property like 'code'. here is complete event object:
{ _id: 5a51019b211d740a174a1fba,
__t: 'EventModel',
code: '02',
event: 'testMe',
source: 'user',
target: 'cronjob',
__v: 0,
params:
[ { key: 'Nodics',
value: 'Framework',
_id: 5a51019b211d740a174a1fbb } ],
log: [ 'Published Successfully' ],
hits: 1,
type: 'ASYNC',
state: 'FINISHED',
testProperty: 'Nodics Framework',
updatedDate: 2018-01-13T17:04:15.288Z,
creationDate: 2018-01-13T17:04:15.288Z
}
Please help me out to understand root cause and how I can deal with this. for me it looks direct or inherited properties issue. but required experts comments.
Did you try lean() when you query? like find(query).lean()... check this http://blog.sandromartis.com/2016/05/08/mongoose-lean/ This will allow you to do any operation to the object.
Other way could be extending the root object with removing unwanted properties from it. you can use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
example:
var obj = { a: 1 };
var copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }
delete copy._id;//or whatever you want
console.lg(copy);//check this doesnt have the _id;
Thanks Hope this helps
It looks like the reason you can't delete some of those properties is that they have been defined as "non-configurable." However, your approach is sub-optimal because it necessarily mutates its argument. Never, ever, mutate an argument. Ever.
Rather than delete properties from event
, you ought to construct a new object that contains only the properties you desire.
Object.getOwnPropertyNames
Libraries like lodash have sugar for doing this kind of thing:
// safelist (using lodash)
var saveableEvent = _.pick(event, ['code', 'event', 'source', 'target', 'params', 'log', 'hits', 'type', 'state', 'testProperty', 'updatedDate', 'creationDate']);
// or blocklist (using lodash)
var saveableEvent = _.omit(event, ['_id', '__t', '__v']);
// only copy object's own properties (without lodash)
var saveableEvent = Object.getOwnPropertyNames(event)
.reduce(function(out, propName) {
return Object.assign(out, event[propName])
}, {})
// create editable copy of object, then remove undesired props
var saveableEvent = event.toObject();
delete saveableEvent._id;
I recently had a similar problem. I used loadash to cut off the values I didn't need from the array, but had to use .toObject()
in the fetched object so I could use it as an argument. Something like:
let resultExample = _.omit(exampleObject.toObject(), ['x', 'y', 'z']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With