@@ -311,34 +311,35 @@ extension Mongo.CollectionModel
311
311
func insert( some elements: some Sequence < some Insertable > ) async throws
312
312
{
313
313
var count : Int = 0
314
- let response : Mongo . InsertResponse = try await session. run (
315
- command: Mongo . Insert. init ( Self . name,
316
- writeConcern: . majority)
314
+ let insert : Mongo . Insert = . init( Self . name, writeConcern: . majority)
315
+ {
316
+ $0 [ . ordered] = false
317
+ }
318
+ documents:
319
+ {
320
+ for element : some Insertable in elements
317
321
{
318
- $0 [ . ordered] = false
322
+ $0. append ( element)
323
+ count += 1
319
324
}
320
- documents:
321
- {
322
- for element : some Insertable in elements
323
- {
324
- $0. append ( element)
325
- count += 1
326
- }
327
- } ,
328
- against: self . database)
325
+ }
329
326
330
327
if count == 0
331
328
{
332
329
return
333
330
}
334
331
332
+ let response : Mongo . InsertResponse = try await self . session. run (
333
+ command: insert,
334
+ against: self . database)
335
+
335
336
let _: Mongo . Insertions = try response. insertions ( )
336
337
}
337
338
338
339
@inlinable
339
340
func insert( some element: some Insertable ) async throws
340
341
{
341
- let response : Mongo . InsertResponse = try await session. run (
342
+ let response : Mongo . InsertResponse = try await self . session. run (
342
343
command: Mongo . Insert. init ( Self . name, writeConcern: . majority)
343
344
{
344
345
$0. append ( element)
@@ -354,14 +355,23 @@ extension Mongo.CollectionModel
354
355
func upsert(
355
356
some elements: some Sequence < some Insertable > ) async throws -> Mongo . Updates < Element . ID >
356
357
{
357
- let response : Mongo . UpdateResponse < Element . ID > = try await session. run (
358
- command: Mongo . Update< Mongo . One, Element . ID> . init( Self . name)
358
+ var count : Int = 0
359
+ let update : Mongo . Update < Mongo . One , Element . ID > = . init( Self . name)
360
+ {
361
+ for element : some BSONDocumentEncodable & Identifiable < Element . ID > in elements
359
362
{
360
- for element : some BSONDocumentEncodable & Identifiable < Element . ID > in elements
361
- {
362
- $0. upsert ( element)
363
- }
364
- } ,
363
+ $0. upsert ( element)
364
+ count += 1
365
+ }
366
+ }
367
+
368
+ if count == 0
369
+ {
370
+ return . init( selected: 0 , modified: 0 , upserted: [ ] )
371
+ }
372
+
373
+ let response : Mongo . UpdateResponse < Element . ID > = try await self . session. run (
374
+ command: update,
365
375
against: self . database)
366
376
367
377
return try response. updates ( )
@@ -370,7 +380,7 @@ extension Mongo.CollectionModel
370
380
@inlinable
371
381
func upsert( some element: some Insertable ) async throws -> Element . ID ?
372
382
{
373
- let response : Mongo . UpdateResponse < Element . ID > = try await session. run (
383
+ let response : Mongo . UpdateResponse < Element . ID > = try await self . session. run (
374
384
command: Mongo . Update< Mongo . One, Element . ID> . init( Self . name)
375
385
{
376
386
$0. upsert ( element)
0 commit comments