description: operation bulk_write
schemaVersion: '1.27'
runOnRequirements:
  - minServerVersion: "8.0"
    serverless: forbid

createEntities:
  - client:
      id: &client0 client0
      useMultipleMongoses: false
      observeTracingMessages:
        enableCommandPayload: true
  - database:
      id: &database0 database0
      client: *client0
      databaseName: &databaseName0 operation-bulk-write-0
  - database:
      id: &database1 database1
      client: *client0
      databaseName: &databaseName1 operation-bulk-write-1
  - collection:
      id: collection0
      database: *database0
      collectionName: &collectionName0 test0
  - collection:
      id: collection1
      database: *database1
      collectionName: &collectionName1 test1

initialData:
  - collectionName: *collectionName0
    databaseName: *databaseName0
    documents: [ ]
  - collectionName: *collectionName1
    databaseName: *databaseName1
    documents: [ ]

_yamlAnchors:
  namespace0: &namespace0 "operation-bulk-write-0.test0"
  namespace1: &namespace1 "operation-bulk-write-1.test1"

tests:
  - description: bulkWrite
    operations:
      - object: *client0
        name: clientBulkWrite
        arguments:
          models:
            - insertOne:
                namespace: *namespace0
                document: { _id: 8, x: 88 }
            - updateOne:
                namespace: *namespace0
                filter: { _id: 1 }
                update: { $inc: { x: 1 } }
            - updateMany:
                namespace: *namespace1
                filter:
                  $and: [ { _id: { $gt: 1 } }, { _id: { $lte: 3 } } ]
                update: { $inc: { x: 2 } }
            - replaceOne:
                namespace: *namespace1
                filter: { _id: 4 }
                replacement: { x: 44 }
                upsert: true
            - deleteOne:
                namespace: *namespace0
                filter: { _id: 5 }
            - deleteMany:
                namespace: *namespace1
                filter:
                  $and: [ { _id: { $gt: 5 } }, { _id: { $lte: 7 } } ]

    expectTracingMessages:
      - client: *client0
        ignoreExtraSpans: false
        spans:
          - name: bulkWrite admin
            attributes:
              db.system.name: mongodb
              db.namespace: admin
              db.collection.name: { $$exists: false }
              db.operation.name: bulkWrite
              db.operation.summary: bulkWrite admin
            nested:
              - name: bulkWrite
                attributes:
                  db.system.name: mongodb
                  db.namespace: admin
                  db.collection.name: { $$exists: false }
                  db.command.name: bulkWrite
                  network.transport: tcp
                  db.mongodb.cursor_id: { $$exists: false }
                  db.response.status_code: { $$exists: false }
                  exception.message: { $$exists: false }
                  exception.type: { $$exists: false }
                  exception.stacktrace: { $$exists: false }
                  server.address: { $$type: string }
                  server.port: { $$type: [ int, long ] }
                  db.query.summary: bulkWrite admin
                  db.query.text:
                    $$matchAsDocument:
                      $$matchAsRoot:
                        bulkWrite: 1
                        errorsOnly: true
                        ordered: true
                        ops: [
                          {
                            "insert": 0,
                            "document": {
                              "_id": 8,
                              "x": 88
                            }
                          },
                          {
                            "update": 0,
                            "multi": false,
                            "filter": {
                              "_id": 1
                            },
                            "updateMods": {
                              "$inc": {
                                "x": 1
                              }
                            }
                          },
                          {
                            "update": 1,
                            "multi": true,
                            "filter": {
                              "$and": [
                                {
                                  "_id": {
                                    "$gt": 1
                                  }
                                },
                                {
                                  "_id": {
                                    "$lte": 3
                                  }
                                }
                              ]
                            },
                            "updateMods": {
                              "$inc": {
                                "x": 2
                              }
                            }
                          },
                          {
                            "update": 1,
                            "multi": false,
                            "filter": {
                              "_id": 4
                            },
                            "updateMods": {
                              "x": 44
                            },
                            "upsert": true
                          },
                          {
                            "delete": 0,
                            "multi": false,
                            "filter": {
                              "_id": 5
                            }
                          },
                          {
                            "delete": 1,
                            "multi": true,
                            "filter": {
                              "$and": [
                                {
                                  "_id": {
                                    "$gt": 5
                                  }
                                },
                                {
                                  "_id": {
                                    "$lte": 7
                                  }
                                }
                              ]
                            }
                          }
                        ]
                        nsInfo: [
                          {
                            "ns": *namespace0
                          },
                          {
                            "ns": *namespace1
                          }
                        ]
                  db.mongodb.server_connection_id:
                    $$type: [ int, long ]
                  db.mongodb.driver_connection_id:
                    $$type: [ int, long ]
