I'm not good at mongodb, so it is hard to manipulate sub documents. What I want to do is implement address book like this:
{
[[email protected]]: [
{
email: '[email protected]',
createdAt: '0000-00-00',
updatedAt: '0000-00-00'
}
],
[[email protected]]: [
{ ... },
{ ... }
]
}
I called that collection 'addressbook', and each item is Array type to contain address information like email, image, date of created something like that.
The problem is I don't know how to do it. Looks like I need to use some operators like $addToSet or $push, but I don't get it how to use them correctly. What I tried:
update({
email: '[email protected]'
}, {
$push: {
'address.$email': {
email: target,
createdAt: new Date(),
updatedAt: new Date()
}
}
})
One thing I knew was using upsert option to update collection makes new document if it doesn't exists, but still docs are not creating. It will be very appreciate to gimme a hand.
$pushor$pullto add or remove content to or from an existing document.