Mongo客户端无法访问前缀为下划线的集合

时间:2014-06-19 14:56:05

标签: javascript mongodb meteor

我在Mongo中使用下划线命名了一个集合,并且无法从shell访问它:

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

尝试在第一个集合上运行任何函数会导致错误:

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

但是,其他收藏品工作正常:

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" }

这里发生了什么?

3 个答案:

答案 0 :(得分:41)

我没有看到除了shell之外的问题,使用它的默认助手语法来解决这个问题。您仍然可以在shell中访问名为this的集合:

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 }

答案 1 :(得分:2)

这是一个已知的错误。不要使用下划线为集合添加前缀。

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

答案 2 :(得分:0)

当集合名称以下划线开头时,必须将其括在双引号中。它说明(至少在MongoDB 4.x或更低版本中)下划线(_作为第一个字符)无法正确解释,因此您必须将其括在引号中(即,指定为纯字符串),例如,以下字符不能工作:

  const QuestionPreview = ({
    text, labels, selectedBlanks, readOnly,
        }) => {
    const readOnlyContent = (id) => {
    const droppedBlank = selectedBlanks && _.find(selectedBlanks, blank => 
                                       blank.textIndex === id);
    const label = droppedBlank && _.find(labels, l => l.id === 
                                       droppedBlank.id);
    return (
      <span className={droppedBlank ? styles.dropped : styles.placeholder}>
       {droppedBlank && <BlankItem label={label} readOnly />}
     </span>
    );
 };

  const splittedText = splitTextWithBlanks(text);
  const blankIndices = getBlankIndices(text);
  const getContentId = index => blankIndices[index];

  const tempArray = [];

  const html = () => {
   return _.map(splittedText, (element, index) => {
     const contentId = getContentId(index);
     const droppedBlank = selectedBlanks && _.find(selectedBlanks, blank => 
                                          blank.textIndex === contentId);
     const label = droppedBlank && _.find(labels, l => l.id === 
                                                        droppedBlank.id);
      const blankContent = readOnly ? readOnlyContent(contentId) : 
      <DroppableContent id={contentId} droppedBlank={droppedBlank} label= 
      {label} 
      />;
       let htmlContent = <span dangerouslySetInnerHTML={{ __html: 
                                                 toHTML(element) }} />;
    if (index !== splittedText.length - 1) {
        return (
         <Fragment>
           {htmlContent}
           {blankContent}
         </Fragment>
        ) 
     }

    return htmlContent
   });

  };

 return (
   {html()}
 )
};

但以下方法可以正常工作:

db._someCollection.find()

HTH