1

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..

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

> db.[1].find(); Sun Oct 9 23:25:40 SyntaxError: missing name after . operator (shell):1 > db.['1'].find(); Sun Oct 9 23:25:46 SyntaxError: missing name after . operator (shell):1 > db.["1"].find(); Sun Oct 9 23:25:55 SyntaxError: missing name after . operator (shell):1 >
still syntax error. The other option is to make it so the user can only name it a certain way, which is hard..
you have a dot in your notation. it is db[1] not db.[1]
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.