@gagik.co/snippet-agent 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +13 -0
- package/.prettierrc.json +1 -0
- package/README.md +23 -0
- package/dist/agent-class.d.ts +47 -0
- package/dist/agent-class.js +314 -0
- package/dist/agent.d.ts +1 -0
- package/dist/agent.js +392 -0
- package/dist/banner.d.ts +1 -0
- package/dist/banner.js +23 -0
- package/dist/confirmation-extension.d.ts +10 -0
- package/dist/confirmation-extension.js +213 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +141 -0
- package/dist/mongosh-interactive-mode.d.ts +33 -0
- package/dist/mongosh-interactive-mode.js +244 -0
- package/dist/project-agent.d.ts +1 -0
- package/dist/project-agent.js +36 -0
- package/dist/shell-context.d.ts +17 -0
- package/dist/shell-context.js +75 -0
- package/dist/skills-loader.d.ts +2 -0
- package/dist/skills-loader.js +69 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +8 -0
- package/dist/src/project-agent.d.ts +1 -0
- package/dist/src/project-agent.js +36 -0
- package/dist/stdout-patcher.d.ts +5 -0
- package/dist/stdout-patcher.js +41 -0
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mongosh-eval.d.ts +7 -0
- package/dist/tools/mongosh-eval.js +84 -0
- package/dist/tools/search-docs.d.ts +2 -0
- package/dist/tools/search-docs.js +106 -0
- package/dist/tools/types.d.ts +12 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools.d.ts +7 -0
- package/dist/tools.js +189 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.js +2 -0
- package/package.json +38 -0
- package/skills/mongodb-connection.md +208 -0
- package/skills/mongodb-natural-language-querying.md +202 -0
- package/skills/mongodb-query-optimizer.md +265 -0
- package/skills/mongodb-schema-design.md +455 -0
- package/skills/mongodb-search-and-ai.md +357 -0
- package/skills/mongosh-shell.md +227 -0
- package/src/agent-class.ts +393 -0
- package/src/banner.ts +36 -0
- package/src/confirmation-extension.ts +297 -0
- package/src/index.ts +137 -0
- package/src/mongosh-interactive-mode.ts +420 -0
- package/src/shell-context.ts +97 -0
- package/src/skills-loader.ts +37 -0
- package/src/stdout-patcher.ts +48 -0
- package/src/tools/index.ts +4 -0
- package/src/tools/mongosh-eval.ts +115 -0
- package/src/tools/search-docs.ts +115 -0
- package/src/tools/types.ts +15 -0
- package/src/types.ts +23 -0
- package/tsconfig-lint.json +4 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mongodb-search-and-ai
|
|
3
|
+
description: Guide users through implementing and optimizing Atlas Search (full-text), Vector Search (semantic), and Hybrid Search solutions. Use this skill when users need to build search functionality for text-based queries (autocomplete, fuzzy matching, faceted search), semantic similarity (embeddings, RAG applications), or combined approaches. Also use when users need text containment, substring matching ('contains', 'includes', 'appears in'), case-insensitive or multi-field text search, or filtering across many fields with variable combinations.
|
|
4
|
+
disable-model-invocation: false
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# MongoDB Search and AI
|
|
8
|
+
|
|
9
|
+
You are helping MongoDB users implement, optimize, and troubleshoot Atlas Search (lexical), Vector Search (semantic), and Hybrid Search (combined) solutions.
|
|
10
|
+
|
|
11
|
+
**Important:** Atlas Search and Vector Search require MongoDB Atlas (cloud) and cannot be used with self-hosted MongoDB instances. These features use specialized Lucene-based indexes that are separate from regular MongoDB indexes.
|
|
12
|
+
|
|
13
|
+
## Core Principles
|
|
14
|
+
|
|
15
|
+
1. **Understand before building** - Validate the use case to ensure you recommend the right solution
|
|
16
|
+
2. **Always inspect first** - Check existing indexes and schema before making recommendations
|
|
17
|
+
3. **Explain before executing** - Describe what indexes will be created and require explicit approval
|
|
18
|
+
4. **Optimize for the use case** - Different use cases require different index configurations and query patterns
|
|
19
|
+
|
|
20
|
+
## Workflow
|
|
21
|
+
|
|
22
|
+
### 1. Discovery Phase
|
|
23
|
+
|
|
24
|
+
**Check the environment using mongosh_eval:**
|
|
25
|
+
- Use `db.getMongo().connectionInfo` to check if connected to Atlas
|
|
26
|
+
- Use `db.serverBuildInfo()` to check MongoDB version
|
|
27
|
+
- Use `show collections` and `db.collection.stats()` to understand data
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
// Check if running on Atlas
|
|
31
|
+
db.adminCommand({ atlasVersion: 1 })
|
|
32
|
+
|
|
33
|
+
// List collections
|
|
34
|
+
db.getCollectionNames()
|
|
35
|
+
|
|
36
|
+
// Inspect collection structure
|
|
37
|
+
db.collection.find().limit(3)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Understand the use case:**
|
|
41
|
+
If the user's request is vague, ask clarifying questions:
|
|
42
|
+
- What are users searching for? (products, movies, documents, etc.)
|
|
43
|
+
- What fields contain the searchable content?
|
|
44
|
+
- Do they need exact matching, fuzzy matching, or semantic similarity?
|
|
45
|
+
- Do they need filters (price ranges, categories, dates)?
|
|
46
|
+
- Do they need autocomplete/typeahead functionality?
|
|
47
|
+
|
|
48
|
+
### 2. Determine Search Type
|
|
49
|
+
|
|
50
|
+
**Atlas Search (Lexical/Full-Text):**
|
|
51
|
+
Use when users need:
|
|
52
|
+
- Keyword matching with relevance scoring
|
|
53
|
+
- Fuzzy matching for typo tolerance
|
|
54
|
+
- Autocomplete/typeahead
|
|
55
|
+
- Faceted search with filters
|
|
56
|
+
- Language-specific text analysis
|
|
57
|
+
- Token-based search
|
|
58
|
+
|
|
59
|
+
**Vector Search (Semantic):**
|
|
60
|
+
Use when users need:
|
|
61
|
+
- Semantic similarity ("find movies about coming of age stories")
|
|
62
|
+
- Natural language understanding
|
|
63
|
+
- RAG (Retrieval Augmented Generation) applications
|
|
64
|
+
- Finding conceptually similar items
|
|
65
|
+
- Cross-modal search
|
|
66
|
+
|
|
67
|
+
**Hybrid Search:**
|
|
68
|
+
Use when users need:
|
|
69
|
+
- Combining multiple search approaches (e.g., vector + lexical)
|
|
70
|
+
- Queries like "find action movies similar to 'epic space battles'" (combining keyword filtering with semantic similarity)
|
|
71
|
+
- Results that factor in multiple relevance criteria
|
|
72
|
+
|
|
73
|
+
### 3. Version Check
|
|
74
|
+
|
|
75
|
+
If using Hybrid Search with `$rankFusion` or `$scoreFusion`, verify the cluster version:
|
|
76
|
+
- `$rankFusion` requires MongoDB 8.0+
|
|
77
|
+
- `$scoreFusion` requires MongoDB 8.2+
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
// Check version
|
|
81
|
+
db.version()
|
|
82
|
+
// or
|
|
83
|
+
db.serverBuildInfo().version
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Atlas Search (Lexical Search)
|
|
87
|
+
|
|
88
|
+
### Creating Search Indexes
|
|
89
|
+
|
|
90
|
+
Atlas Search indexes are created through the Atlas UI or API, not through mongosh directly. However, you can use mongosh to query them once created.
|
|
91
|
+
|
|
92
|
+
**Index Configuration Examples:**
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
// Basic text index (via Atlas UI/API)
|
|
96
|
+
{
|
|
97
|
+
"mappings": {
|
|
98
|
+
"dynamic": false,
|
|
99
|
+
"fields": {
|
|
100
|
+
"title": { "type": "string", "analyzer": "standard" },
|
|
101
|
+
"description": { "type": "string", "analyzer": "standard" },
|
|
102
|
+
"category": { "type": "stringFacet" }
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Autocomplete index
|
|
108
|
+
{
|
|
109
|
+
"mappings": {
|
|
110
|
+
"fields": {
|
|
111
|
+
"title": {
|
|
112
|
+
"type": "autocomplete",
|
|
113
|
+
"tokenization": "edgeGram",
|
|
114
|
+
"minGrams": 2,
|
|
115
|
+
"maxGrams": 10
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Querying with $search
|
|
123
|
+
|
|
124
|
+
Once the index is created via Atlas, query using mongosh:
|
|
125
|
+
|
|
126
|
+
```javascript
|
|
127
|
+
// Basic text search
|
|
128
|
+
db.collection.aggregate([
|
|
129
|
+
{
|
|
130
|
+
$search: {
|
|
131
|
+
index: "default",
|
|
132
|
+
text: {
|
|
133
|
+
query: "mongodb performance",
|
|
134
|
+
path: ["title", "description"]
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
{ $limit: 10 }
|
|
139
|
+
])
|
|
140
|
+
|
|
141
|
+
// Search with fuzzy matching
|
|
142
|
+
db.collection.aggregate([
|
|
143
|
+
{
|
|
144
|
+
$search: {
|
|
145
|
+
index: "default",
|
|
146
|
+
text: {
|
|
147
|
+
query: "databse",
|
|
148
|
+
path: "title",
|
|
149
|
+
fuzzy: { maxEdits: 1, prefixLength: 3 }
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
])
|
|
154
|
+
|
|
155
|
+
// Autocomplete
|
|
156
|
+
db.collection.aggregate([
|
|
157
|
+
{
|
|
158
|
+
$search: {
|
|
159
|
+
index: "autocomplete_index",
|
|
160
|
+
autocomplete: {
|
|
161
|
+
query: "mong",
|
|
162
|
+
path: "title",
|
|
163
|
+
tokenOrder: "sequential"
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
{ $limit: 5 }
|
|
168
|
+
])
|
|
169
|
+
|
|
170
|
+
// Faceted search with filters
|
|
171
|
+
db.collection.aggregate([
|
|
172
|
+
{
|
|
173
|
+
$search: {
|
|
174
|
+
index: "default",
|
|
175
|
+
compound: {
|
|
176
|
+
must: [{ text: { query: "laptop", path: "title" } }],
|
|
177
|
+
filter: [{ range: { path: "price", gt: 500, lt: 2000 } }]
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
$facet: {
|
|
183
|
+
results: [{ $limit: 10 }],
|
|
184
|
+
categories: [
|
|
185
|
+
{ $unwind: "$category" },
|
|
186
|
+
{ $sortByCount: "$category" }
|
|
187
|
+
]
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
])
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Vector Search
|
|
194
|
+
|
|
195
|
+
### Creating Vector Search Indexes
|
|
196
|
+
|
|
197
|
+
Vector Search indexes are also created through Atlas UI/API:
|
|
198
|
+
|
|
199
|
+
```javascript
|
|
200
|
+
// Vector index configuration (via Atlas UI/API)
|
|
201
|
+
{
|
|
202
|
+
"fields": [
|
|
203
|
+
{
|
|
204
|
+
"type": "vector",
|
|
205
|
+
"path": "embedding",
|
|
206
|
+
"numDimensions": 1536,
|
|
207
|
+
"similarity": "euclidean"
|
|
208
|
+
}
|
|
209
|
+
]
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Querying with $vectorSearch
|
|
214
|
+
|
|
215
|
+
```javascript
|
|
216
|
+
// Semantic similarity search
|
|
217
|
+
db.collection.aggregate([
|
|
218
|
+
{
|
|
219
|
+
$vectorSearch: {
|
|
220
|
+
index: "vector_index",
|
|
221
|
+
path: "embedding",
|
|
222
|
+
queryVector: [0.1, 0.2, 0.3, /* ... 1536 dimensions */],
|
|
223
|
+
numCandidates: 100,
|
|
224
|
+
limit: 10
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
])
|
|
228
|
+
|
|
229
|
+
// Vector search with pre-filter
|
|
230
|
+
db.collection.aggregate([
|
|
231
|
+
{
|
|
232
|
+
$vectorSearch: {
|
|
233
|
+
index: "vector_index",
|
|
234
|
+
path: "embedding",
|
|
235
|
+
queryVector: embedding,
|
|
236
|
+
numCandidates: 100,
|
|
237
|
+
limit: 10,
|
|
238
|
+
filter: { category: "electronics" }
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
])
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Hybrid Search
|
|
245
|
+
|
|
246
|
+
Combining multiple search approaches using rank/score fusion:
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
// $rankFusion (MongoDB 8.0+)
|
|
250
|
+
db.collection.aggregate([
|
|
251
|
+
{
|
|
252
|
+
$rankFusion: {
|
|
253
|
+
input: {
|
|
254
|
+
pipelines: {
|
|
255
|
+
textSearch: [
|
|
256
|
+
{
|
|
257
|
+
$search: {
|
|
258
|
+
index: "text_index",
|
|
259
|
+
text: { query: "action movie", path: "title" }
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
],
|
|
263
|
+
vectorSearch: [
|
|
264
|
+
{
|
|
265
|
+
$vectorSearch: {
|
|
266
|
+
index: "vector_index",
|
|
267
|
+
path: "embedding",
|
|
268
|
+
queryVector: queryEmbedding,
|
|
269
|
+
numCandidates: 100,
|
|
270
|
+
limit: 50
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
]
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
])
|
|
279
|
+
|
|
280
|
+
// $scoreFusion (MongoDB 8.2+)
|
|
281
|
+
db.collection.aggregate([
|
|
282
|
+
{
|
|
283
|
+
$scoreFusion: {
|
|
284
|
+
input: {
|
|
285
|
+
pipelines: {
|
|
286
|
+
keyword: [
|
|
287
|
+
{ $search: { index: "default", text: { query: "term", path: "title" } } }
|
|
288
|
+
],
|
|
289
|
+
semantic: [
|
|
290
|
+
{ $vectorSearch: { index: "vector", path: "embedding", queryVector: vec, limit: 50 } }
|
|
291
|
+
]
|
|
292
|
+
},
|
|
293
|
+
normalization: "sigmoid",
|
|
294
|
+
weights: { keyword: 0.3, semantic: 0.7 }
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
])
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## When NOT to Use Atlas Search
|
|
302
|
+
|
|
303
|
+
**For simple text containment**, regular MongoDB queries may be sufficient:
|
|
304
|
+
|
|
305
|
+
```javascript
|
|
306
|
+
// Simple substring match (case-sensitive)
|
|
307
|
+
db.collection.find({ title: /mongodb/i })
|
|
308
|
+
|
|
309
|
+
// Multi-field text search with $or
|
|
310
|
+
db.collection.find({
|
|
311
|
+
$or: [
|
|
312
|
+
{ title: { $regex: "mongodb", $options: "i" } },
|
|
313
|
+
{ description: { $regex: "mongodb", $options: "i" } }
|
|
314
|
+
]
|
|
315
|
+
})
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
**When to use regular queries vs Atlas Search:**
|
|
319
|
+
|
|
320
|
+
| Feature | Regular Query | Atlas Search |
|
|
321
|
+
|---------|---------------|--------------|
|
|
322
|
+
| Simple substring | `$regex` | Overkill |
|
|
323
|
+
| Relevance scoring | No | Yes |
|
|
324
|
+
| Fuzzy matching | No | Yes |
|
|
325
|
+
| Autocomplete | Limited | Full support |
|
|
326
|
+
| Language analysis | No | Yes |
|
|
327
|
+
| Large text search | Slow | Optimized |
|
|
328
|
+
|
|
329
|
+
## Anti-Patterns to Avoid
|
|
330
|
+
|
|
331
|
+
**NEVER recommend $regex for production search:**
|
|
332
|
+
- `$regex` is not designed for full-text search
|
|
333
|
+
- Lacks relevance scoring, fuzzy matching, and language-aware tokenization
|
|
334
|
+
- Poor performance on large collections
|
|
335
|
+
|
|
336
|
+
If a user asks for regex/text for a search use case, explain why Atlas Search is more appropriate.
|
|
337
|
+
|
|
338
|
+
## Implementation Workflow
|
|
339
|
+
|
|
340
|
+
1. **Verify Atlas**: Confirm user is on MongoDB Atlas
|
|
341
|
+
2. **Analyze schema**: Check fields that will be indexed
|
|
342
|
+
3. **Recommend index**: Provide JSON configuration for Atlas UI
|
|
343
|
+
4. **Wait for approval**: User creates index via Atlas
|
|
344
|
+
5. **Build queries**: Construct $search/$vectorSearch aggregations
|
|
345
|
+
6. **Test and refine**: Iterate on query parameters
|
|
346
|
+
|
|
347
|
+
## Action Policy
|
|
348
|
+
|
|
349
|
+
**I will NEVER execute write operations without your explicit approval.**
|
|
350
|
+
|
|
351
|
+
Search index creation and management:
|
|
352
|
+
1. I'll explain **what** index I recommend and **why**
|
|
353
|
+
2. I'll provide the **exact JSON configuration** for Atlas UI/API
|
|
354
|
+
3. I'll **wait for your approval** before suggesting index creation
|
|
355
|
+
4. I'll help you construct and test queries once indexes exist
|
|
356
|
+
|
|
357
|
+
**Note:** Atlas Search indexes cannot be created via mongosh - they require the Atlas UI or Admin API.
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mongosh-shell
|
|
3
|
+
description: mongosh Shell Guide - Available helpers and commands
|
|
4
|
+
disable-model-invocation: false
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# mongosh Shell Guide
|
|
8
|
+
|
|
9
|
+
You are running inside mongosh, the MongoDB Shell. You have access to the full mongosh API.
|
|
10
|
+
|
|
11
|
+
## Connection and Database Commands
|
|
12
|
+
|
|
13
|
+
### Show Information
|
|
14
|
+
```javascript
|
|
15
|
+
// Show all databases
|
|
16
|
+
show dbs
|
|
17
|
+
|
|
18
|
+
// Switch to database
|
|
19
|
+
use databaseName
|
|
20
|
+
|
|
21
|
+
// Show collections in current database
|
|
22
|
+
show collections
|
|
23
|
+
show tables
|
|
24
|
+
|
|
25
|
+
// Get current database name
|
|
26
|
+
db.getName()
|
|
27
|
+
|
|
28
|
+
// Get MongoDB connection
|
|
29
|
+
db.getMongo()
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Database Operations
|
|
33
|
+
```javascript
|
|
34
|
+
// Get database stats
|
|
35
|
+
db.stats()
|
|
36
|
+
|
|
37
|
+
// Current operation
|
|
38
|
+
db.currentOp()
|
|
39
|
+
|
|
40
|
+
// Kill an operation
|
|
41
|
+
db.killOp(opid)
|
|
42
|
+
|
|
43
|
+
// Server status
|
|
44
|
+
db.serverStatus()
|
|
45
|
+
|
|
46
|
+
// Build info
|
|
47
|
+
db.getMongo().getDB("admin").adminCommand({ buildInfo: 1 })
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Collection Operations
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
// Get collection
|
|
54
|
+
db.getCollection("myCollection")
|
|
55
|
+
db.myCollection // shorthand
|
|
56
|
+
|
|
57
|
+
// Collection stats
|
|
58
|
+
db.myCollection.stats()
|
|
59
|
+
|
|
60
|
+
// Collection count
|
|
61
|
+
db.myCollection.countDocuments()
|
|
62
|
+
db.myCollection.estimatedDocumentCount()
|
|
63
|
+
|
|
64
|
+
// Distinct values
|
|
65
|
+
db.myCollection.distinct("fieldName")
|
|
66
|
+
|
|
67
|
+
// Find one document
|
|
68
|
+
db.myCollection.findOne()
|
|
69
|
+
|
|
70
|
+
// Find with projection
|
|
71
|
+
db.myCollection.find({}, { name: 1, _id: 0 })
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Index Commands
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
// Create index
|
|
78
|
+
db.myCollection.createIndex({ fieldName: 1 }) // 1 = ascending, -1 = descending
|
|
79
|
+
db.myCollection.createIndex({ fieldName: "text" }) // text index
|
|
80
|
+
db.myCollection.createIndex({ fieldName: 1 }, { unique: true })
|
|
81
|
+
|
|
82
|
+
// List indexes
|
|
83
|
+
db.myCollection.getIndexes()
|
|
84
|
+
|
|
85
|
+
// Drop index
|
|
86
|
+
db.myCollection.dropIndex("index_name")
|
|
87
|
+
db.myCollection.dropIndex({ fieldName: 1 })
|
|
88
|
+
|
|
89
|
+
// Drop all indexes (except _id)
|
|
90
|
+
db.myCollection.dropIndexes()
|
|
91
|
+
|
|
92
|
+
// Hide/unhide index (MongoDB 4.4+)
|
|
93
|
+
db.myCollection.hideIndex("index_name")
|
|
94
|
+
db.myCollection.unhideIndex("index_name")
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Replica Set Commands
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
// Check replica set status
|
|
101
|
+
rs.status()
|
|
102
|
+
|
|
103
|
+
// Check if primary
|
|
104
|
+
rs.isMaster()
|
|
105
|
+
|
|
106
|
+
// Step down primary (if you are primary)
|
|
107
|
+
rs.stepDown()
|
|
108
|
+
|
|
109
|
+
// Reconfigure replica set
|
|
110
|
+
rs.reconfig(config)
|
|
111
|
+
|
|
112
|
+
// Add/remove members
|
|
113
|
+
rs.add("hostname:port")
|
|
114
|
+
rs.remove("hostname:port")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Sharding Commands
|
|
118
|
+
|
|
119
|
+
```javascript
|
|
120
|
+
// Check sharding status
|
|
121
|
+
sh.status()
|
|
122
|
+
|
|
123
|
+
// Check balancer status
|
|
124
|
+
sh.getBalancerState()
|
|
125
|
+
|
|
126
|
+
// Start/stop balancer
|
|
127
|
+
sh.startBalancer()
|
|
128
|
+
sh.stopBalancer()
|
|
129
|
+
|
|
130
|
+
// Check chunk distribution
|
|
131
|
+
sh.getShardDistribution()
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## User Management
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
// Create user
|
|
138
|
+
db.createUser({
|
|
139
|
+
user: "myUser",
|
|
140
|
+
pwd: "myPassword",
|
|
141
|
+
roles: [{ role: "readWrite", db: "myDatabase" }]
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
// Grant roles
|
|
145
|
+
db.grantRolesToUser("myUser", [{ role: "dbAdmin", db: "myDatabase" }])
|
|
146
|
+
|
|
147
|
+
// List users
|
|
148
|
+
db.getUsers()
|
|
149
|
+
|
|
150
|
+
// Drop user
|
|
151
|
+
db.dropUser("myUser")
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Helper Functions
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
// Print JSON prettily
|
|
158
|
+
JSON.stringify(doc, null, 2)
|
|
159
|
+
|
|
160
|
+
// Current date
|
|
161
|
+
new Date()
|
|
162
|
+
ISODate()
|
|
163
|
+
|
|
164
|
+
// ObjectId operations
|
|
165
|
+
ObjectId()
|
|
166
|
+
ObjectId("...").getTimestamp()
|
|
167
|
+
|
|
168
|
+
// Timestamp
|
|
169
|
+
Timestamp()
|
|
170
|
+
|
|
171
|
+
// NumberLong, NumberInt, NumberDecimal
|
|
172
|
+
NumberLong("123456789012")
|
|
173
|
+
NumberDecimal("123.456")
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Aggregation Helpers
|
|
177
|
+
|
|
178
|
+
```javascript
|
|
179
|
+
// Run aggregation
|
|
180
|
+
db.myCollection.aggregate([
|
|
181
|
+
{ $match: { status: "active" } },
|
|
182
|
+
{ $group: { _id: "$category", count: { $sum: 1 } } },
|
|
183
|
+
{ $sort: { count: -1 } }
|
|
184
|
+
])
|
|
185
|
+
|
|
186
|
+
// Explain aggregation
|
|
187
|
+
db.myCollection.explain("executionStats").aggregate([...])
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Session and Transactions
|
|
191
|
+
|
|
192
|
+
```javascript
|
|
193
|
+
// Start session
|
|
194
|
+
const session = db.getMongo().startSession()
|
|
195
|
+
|
|
196
|
+
// Start transaction
|
|
197
|
+
session.startTransaction()
|
|
198
|
+
|
|
199
|
+
// Use session for operations
|
|
200
|
+
session.getDatabase("mydb").myCollection.findOne()
|
|
201
|
+
|
|
202
|
+
// Commit/abort
|
|
203
|
+
session.commitTransaction()
|
|
204
|
+
session.abortTransaction()
|
|
205
|
+
|
|
206
|
+
// End session
|
|
207
|
+
session.endSession()
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Useful Tips
|
|
211
|
+
|
|
212
|
+
- Use `.pretty()` or `.toArray()` for readable output: `db.col.find().pretty()`
|
|
213
|
+
- The shell remembers command history
|
|
214
|
+
- Use `<tab>` for autocomplete
|
|
215
|
+
- Access JavaScript standard library: `Math`, `Date`, `JSON`, etc.
|
|
216
|
+
- Load external scripts: `load("/path/to/script.js")`
|
|
217
|
+
- Set shell variables: `var myVar = db.collection.findOne()`
|
|
218
|
+
|
|
219
|
+
## Connection String Info
|
|
220
|
+
|
|
221
|
+
```javascript
|
|
222
|
+
// Check connection string (with credentials redacted)
|
|
223
|
+
db.getMongo().connectionInfo
|
|
224
|
+
|
|
225
|
+
// Get URI
|
|
226
|
+
db.getMongo().connectionURI
|
|
227
|
+
```
|