zoukankan      html  css  js  c++  java
  • Mongodb分片问题之please create an index that starts with the shard key before sharding

    Mongodb版本2.2.1

    场景:

    mongo数据库test分片已经启用,在新增加一个collection之后,并且该表已经有数据写入,当对该表启用分片的时候报错:please create an index that starts with the shard key before sharding

    大概意思是在启用分片前需要在分片key上建立索引。我并没有立刻在分片key上建立索引,因为之前在启用表分片,在分片key上会自动建立索引,为什么要我手动建立呢?所以先google之看有没有相关资料,没想到搜索的结果大失所望,总共才5条结果。2条英文结果,3条日文。第一条好像是mongodb源码,当时没注意。没办法只能看下根据提示关键字搜索下mongodb源码,在src/mongo/s/commands_admin.cpp文件中544行找到这个错误信息

    虽然C++在大学时只学了皮毛,看看判断还是可以的....原来判断分支是查找是否有可用的索引存在,当无可用的索引,并且表不为空时,就会出现这个错误信息。

    好了找到根源了,现在解决问题:在分片key上个建立索引

    use test
    db.ttl.ensureIndex({date:1,node_id:1})

    然后对表进行分片

    sh.shardCollection('test.ttl',{date:1,node_id:1})

    结果显示:

    { "collectionsharded" : "test.ttl", "ok" : 1 }

    OK.至此问题解决.

  • 相关阅读:
    XML语法
    C/C++对MySQL操作
    HDU 3966 Aragorn's Story
    SPOJ 375 Query on a tree
    SPOJ 913 Query on a tree II
    SPOJ 6779 Can you answer these queries VII
    URAL 1471 Tree
    SPOJ 2798 Query on a tree again!
    POJ 3237 Tree
    SPOJ 4487 Can you answer these queries VI
  • 原文地址:https://www.cnblogs.com/lazyboy/p/2789401.html
Copyright © 2011-2022 走看看