I am trying to remove an object from every item of an array but leaving the rest of the object and array element as is. For example:
[
{
_id: 1,
analytics: [
{
date: ISODate("2017-09-10T14:00:00Z"),
country: { uk: 1, fr: 5, de: 15 },
browser: { ie: 2, ff: 5, ch: 14 }
},
{
date: ISODate("2017-09-10T14:00:00Z"),
country: { uk: 1, fr: 5, de: 15 },
browser: { ie: 2, ff: 5, ch: 14 }
},
]
},
{
_id: 2,
analytics: [
{
date: ISODate("2017-09-10T14:00:00Z"),
country: { uk: 5, fr: 1, de: 5 },
browser: { ie: 14, ff: 2, ch: 5 }
},
{
date: ISODate("2017-09-10T15:00:00Z"),
country: { uk: 5, fr: 1, de: 5 },
browser: { ie: 14, ff: 2, ch: 5 }
]
}
]
I want to run a command that will remove browser object from each member of the array, for each document. The result would look like
[
{
_id: 1,
analytics: [
{
date: ISODate("2017-09-10T14:00:00Z"),
country: { uk: 1, fr: 5, de: 15 }
},
{
date: ISODate("2017-09-10T14:00:00Z"),
country: { uk: 1, fr: 5, de: 15 }
},
]
},
{
_id: 2,
analytics: [
{
date: ISODate("2017-09-10T14:00:00Z"),
country: { uk: 5, fr: 1, de: 5 }
},
{
date: ISODate("2017-09-10T15:00:00Z"),
country: { uk: 5, fr: 1, de: 5 }
]
}
]
something the lines of:
db.collection.update({}, { $pull: { analytics: { country: { $exists: true } } } }, { multi: true});
?
thanks