mongo

package
v1.2.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

Venom - Executor Mongo

Step to execute actions on a MongoDB database.

Input

See also tests/mongo.yml for executable examples.

Note: most fields support MongoDB Extended JSON v2. This means some special values (ObjectIds, ISODates, etc.) can be represented using the relaxed format.

Example:

{
  "_id": {
    "$oid": "5d505646cf6d4fe581014ab2"
  }
}
Load fixtures
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  actions:
    - type: loadFixtures
      folder: fixtures/

This action will first drop all the collections in the database, and then load multiple collections at once from a folder. The fixtures folder must contain one file per collection, and be named after the collection. For example, cards.yml will create a cards collection. The items in the collections are declared as a YAML array. For example:

# fixtures/cards.yml
- suit: clubs
  value: jack

- suit: clubs
  value: queen

- suit: clubs
  value: king
Insert documents
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: insert
      documents:
        - '{"suit": "hearts", "value": "queen"}'
        - '{"suit": "diamonds", "value": "three"}'
Insert documents from a file
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: insert
      file: fixtures/my-collection.jsonlist

fixtures/my-collection.jsonlist:

{
  "suit": "hearts",
  "value": "queen"
}
{
  "suit": "diamonds",
  "value": "three"
}
Find documents
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: find
      filter: |
        {
          "suit": "clubs"
        }
      options: # optional
        limit: 3
        skip: 1
        sort: '{"_id": -1}'
        projection: '{"value": 1}'
Find documents by ObjectID

See: MongoDB Extended JSON - Type Representations

- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: find
      filter: |
        {
          "_id": {
            "$oid": "5d505646cf6d4fe581014ab2"
          }
        }
      options: # optional
        limit: 3
        skip: 1
        sort: '{"_id": -1}'
        projection: '{"value": 1}'
Count documents
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: count
      filter: |
        {
          "suit": "clubs"
        }
Update documents
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: update
      filter: |
        {
          "suit": {
            "$in": ["clubs", "spades"]
          }
        }
      update: |
        {
          "$set": {
            "color": "black"
          }
        }
Delete documents
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: delete
      filter: |
        {
          "suit": "circles"
        }
Aggregate documents
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: aggregate
      pipeline:
        - |
          { "$match": {
            "color": "black"
          }}
        - |
          { "$group": {
            "_id": "$value",
            "count": {"$sum": 1}
          }}
Create a collection
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: createCollection
Drop a collection
- type: mongo
  uri: mongodb://localhost:27017
  database: my-database
  collection: my-collection
  actions:
    - type: dropCollection

Documentation

Index

Constants

View Source
const Name = "mongo"

Variables

This section is empty.

Functions

func New

func New() venom.Executor

Types

type Action

type Action struct {
	Type string
}

type AggregateAction

type AggregateAction struct {
	Action
	Pipeline []string
}

type CountAction

type CountAction struct {
	Action
	Filter  string
	Options struct {
		Limit *int64
	}
}

type DeleteAction

type DeleteAction struct {
	Action
	Filter string
}

type Executor

type Executor struct {
	URI        string           `json:"uri,omitempty" yaml:"uri,omitempty"`
	Database   string           `json:"database,omitempty" yaml:"database,omitempty"`
	Collection string           `json:"collection,omitempty" yaml:"collection,omitempty"`
	Actions    []map[string]any `json:"actions,omitempty" yaml:"actions,omitempty"`
}

func (Executor) Run

func (e Executor) Run(ctx context.Context, step venom.TestStep) (any, error)

type FindAction

type FindAction struct {
	Action
	Filter  string
	Options struct {
		Limit      *int64
		Skip       *int64
		Sort       string
		Projection string
	}
}

type InsertAction

type InsertAction struct {
	Action
	File      string
	Documents []string
}

type LoadFixturesAction

type LoadFixturesAction struct {
	Action
	Folder string
}

type Result

type Result struct {
	Actions []map[string]any `json:"actions,omitempty" yaml:"actions,omitempty"`
}

type UpdateAction

type UpdateAction struct {
	Action
	Filter  string
	Update  string
	Options struct {
		Upsert *bool
	}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL