Пример #1
0
 bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
     
     string dbName = getDBName( ns );
     string collection = cmdObj.firstElement().valuestrsafe();
     string fullns = dbName + "." + collection;
     
     BSONObj filter = cmdObj["query"].embeddedObject();
     
     DBConfig * conf = grid.getDBConfig( dbName , false );
     
     if ( ! conf || ! conf->isShardingEnabled() || ! conf->isSharded( fullns ) ){
         ScopedDbConnection conn( conf->getPrimary() );
         result.append( "n" , (double)conn->count( fullns , filter ) );
         conn.done();
         result.append( "ok" , 1 );
         return true;
     }
     
     ChunkManager * cm = conf->getChunkManager( fullns );
     massert( "how could chunk manager be null!" , cm );
     
     vector<Chunk*> chunks;
     cm->getChunksForQuery( chunks , filter );
     
     unsigned long long total = 0;
     for ( vector<Chunk*>::iterator i = chunks.begin() ; i != chunks.end() ; i++ ){
         Chunk * c = *i;
         total += c->countObjects();
     }
     
     result.append( "n" , (double)total );
     result.append( "ok" , 1 );
     return true;
 }