[SERVER-445] Can't reference collection names beginning with an underscore in the mongo shell Created: 29/Nov/09  Updated: 16/Apr/21  Resolved: 22/May/18

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 1.1.3
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Tomasz Ratajczak Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Done Votes: 5
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

linux


Issue Links:
Duplicate
is duplicated by SERVER-42346 Cannot drop collection with character... Closed
is duplicated by SERVER-56055 "indexOf is not a function" error for... Closed
Related
Participants:

 Description   

1. according to the MongoDB FAQ, collection names can begin with an underscore, but in mongo shell it results in

MongoDB shell version: 1.1.3
> db._a.insert({a: 1});
Sun Nov 29 14:19:16 JS Error: TypeError: db._a has no properties (shell):0
> db._a.findOne();
Sun Nov 29 14:19:36 JS Error: TypeError: db._a has no properties (shell):0
 
while
> db.a.insert({a: 1});
> db.a.findOne();
{ "_id" : ObjectId("4b1274ce2da8fd0af74f3a1c"), "a" : 1 }

2. When inserting document with PHP driver it works

3. I also tried mongodb-linux-i686-2009-11-29 nightly with the same result



 Comments   
Comment by Matt Lord (Inactive) [ 22/May/18 ]

Closing this for now since the described behavior is intentional and documented. If I missed or misunderstood something, please let me know. 

Thank you!

Comment by Asya Kamsky [ 22/May/18 ]

matt.lord  is there a reason not to close this ticket (given this is WAD and there is a docs note about it)?

 

Comment by Asya Kamsky [ 27/Dec/17 ]

There is a note in the documentation about this:

https://docs.mongodb.com/manual/reference/limits/index.html#Restriction-on-Collection-Names

If your collection name includes special characters, such as the underscore character, or begins with numbers, then to access the collection use the db.getCollection() method in the mongo shell or a similar method for your driver.

Comment by Asya Kamsky [ 30/Oct/16 ]

db.getCollection("_foo").find()
//works
db.getCollection("_foo").drop()
//works

Should this ticket be changed to documentation ticket?

Comment by Shawn Welch [ 04/Mar/14 ]

Still not working. Why not?

Yo: http://docs.mongodb.org/manual/reference/limits/#Restriction on Collection Names

Wudupwidit? Now I can't even drop the stupid collection.

Comment by MattyG [ 06/Sep/13 ]

This bug is still present. Can anyone tell me why the leading underscore semantic is special? When/where is is used etc?

Comment by auto [ 05/Oct/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: - SERVER-4025: segv in V8 when certain special objects (e.g. DBCollection) are converted from JS to BSON

Comment by Eliot Horowitz (Inactive) [ 27/Sep/11 ]

Not sure - this is why its in planned bucket b - shouldn't work on right now

Comment by Antoine Girbal [ 26/Sep/11 ]

what behavior do you want?
we can make it look for variable and if none exist then consider it a collection.

Comment by Eliot Horowitz (Inactive) [ 26/Sep/11 ]

We can do something better

Comment by Antoine Girbal [ 16/Sep/11 ]

yes there is a piece of code in the interceptor, that will only look at real JS properties if the property name starts with "_".
It seems that the motivation is to never be able to override a collection name with an arbitrary variable.
PRIMARY> db.a.find()
PRIMARY> db.a = "blah"
blah
PRIMARY> db.a
test.a
PRIMARY> db.a.find()

But with "_" it is possible to set properties:
PRIMARY> db._a = "blah"
blah
PRIMARY> db._a
blah

fixing doc on this in wiki.

Comment by Eliot Horowitz (Inactive) [ 17/Mar/11 ]

we should provide a warning or something, not sure exactly what - but the behavior shouldn't change in 2.0, just docs, warnings, etc.

Comment by dan [ 29/May/10 ]

This could be a documentation error, perhaps? This is mentioned on the docs at http://www.mongodb.org/display/DOCS/Collections

Comment by Eliot Horowitz (Inactive) [ 15/Mar/10 ]

Right - sorry.
db.getCollection( "_a" ) should work

Comment by Aaron Wieland [ 15/Mar/10 ]

Even db["_a"] doesn't work for me. For example, db["_Feeds"].count() yields the error, "JS Error: TypeError: db._Feeds has no properties (shell):0". I'm using version 1.1.4-.

Comment by Eliot Horowitz (Inactive) [ 12/Mar/10 ]

this is a problem just with the shell
you can do db["_a"]
though this is less than ideal

Generated at Thu Feb 08 02:54:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.