I'm trying to update data in MongoDB using NodeJs, I want to use user email as key and an object as value, like
changes : {
user@email.com : {
//key value pairs
},
user2@email.com : {
//key value pairs
}
}
Problem is when I'm using this code
var query = {id:"01"},
update = { "$set": { } };
update["$set"]["changes."+ req.session.email ] = req.body; //This line is causing problem
model.findOneAndUpdate(query, update, function(err, result){
if(err){
throw err;
}
console.log('Updated');
});
The "changes."+req.session.email
is formed as changes.user@email.com
and it is saved in MongoDb as
changes : {
user@email : {
com : {
//data from req.body
}
}
}
The .
in email is interpreted as property
,
So how do I tell it to use .
as string
in key
and not as property
. I tried wrapping req.session.email
in []
, didn't work.
Any help is appreciated, thanks.
EDIT
I know I'm not supposed to use .
in key names, but it is okay to use string
, and I just want to use a string that has a dot
in it, but I don't know how to make Node read it as a complete string.
Here is what I want:
changes : {
"user@email.com" : {
//key value pairs
},
"user2@email.com" : {
//key value pairs
}
}
What I'm getting right now is:
changes : {
"user@email" : {
"com" : {
//data from req.body
}
}
}
user@email
and com
is interpreted as string, but not the dot
in between them. How do I use it as complete string?