thesonix
thesonix

Reputation: 3260

MongoDB escape special characters in db-name

does anyone know how I can use a database-name like "abc-123" from the mongodb console doing something like this in one command:

 use abc-123;
 db.Configuration.find()

I tried somethings like

 ['abc-123'].Configuration.find()

but nothing worked.

Thx for any help!

Upvotes: 2

Views: 2706

Answers (1)

Mark Hillick
Mark Hillick

Reputation: 6973

You can use db.getSibling() to do this -

> db.getSiblingDB("abc-123").Configuration.find()

{ "_id" : ObjectId("4fd20ac548b3c185dd9f8e5c"), "name" : "mongo" }
{ "_id" : ObjectId("4fd20ac748b3c185dd9f8e5d"), "x" : 3 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e5e"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e5f"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e60"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e61"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e62"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e63"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e64"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e65"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e66"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4fd20b9648b3c185dd9f8e67"), "x" : 4, "j" : 10 }
......
......

Here's a link to the command refererence list for MongoDB where it talks about getSibling a little more - http://docs.mongodb.org/manual/reference/commands/.

Additionally, it's recommended to keep the db name alphanumeric.

Upvotes: 1

Related Questions