I'm trying to add a field to a logs field that will gain it's name dynamically. E.g if I want to push an update about the field "Name" I'd want to be able to decide at runtime that it should be named so. I'm going to try my best to explain below.
How my data should look is this. Notice the tree: logs.(dynamicName).{ts, prop, value}. The dynamicName is inherited from the prop value.
"logs": {
"Name": [
{
"ts": 1436952585736,
"prop": "Name",
"value": "Fredd"
},
{
"ts": 1436952600336,
"prop": "Name",
"value": "Fred"
}
],
"description": [
{
"ts": 1436952585736,
"prop": "description",
"value": "A normal bloke"
}
],
The data I receive in my application is shaped like this:
{
"prop": "description",
"value": "A normal bloke"
}
I'm stumped. I simply want to $push to a new array with a dynamic name, e.g. like this:
var logName = "logs."+prop;
collection.update(
{_id: documentId},
{ $push:
{ logName:
{
"ts": Date.now(),
"prop": log.prop,
"value": log.value
}
}
}
But of course this cannot simply be: Mongo (or perhaps the node.js driver) takes the variable logName literally. And I really want to keep this data format instead of just having a singular logs array.