开发者

MongoDB Shell - access collection with period in name?

I have found a collection in one of our MongoDB databases with the name my.collection.

Is there a way to access this collection from the MongoDB shell, des开发者_如何学Cpite it having a point in the name?

> db.my.collection.findOne();
null

I'm pretty sure that that is not correct.


try this instead:

db["my.collection"].findOne();

you run into the same issue with hyphens or any other name that does not match on [a-zA-Z_$][0-9a-zA-Z_$]

This limitation comes from valid named for javascript object properties.


if collection name is "my.collection"

db.my.collection.findOne(); // OK
null

if collection name is "my.1.collection"

db.my.1.collection.findOne(); // Not OK
SyntaxError: missing ; before statement

Fix:

db["my.1.collection"].findOne(); // Now is OK
null


Another foolproof method is:

db.getCollection("_SCHEMA").find()

While in the case of a underscore in the name, still cause a error with @Laura answer:

> db["_SCHEMA"].find()
2016-07-18T17:44:16.948+0200 E QUERY    [thread1] TypeError: db._SCHEMA is undefined :
@(shell):1:1


Your code is correct. If it returns null it means that the collection is empty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