I try to insert some data into an existing document:
Graph.update(
{ id: id },
{
$push: {
tooltips: {
element: Session.get('tooltipID'),
text: text
}
}
}
);
This is working quite well, but if there is already data in tooltips, this one should be updated instead of adding a new object, as there can only be a unique object for a unique element (tooltipID).
I want to avoid these multiple entries for the same element-value in tooltips.
{
"_id" : "c4bKur6TKcgFHGLZZ",
"data" : "[]",
"tooltips" : [
{
"element" : "2d4edaaf",
"text" : "Lorem"
},
{
"element" : "2d4edaaf",
"text" : "ipsum"
}
]
}
But it should be possible to have more then one object in tooltips, if element is really unique...
I tried to add a upsert:true
to the update(), but that doesn't work.
Definitely upsert
won't work with embedded document.
One approach can be
Graph.update({id:id},
{
$addToSet: {
'tooltips': {
element: Session.get('tooltipID'),
text: text
}
}
})
It will ensure no duplicate on tooltips;
similarly you can use $set
Graph.update({
id:id,
'tooltips.element': Session.get('tooltipID')
},
{
$set: {
'tooltips.$.text':text
}
})
or you can pull before push
Graph.update({
"id":id
}, {
$pull: {
'tooltips': {
"element": Session.get('tooltipID')
}
}
})
Graph.update(
{ id: id },
{
$push: {
tooltips: {
element: Session.get('tooltipID'),
text: text
}
}
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.