Skip to content

Commit 88ce61b

Browse files
h3n4lclaude
andauthored
feat: implement milestone 2 write operations (#16)
Add support for 10 MongoDB write operations: Insert: - insertOne(document, options) - insertMany(documents, options) Update: - updateOne(filter, update, options) - updateMany(filter, update, options) - replaceOne(filter, replacement, options) Delete: - deleteOne(filter, options) - deleteMany(filter, options) Atomic find-and-modify: - findOneAndUpdate(filter, update, options) - findOneAndReplace(filter, replacement, options) - findOneAndDelete(filter, options) Supported options include: - writeConcern (w, j; wtimeout parsed but ignored in driver v2) - upsert, hint, collation, arrayFilters, let - bypassDocumentValidation, comment, ordered - sort, projection, returnDocument Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4d0ac79 commit 88ce61b

9 files changed

Lines changed: 2249 additions & 49 deletions

File tree

README.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,56 @@ Results are returned in Extended JSON (Relaxed) format:
121121
| BinData() | `BinData(subtype, base64)` | |
122122
| RegExp() | `RegExp("pattern", "flags")`, `/pattern/flags` | |
123123

124-
### Milestone 2: Write Operations (Planned)
124+
### Milestone 2: Write Operations (Current)
125+
126+
#### Insert Commands
127+
128+
| Command | Syntax | Status |
129+
|---------|--------|--------|
130+
| db.collection.insertOne() | `insertOne(document, options)` | Supported |
131+
| db.collection.insertMany() | `insertMany(documents, options)` | Supported |
132+
133+
#### Update Commands
134+
135+
| Command | Syntax | Status |
136+
|---------|--------|--------|
137+
| db.collection.updateOne() | `updateOne(filter, update, options)` | Supported |
138+
| db.collection.updateMany() | `updateMany(filter, update, options)` | Supported |
139+
| db.collection.replaceOne() | `replaceOne(filter, replacement, options)` | Supported |
140+
141+
#### Delete Commands
142+
143+
| Command | Syntax | Status |
144+
|---------|--------|--------|
145+
| db.collection.deleteOne() | `deleteOne(filter, options)` | Supported |
146+
| db.collection.deleteMany() | `deleteMany(filter, options)` | Supported |
147+
148+
#### Atomic Find-and-Modify Commands
125149

126150
| Command | Syntax | Status |
127151
|---------|--------|--------|
128-
| db.collection.insertOne() | `insertOne(document)` | Not yet supported |
129-
| db.collection.insertMany() | `insertMany(documents)` | Not yet supported |
130-
| db.collection.updateOne() | `updateOne(filter, update)` | Not yet supported |
131-
| db.collection.updateMany() | `updateMany(filter, update)` | Not yet supported |
132-
| db.collection.deleteOne() | `deleteOne(filter)` | Not yet supported |
133-
| db.collection.deleteMany() | `deleteMany(filter)` | Not yet supported |
134-
| db.collection.replaceOne() | `replaceOne(filter, replacement)` | Not yet supported |
135-
| db.collection.findOneAndUpdate() | `findOneAndUpdate(filter, update)` | Not yet supported |
136-
| db.collection.findOneAndReplace() | `findOneAndReplace(filter, replacement)` | Not yet supported |
137-
| db.collection.findOneAndDelete() | `findOneAndDelete(filter)` | Not yet supported |
152+
| db.collection.findOneAndUpdate() | `findOneAndUpdate(filter, update, options)` | Supported |
153+
| db.collection.findOneAndReplace() | `findOneAndReplace(filter, replacement, options)` | Supported |
154+
| db.collection.findOneAndDelete() | `findOneAndDelete(filter, options)` | Supported |
155+
156+
#### Write Operation Options
157+
158+
| Option | Applies To | Description |
159+
|--------|-----------|-------------|
160+
| `writeConcern` | All write ops | Write concern settings (`w`, `j`, `wtimeout`*) |
161+
| `bypassDocumentValidation` | Insert, Update, Replace, FindOneAndUpdate/Replace | Skip schema validation |
162+
| `comment` | All write ops | Comment for server logs |
163+
| `ordered` | insertMany | Execute inserts sequentially (default: true) |
164+
| `upsert` | Update, Replace, FindOneAndUpdate/Replace | Insert if no match found |
165+
| `hint` | Update, Replace, Delete, FindOneAnd* | Force index usage |
166+
| `collation` | Update, Replace, Delete, FindOneAnd* | String comparison rules |
167+
| `arrayFilters` | updateOne, updateMany, findOneAndUpdate | Array element filtering |
168+
| `let` | Update, Replace, Delete, FindOneAnd* | Variables for expressions |
169+
| `sort` | updateOne, replaceOne, FindOneAnd* | Document selection order |
170+
| `projection` | FindOneAnd* | Fields to return |
171+
| `returnDocument` | FindOneAndUpdate/Replace | Return "before" or "after" |
172+
173+
*Note: `wtimeout` is parsed but ignored as it's not supported in MongoDB Go driver v2.
138174

139175
### Milestone 3: Administrative Operations (Planned)
140176

error_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func TestPlannedOperation(t *testing.T) {
3232
gc := gomongo.NewClient(client)
3333
ctx := context.Background()
3434

35-
// insertOne is a planned M2 operation - should return PlannedOperationError
36-
_, err := gc.Execute(ctx, dbName, "db.users.insertOne({ name: 'test' })")
35+
// createIndex is a planned M3 operation - should return PlannedOperationError
36+
_, err := gc.Execute(ctx, dbName, "db.users.createIndex({ name: 1 })")
3737
require.Error(t, err)
3838

3939
var plannedErr *gomongo.PlannedOperationError
4040
require.ErrorAs(t, err, &plannedErr)
41-
require.Equal(t, "insertOne()", plannedErr.Operation)
41+
require.Equal(t, "createIndex()", plannedErr.Operation)
4242
}
4343

4444
func TestUnsupportedOperation(t *testing.T) {
@@ -78,8 +78,9 @@ func TestUnsupportedOptionError(t *testing.T) {
7878
func TestMethodRegistryStats(t *testing.T) {
7979
total := gomongo.MethodRegistryStats()
8080

81-
// Registry should contain M2 (10) + M3 (22) = 32 planned methods
82-
require.Equal(t, 32, total, "expected 32 planned methods in registry (M2: 10, M3: 22)")
81+
// Registry should contain M3 (22) planned methods
82+
// M2 write operations have been implemented and removed from the registry
83+
require.Equal(t, 22, total, "expected 22 planned methods in registry (M3: 22)")
8384

8485
// Log stats for visibility
8586
t.Logf("Method Registry Stats: total=%d planned methods", total)

internal/executor/executor.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ func Execute(ctx context.Context, client *mongo.Client, database string, op *tra
4040
return executeEstimatedDocumentCount(ctx, client, database, op)
4141
case translator.OpDistinct:
4242
return executeDistinct(ctx, client, database, op)
43+
case translator.OpInsertOne:
44+
return executeInsertOne(ctx, client, database, op)
45+
case translator.OpInsertMany:
46+
return executeInsertMany(ctx, client, database, op)
47+
case translator.OpUpdateOne:
48+
return executeUpdateOne(ctx, client, database, op)
49+
case translator.OpUpdateMany:
50+
return executeUpdateMany(ctx, client, database, op)
51+
case translator.OpReplaceOne:
52+
return executeReplaceOne(ctx, client, database, op)
53+
case translator.OpDeleteOne:
54+
return executeDeleteOne(ctx, client, database, op)
55+
case translator.OpDeleteMany:
56+
return executeDeleteMany(ctx, client, database, op)
57+
case translator.OpFindOneAndUpdate:
58+
return executeFindOneAndUpdate(ctx, client, database, op)
59+
case translator.OpFindOneAndReplace:
60+
return executeFindOneAndReplace(ctx, client, database, op)
61+
case translator.OpFindOneAndDelete:
62+
return executeFindOneAndDelete(ctx, client, database, op)
4363
default:
4464
return nil, fmt.Errorf("unsupported operation: %s", statement)
4565
}

0 commit comments

Comments
 (0)