mongosh 0.9.0

A high-performance MongoDB Shell implementation in Rust
Documentation
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Command type definitions for mongosh
//!
//! This module defines all command types that can be parsed and executed,
//! including queries, administrative commands, utilities, and scripts.

use mongodb::bson::Document;
use serde::{Deserialize, Serialize};

use crate::error::ParseError;

/// Query execution mode
///
/// Determines how query results are returned and processed.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum QueryMode {
    /// Interactive mode with pagination
    ///
    /// Returns results in batches and saves cursor state for pagination.
    /// Used for normal interactive queries in the shell.
    Interactive { batch_size: u32 },

    /// Streaming mode for export
    ///
    /// Returns a streaming interface for processing all results.
    /// Used for export operations to avoid loading all data into memory.
    Streaming { batch_size: u32 },
}

impl Default for QueryMode {
    fn default() -> Self {
        QueryMode::Interactive { batch_size: 20 }
    }
}

/// Represents a parsed command
#[derive(Debug, Clone, PartialEq)]
pub enum Command {
    /// Database query command (CRUD operations)
    Query(QueryCommand),

    /// Administrative command (show, use, create, drop, etc.)
    Admin(AdminCommand),

    /// Utility command (print, serverStatus, etc.)
    Utility(UtilityCommand),

    /// Configuration command (set format, color, etc.)
    Config(ConfigCommand),

    /// Piped command (query with post-processing)
    Pipe(Box<Command>, PipeCommand),

    /// Help command with optional topic
    Help(Option<String>),

    /// Exit/quit command
    Exit,
}

/// Query-related commands (CRUD operations)
#[derive(Debug, Clone, PartialEq)]
pub enum QueryCommand {
    /// Find documents matching a filter
    Find {
        collection: String,
        filter: Document,
        options: FindOptions,
    },

    /// Find one document matching a filter
    FindOne {
        collection: String,
        filter: Document,
        options: FindOptions,
    },

    /// Insert a single document
    InsertOne {
        collection: String,
        document: Document,
    },

    /// Insert multiple documents
    InsertMany {
        collection: String,
        documents: Vec<Document>,
    },

    /// Update one document
    UpdateOne {
        collection: String,
        filter: Document,
        update: Document,
        options: UpdateOptions,
    },

    /// Update multiple documents
    UpdateMany {
        collection: String,
        filter: Document,
        update: Document,
        options: UpdateOptions,
    },

    /// Replace one document
    ReplaceOne {
        collection: String,
        filter: Document,
        replacement: Document,
        options: UpdateOptions,
    },

    /// Delete one document
    DeleteOne {
        collection: String,
        filter: Document,
    },

    /// Delete multiple documents
    DeleteMany {
        collection: String,
        filter: Document,
    },

    /// Run an aggregation pipeline
    Aggregate {
        collection: String,
        pipeline: Vec<Document>,
        options: AggregateOptions,
    },

    /// Count documents matching a filter
    CountDocuments {
        collection: String,
        filter: Document,
    },

    /// Estimate document count (fast but approximate)
    EstimatedDocumentCount { collection: String },

    /// Find one document and delete it
    FindOneAndDelete {
        collection: String,
        filter: Document,
        options: FindAndModifyOptions,
    },

    /// Find one document and update it
    FindOneAndUpdate {
        collection: String,
        filter: Document,
        update: Document,
        options: FindAndModifyOptions,
    },

    /// Find one document and replace it
    FindOneAndReplace {
        collection: String,
        filter: Document,
        replacement: Document,
        options: FindAndModifyOptions,
    },

    /// Find and modify a document (unified interface)
    FindAndModify {
        collection: String,
        query: Document,
        sort: Option<Document>,
        remove: bool,
        update: Option<Document>,
        new: bool,
        fields: Option<Document>,
        upsert: bool,
        array_filters: Option<Vec<Document>>,
        max_time_ms: Option<u64>,
        collation: Option<Document>,
    },

    /// Create a distinct query
    Distinct {
        collection: String,
        field: String,
        filter: Option<Document>,
    },

    /// Bulk write operations
    BulkWrite {
        collection: String,
        operations: Vec<Document>,
        ordered: bool,
    },

    /// Explain query execution plan
    Explain {
        collection: String,
        verbosity: ExplainVerbosity,
        query: Box<QueryCommand>,
    },
}

