8/15/2014

MongoDB escape special characters in a collection

Regarding how to escape dots in map key, please refer to below page:

MongoDB-Escape dots '.' in map key


If we have a collection named "test-core". then we do the operation using script directly, like:
db.test-core.ensureIndex( { userid: 1 } )
The server will throw exception: ReferenceError: core is not defined

This is because the character '-' inside the collection name is not correctly read.

The way to resolve is : 
db["test-core"].ensureIndex( { userid: 1 } )