src/mongo/db/s/shard_key_util.cpp
/**
 * Constructs the BSON specification document for the create indexes command using the given
 * namespace, index key and options.
 */
Constructs the BSON specification document for the create indexes command using the given
namespace, index key and options.
Constructs the BSON specification document for the create indexes command using the given namespace index key and options .
BSONObj makeCreateIndexesCmd(const NamespaceString& nss,
                             const BSONObj& keys,
                             const BSONObj& collation,
                             bool unique) {
    BSONObjBuilder index;
    index.append("key", keys);
    StringBuilder indexName;
    bool isFirstKey = true;
    for (BSONObjIterator keyIter(keys); keyIter.more();) {
        BSONElement currentKey = keyIter.next();
        if (isFirstKey) {
            isFirstKey = false;
        } else {
            indexName << "_";
        }
        indexName << currentKey.fieldName() << "_";
        if (currentKey.isNumber()) {
            indexName << currentKey.numberInt();
        } else {
            indexName << currentKey.str();  
        }
    }
    index.append("name", indexName.str());
    if (!collation.isEmpty()) {
        index.append("v", static_cast<int>(IndexDescriptor::IndexVersion::kV2));
        index.append("collation", collation);
    }
    if (unique && !IndexDescriptor::isIdIndexPattern(keys)) {
        index.appendBool("unique", unique);
    }
    BSONObjBuilder createIndexes;
    createIndexes.append("createIndexes", nss.coll());
    createIndexes.append("indexes", BSON_ARRAY(index.obj()));
    createIndexes.append("writeConcern", WriteConcernOptions::Majority);
    return createIndexes.obj();
}
BSONObj makeCreateIndexesCmd ( const NamespaceString & nss , const BSONObj & keys , const BSONObj & collation , bool unique ) { BSONObjBuilder index ; index . append ( " " , keys ) ; StringBuilder indexName ; bool isFirstKey = true ; for ( BSONObjIterator keyIter ( keys ) ; keyIter . more ( ) ; ) { BSONElement currentKey = keyIter . next ( ) ; if ( isFirstKey ) { isFirstKey = false ; } else { indexName << " " ; } indexName << currentKey . fieldName ( ) << " " ; if ( currentKey . isNumber ( ) ) { indexName << currentKey . numberInt ( ) ; } else { indexName << currentKey . str ( ) ; } } index . append ( " " , indexName . str ( ) ) ; if ( ! collation . isEmpty ( ) ) { index . append ( " " , static_cast < int > ( IndexDescriptor :: IndexVersion :: kV2 ) ) ; index . append ( " " , collation ) ; } if ( unique && ! IndexDescriptor :: isIdIndexPattern ( keys ) ) { index . appendBool ( " " , unique ) ; } BSONObjBuilder createIndexes ; createIndexes . append ( " " , nss . coll ( ) ) ; createIndexes . append ( " " , BSON_ARRAY ( index . obj ( ) ) ) ; createIndexes . append ( " " , WriteConcernOptions :: Majority ) ; return createIndexes . obj ( ) ; }
Constructs the BSON specification document for the create indexes command using the given
 namespace, index key and options.
Constructs the BSON specification document for the create indexes command using the given namespace index key and options .
// Required fields for an index. // This should match up with shell command. // Index options. // Creating an index with the "collation" option requires a v=2 index. // The outer createIndexes command.