impl QueryCommand {
    /// Get the collection name for this query command
    pub fn collection(&self) -> &str {
        match self {
            QueryCommand::Find { collection, .. }
            | QueryCommand::FindOne { collection, .. }
            | QueryCommand::InsertOne { collection, .. }
            | QueryCommand::InsertMany { collection, .. }
            | QueryCommand::UpdateOne { collection, .. }
            | QueryCommand::UpdateMany { collection, .. }
            | QueryCommand::ReplaceOne { collection, .. }
            | QueryCommand::DeleteOne { collection, .. }
            | QueryCommand::DeleteMany { collection, .. }
            | QueryCommand::Aggregate { collection, .. }
            | QueryCommand::CountDocuments { collection, .. }
            | QueryCommand::EstimatedDocumentCount { collection }
            | QueryCommand::FindOneAndDelete { collection, .. }
            | QueryCommand::FindOneAndUpdate { collection, .. }
            | QueryCommand::FindOneAndReplace { collection, .. }
            | QueryCommand::FindAndModify { collection, .. }
            | QueryCommand::Distinct { collection, .. }
            | QueryCommand::BulkWrite { collection, .. }
            | QueryCommand::Explain { collection, .. } => collection,
        }
    }

    /// Check if this query command supports explain
    pub fn supports_explain(&self) -> bool {
        matches!(
            self,
            QueryCommand::Find { .. }
                | QueryCommand::FindOne { .. }
                | QueryCommand::Aggregate { .. }
                | QueryCommand::CountDocuments { .. }
                | QueryCommand::Distinct { .. }
        )
    }
}

/// Explain verbosity modes
#[derive(Debug, Clone, PartialEq)]
pub enum ExplainVerbosity {
    /// Query planner information only (default)
    QueryPlanner,
    /// Query planner and execution statistics
    ExecutionStats,
    /// All plans execution information
    AllPlansExecution,
}

impl ExplainVerbosity {
    /// Verbosity mode constants
    pub const QUERY_PLANNER: &'static str = "queryPlanner";
    pub const EXECUTION_STATS: &'static str = "executionStats";
    pub const ALL_PLANS_EXECUTION: &'static str = "allPlansExecution";

    /// Backwards compatibility constants
    pub const COMPAT_TRUE: &'static str = "true";
    pub const COMPAT_FALSE: &'static str = "false";

    /// Parse verbosity from string
    pub fn from_str(s: &str) -> Result<Self, ParseError> {
        match s {
            Self::QUERY_PLANNER => Ok(ExplainVerbosity::QueryPlanner),
            Self::EXECUTION_STATS => Ok(ExplainVerbosity::ExecutionStats),
            Self::ALL_PLANS_EXECUTION => Ok(ExplainVerbosity::AllPlansExecution),
            Self::COMPAT_TRUE => Ok(ExplainVerbosity::AllPlansExecution), // backwards compatibility
            Self::COMPAT_FALSE => Ok(ExplainVerbosity::QueryPlanner), // backwards compatibility
            _ => Err(ParseError::InvalidCommand(format!(
                "Invalid explain verbosity: '{}'. Valid options are: '{}', '{}', '{}' (or boolean true/false for compatibility)",
                s, Self::QUERY_PLANNER, Self::EXECUTION_STATS, Self::ALL_PLANS_EXECUTION
            ))),
        }
    }

    /// Parse verbosity from boolean (backwards compatibility)
    pub fn from_bool(value: bool) -> Self {
        if value {
            ExplainVerbosity::AllPlansExecution
        } else {
            ExplainVerbosity::QueryPlanner
        }
    }

    /// Convert to MongoDB command string
    pub fn as_str(&self) -> &'static str {
        match self {
            ExplainVerbosity::QueryPlanner => Self::QUERY_PLANNER,
            ExplainVerbosity::ExecutionStats => Self::EXECUTION_STATS,
            ExplainVerbosity::AllPlansExecution => Self::ALL_PLANS_EXECUTION,
        }
    }

    /// Validate explain arguments and parse verbosity
    pub fn parse_from_args(args: &[crate::parser::mongo_ast::Expr]) -> Result<Self, ParseError> {
        use crate::parser::mongo_ast::Expr;

        if args.is_empty() {
            return Ok(Self::default());
        }

        if args.len() > 1 {
            return Err(ParseError::InvalidCommand(
                format!("explain() expects at most 1 argument, got {}", args.len())
            ));
        }

        match args.first().unwrap() {
            Expr::String(verb_str) => Self::from_str(verb_str),
            Expr::Boolean(b) => Ok(Self::from_bool(*b)),
            _ => Err(ParseError::InvalidCommand(
                "explain() expects a string verbosity ('queryPlanner', 'executionStats', 'allPlansExecution') or boolean".to_string()
            )),
        }
    }
}

impl Default for ExplainVerbosity {
    fn default() -> Self {
        ExplainVerbosity::QueryPlanner
    }
}

