28

I named a collection with an underscore in Mongo, and can't access it from the shell:

meteor:PRIMARY> show collections
_assignments
chatmessages
(... other stuff)

Trying to run any function on the first collection results in an error:

meteor:PRIMARY> db._assignments.find()
Thu Jun 19 10:53:28.450 TypeError: Cannot call method 'find' of undefined

However, other collections work fine:

meteor:PRIMARY> db.chatmessages.find()
{ "room" : "j5oau9DJ6GNpT9nR8", "userId" : "at9Kt8NNL4aeof6LE", "text" : "@nomad943 can you take a look at event #1?", "timestamp" : 1391806611977, "_id" : "26GbXa6c4B65FYRxC" }
{ "room" : "T7JfjBhri48bNHAfQ", "userId" : "B82LxmPBZWDnN4N2p", "text" : "Thinking #60 should be deleted, it's a duplicate of #36 and the region is wrong for the province", "timestamp" : ISODate("2014-06-18T18:57:56.480Z"), "_id" : "29pKqPhi4hgxCb2Ky" }

What's going on here?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Andrew Mao
  • 35,740
  • 23
  • 143
  • 224

4 Answers4

51

I don't see the problem other than the shell cannot address this with it's default helper syntax. You can still access collections named like this in the shell:

db.createCollection("_assignments")
{ "ok" : 1 }
db.getCollection("_assignments").find()
db.getCollection("_assignments").insert({ "a": 1 })
WriteResult({ "nInserted" : 1 })
db.getCollection("_assignments").find({ "a": 1 })
{ "_id" : ObjectId("53a36a4047234c4e9bb4feac"), "a" : 1 }
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • It's the shorthand notations that don't work, for example: `db._assignments` or `db["_assignments"]` don't work, however db.getCollection("_assignments") works. – Octo Poulos Mar 02 '23 at 11:05
4

This is a known bug. Don't prefix collections with underscores.

https://jira.mongodb.org/browse/SERVER-445

Brad M
  • 7,857
  • 1
  • 23
  • 40
  • I am not even using that collection, I'd just like to drop it. Possible? – Andrew Mao Jun 19 '14 at 15:04
  • 1
    Use a tool like robomongo. I just tried your scenario and the mongo collection prefixed with _ works fine in both meteor and robomongo. Only way I can't reach it is via the mongo console – Marco de Jongh Jun 19 '14 at 15:05
  • @marcodejongh You can user these collections in the mongo shell, just not directly with the helpers. Just use the correct method instead. – Neil Lunn Jun 20 '14 at 00:31
3

When a collection name starts with an underscore, it must be enclosed within double quotes. It transpires that (at least in MongoDB version 4.x or below) underscore (_ as first character) doesn't get interpreted correctly, hence you have to enclose it in quotes (i.e. specify as plain string), for example following doesn't work:

db._someCollection.find()

but following works fine:

db.getCollection("_someCollection").find();

HTH

Eddie Kumar
  • 1,216
  • 18
  • 20
2

If your collection name contains underscore and you want to insert a new document you have to assign the collection to a variable and use it for the insertion like the following:

var assignments = db.getCollection("_assignments")

assignments.insertOne(
   {
      room : "123",
      userId : "1",
      text : "text"
   }
)

after the documentation.

tsveti_iko
  • 6,834
  • 3
  • 47
  • 39