MongoDB Shell - access collection with period in name?

后端 未结 4 1385
醉话见心
醉话见心 2020-12-30 20:43

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,

4条回答
  •  心在旅途
    2020-12-30 20:43

    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

提交回复
热议问题