2012-10-24 58 views
2

有没有办法使用MongoDb(2.2)C++驱动程序创建sparse索引?MongoDb使用C++驱动程序创建稀疏索引

看来ensureIndex函数不接受这个参数。从MongoDb docs

bool mongo::DBClientWithCommands::ensureIndex( 
          const string & ns, 
          BSONObj keys, 
          bool unique = false, 
          const string & name = "", 
          bool cache = true, 
          bool background = false, 
          int v = -1) 

回答

0

我结束了修补蒙戈源代码(mongo/cleint/dbclient.cpp):

bool DBClientWithCommands::ensureIndex(const string &ns , BSONObj keys , bool unique, const string & name , bool cache, bool background, int version, bool sparse) { 
    BSONObjBuilder toSave; 
    toSave.append("ns" , ns); 
    toSave.append("key" , keys); 

    string cacheKey(ns); 
    cacheKey += "--"; 

    if (name != "") { 
     toSave.append("name" , name); 
     cacheKey += name; 
    } 
    else { 
     string nn = genIndexName(keys); 
     toSave.append("name" , nn); 
     cacheKey += nn; 
    } 

    if(version >= 0) 
     toSave.append("v", version); 

    if (unique) 
     toSave.appendBool("unique", unique); 

    if (sparse) 
     toSave.appendBool("sparse", true); 

    if(background) 
     toSave.appendBool("background", true); 

    if (_seenIndexes.count(cacheKey)) 
     return 0; 

    if (cache) 
     _seenIndexes.insert(cacheKey); 

    insert(Namespace(ns.c_str()).getSisterNS("system.indexes" ).c_str() , toSave.obj()); 
    return 1; 
} 

的问题应该是在版本2.3.2 resolved

2

对于这个问题,dropDups不是一个参数要么...

作为一种变通方法,你可以建立自己的服务器命令,附加sparse说法。如果您按照this link,您会注意到服务器命令由构建BSONObject组成,各种索引选项作为字段附加。编写您自己的版本ensureIndex应该很简单。

+0

这似乎有些施工过程中使用的变量都是私人的,在课堂之外不要修改。我最终修补了C++驱动程序代码。 – Xyand

相关问题