/// Administrative commands
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum AdminCommand {
    /// Show all databases
    ShowDatabases,

    /// Show collections in current database
    ShowCollections,

    /// Show users in current database
    ShowUsers,

    /// Show roles in current database
    ShowRoles,

    /// Show database profile information
    ShowProfile,

    /// Show logs
    ShowLogs(Option<String>),

    /// Switch to a database
    UseDatabase(String),

    /// Create an index
    CreateIndex {
        collection: String,
        keys: Document,
        options: Option<Document>,
    },

    /// Create multiple indexes
    CreateIndexes {
        collection: String,
        indexes: Vec<Document>,
    },

    /// List indexes on a collection
    ListIndexes(String),

    /// Drop a single index from a collection
    DropIndex { collection: String, index: String },

    /// Drop multiple indexes from a collection
    DropIndexes {
        collection: String,
        indexes: Option<Vec<String>>,
    },

    /// Drop a collection
    DropCollection(String),

    /// Rename a collection
    RenameCollection {
        collection: String,
        target: String,
        drop_target: bool,
    },

    /// Get collection statistics
    CollectionStats {
        collection: String,
        scale: Option<i32>,
    },
}

/// Pipe commands for post-processing query results
#[derive(Debug, Clone, PartialEq)]
pub enum PipeCommand {
    /// Export results to a file
    Export {
        format: ExportFormat,
        file: Option<String>,
    },

    /// Explain query execution plan
    Explain,
}

/// Export format types
#[derive(Debug, Clone, PartialEq)]
pub enum ExportFormat {
    /// JSON Lines format (one JSON object per line)
    JsonL,
    /// CSV format
    Csv,
}

/// Utility commands
#[derive(Debug, Clone, PartialEq)]
/// Utility commands for shell operations
pub enum UtilityCommand {
    /// Print/echo a value
    #[allow(dead_code)]
    Print(String),

    /// Iterate through more results (it command)
    Iterate,
}

/// Configuration commands for runtime settings
#[derive(Debug, Clone, PartialEq)]
pub enum ConfigCommand {
    /// Set output format (shell, json, json-pretty, table, compact)
    SetFormat(String),

    /// Get current format
    GetFormat,

    /// Enable/disable colors
    SetColor(bool),

    /// Get current color setting
    GetColor,

    /// Show all current settings
    ShowConfig,

    /// List all named queries
    ListNamedQueries,

    /// Execute a named query with arguments
    ExecuteNamedQuery { name: String, args: Vec<String> },

    /// Save a named query
    SaveNamedQuery { name: String, query: String },

    /// Delete a named query
    DeleteNamedQuery(String),
}

/// Options for find operations
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct FindOptions {
    /// Maximum number of documents to return
    pub limit: Option<i64>,

    /// Number of documents to skip
    pub skip: Option<u64>,

    /// Sort specification
    pub sort: Option<Document>,

    /// Projection specification (fields to include/exclude)
    pub projection: Option<Document>,

    /// Batch size for cursor
    pub batch_size: Option<u32>,

    /// Enable collation
    pub collation: Option<Document>,

    /// Hint for index to use
    pub hint: Option<Document>,

    /// Maximum time in milliseconds
    pub max_time_ms: Option<u64>,

    /// Read concern level
    pub read_concern: Option<Document>,
}

/// Options for update operations
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct UpdateOptions {
    /// Create document if not found
    pub upsert: bool,

    /// Array filters for positional updates
    pub array_filters: Option<Vec<Document>>,

    /// Collation
    pub collation: Option<Document>,

    /// Hint for index to use
    pub hint: Option<Document>,

    /// Write concern
    pub write_concern: Option<Document>,
}

/// Options for aggregate operations
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct AggregateOptions {
    /// Allow writing to temporary files for large aggregations
    pub allow_disk_use: bool,

    /// Batch size for cursor
    pub batch_size: Option<u32>,

    /// Maximum time in milliseconds
    pub max_time_ms: Option<u64>,

    /// Collation
    pub collation: Option<Document>,

    /// Hint for index to use
    pub hint: Option<Document>,

    /// Read concern level
    pub read_concern: Option<Document>,

    /// Let variables for aggregation expressions
    pub let_vars: Option<Document>,
}

/// Options for findAndModify operations
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct FindAndModifyOptions {
    /// Return the modified document instead of the original
    pub return_new: bool,

    /// Create document if not found
    pub upsert: bool,

    /// Sort specification
    pub sort: Option<Document>,

    /// Projection specification
    pub projection: Option<Document>,

    /// Collation
    pub collation: Option<Document>,

    /// Array filters
    pub array_filters: Option<Vec<Document>>,

    /// Maximum time in milliseconds
    pub max_time_ms: Option<u64>,

    /// Hint for index to use
    pub hint: Option<Document>,
}