-1

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?

JJ.
  • 112
  • 2
  • 10
  • Hello Jazib, MongoDB doesn't support dot in the field object. https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names – Maxi Schvindt May 30 '19 at 03:56
  • But exist some npm packets that can help you to convert '.' to another character to save in mongoDB – Maxi Schvindt May 30 '19 at 04:01
  • Hey @Cuchu, maybe you're right but I don't want "dot". I want string as key. I just want it to read the dot as part of a string. – JJ. May 30 '19 at 04:09
  • `"Until support is added in the query language, the use of $ and . in field names is not recommended and is not supported by the official MongoDB drivers."` You can't do that at the moment. – Herohtar May 30 '19 at 04:17
  • @Herohtar but I can use anything as long as it is in a string, right? – JJ. May 30 '19 at 11:14
  • Possible duplicate of [MongoDB dot (.) in key name](https://stackoverflow.com/questions/12397118/mongodb-dot-in-key-name) – Tejashwi Kalp Taru May 30 '19 at 11:56

1 Answers1

0

The Mongo docs suggests replacing illegal characters such as $ and . with their Unicode equivalents.

For example:

In these situations, keys will need to substitute the reserved $ and . characters. Any character is sufficient, but consider using the Unicode full-width equivalents: U+FF04 (i.e. “$”) and U+FF0E (i.e. “.”).

Original answer and related discussion can be seen here

Tejashwi Kalp Taru
  • 2,994
  • 2
  • 20
  • 35
  • So do I have to escape the `.` like `user@mail\uff0ecom` in my string? Will that cause any difficulties when updating or querying? – JJ. May 30 '19 at 12:38