rissicay
rissicay

Reputation: 405

Variable naming in the interactive shell, mongodb

I have a web app, that accepts parameters that then go on to create a collection in mongodb. I'm currently doing some testing and using the interactive shell to find if the read and writes are correct. If I use a some character everything works correctly. When I use something with a - or numbers, I get an error. eg:

> db.getCollectionNames();
[ "1", "2", "deviceslist", "system.indexes" ]
> db.1.find()
Sun Oct  9 22:58:22 SyntaxError: missing ; before statement (shell):1
>

Is there something I am missing or does mongodb just dont accept these things??

I've tried combinations of

db.'1'.find() db."1".find()

and none seem to work.

Help please..

Upvotes: 1

Views: 1105

Answers (2)

Tad Marshall
Tad Marshall

Reputation: 1363

There is nothing fundamentally wrong with giving a collection a numeric name except that JavaScript parsing will require you to use the bracket notation as harald's answer suggested.

db.one.find() // no issues

db['1'].find() // can't type db.1.find(), JavaScript parsing won't accept it

Upvotes: 0

aurora
aurora

Reputation: 9627

You should be able to access the collection in the following way:

db[1].find(); 

or

db['1'].find();

but i don't know, if there are any negative side-effects when naming a collection like so.

Upvotes: 4

Related Questions