I want to name mongo collections after email addresses, how can I do this? is there a way to escape . and @ ?
-
You need to explain yourself more thoroughly than this. We have no idea what you mean. – Neil Lunn Jun 17 '14 at 03:29
-
I think he means mongo collections. – Martin Konecny Jun 17 '14 at 03:29
-
yes I meant collections my bad – user2661048 Jun 17 '14 at 03:34
3 Answers
There is nothing wrong with naming a collection like this. There is a section within the documentation on collection names under the general title of limitations:
Basically these:
In version 2.2, collection names cannot:
- contain the $.
- be an empty string (i.e. "").
So you seem to be missing the real operators other than the "shorthand" variable notations available under the mongoDb shell:
db.createCollection("this@my.com")
db.getCollection("this@my.com").insert({ a: 1 })
db.getCollection("this@my.com").find()
All of those methods are available to drivers as well.
Not sure what the utility of this is, it would seem better to just use the "email" as a key within your documents than create separate collections for these.

- 148,042
- 36
- 346
- 317
You can use the following command
db.runCommand({"create":"a@b.com"});
This will create the collection with @ in its name, you'll get following output indicating collection creation.
{ "ok" : 1 }

- 1,096
- 11
- 12
As others have mentioned '@' is valid, it is not noted in the collection name restrictions.
Some other considerations that you might want to make however:
The maximum length of a MongoDB collection is 120 bytes (I think it ends up being 116 characters). The maximum length of an email address is 254 characters.
There is a limit on the maximum number of collections in MongoDB which is based on the size of your namespace file. Using defaults you are essentially limited to 24,000 collections + indexes. See MongoDB Limits and Thesholds and also this question [limits of number of collections in databases. Given this limit, even if you were to raise it to the maximum using a larger namespace file, you should consider if it's possible that you might exceed this limit in your particular application
Querying across collections is not easy, so make sure that you don't have any use cases that might require this
Assuming that you may be building some sort of multi-tenant application, there are a number of resources out there that weigh the various options. Check out Building Multi-tenant applications on MongoDB.