333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
 
const Aspect = require('./operation').Aspect;
const CommandOperation = require('./command');
const defineAspects = require('./operation').defineAspects;
const handleCallback = require('../utils').handleCallback;
const MongoError = require('../core').MongoError;
const parseIndexOptions = require('../utils').parseIndexOptions;
 
const keysToOmit = new Set([
  'name',
  'key',
  'writeConcern',
  'w',
  'wtimeout',
  'j',
  'fsync',
  'readPreference',
  'session'
]);
 
class CreateIndexOperation extends CommandOperation {
  constructor(db, name, fieldOrSpec, options) {
    super(db, options);
 
    // Build the index
    const indexParameters = parseIndexOptions(fieldOrSpec);
    // Generate the index name
    const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
    // Set up the index
    const indexesObject = { name: indexName, key: indexParameters.fieldHash };
 
    this.name = name;
    this.fieldOrSpec = fieldOrSpec;
    this.indexes = indexesObject;
  }
 
  _buildCommand() {
    const options = this.options;
    const name = this.name;
    const indexes = this.indexes;
 
    // merge all the options
    for (let optionName in options) {
      if (!keysToOmit.has(optionName)) {
        indexes[optionName] = options[optionName];
      }
    }
 
    // Create command, apply write concern to command
    const cmd = { createIndexes: name, indexes: [indexes] };
 
    return cmd;
  }
 
  execute(callback) {
    const db = this.db;
    const options = this.options;
    const indexes = this.indexes;
 
    // Get capabilities
    const capabilities = db.s.topology.capabilities();
 
    // Did the user pass in a collation, check if our write server supports it
    if (options.collation && capabilities && !capabilities.commandsTakeCollation) {
      // Create a new error
      const error = new MongoError('server/primary/mongos does not support collation');
      error.code = 67;
      // Return the error
      return callback(error);
    }
 
    // Ensure we have a callback
    if (options.writeConcern && typeof callback !== 'function') {
      throw MongoError.create({
        message: 'Cannot use a writeConcern without a provided callback',
        driver: true
      });
    }
 
    // Attempt to run using createIndexes command
    super.execute((err, result) => {
      if (err == null) return handleCallback(callback, err, indexes.name);
 
      return handleCallback(callback, err, result);
    });
  }
}
 
defineAspects(CreateIndexOperation, Aspect.WRITE_OPERATION);
 
module.exports = CreateIndexOperation;