insertOne

insertOne is a Change Type in the Liquibase Open Source and Liquibase Pro extensions for MongoDB that inserts a single document into a collection.

Uses

The Liquibase Pro extension for MongoDB includes several modeled Change Types from the Liquibase Open Source version. These let you specify a few MongoDB commands using Liquibase XML, JSON, and YAML changelogs.

insertOne is one such Change Type. You can use it to insert a single document into a collection in your database.

If you want to insert multiple documents at once, use insertMany instead.

Note: If you want to specify mongosh statements in your XML, JSON, and YAML changelogs, use the mongo and mongoFile Change Types instead.

Run

To run this Change Type, follow these steps:

  1. Add the Change Type to your changeset, as shown in the examples on this page.

  2. Specify any required attributes. Use the table on this page to see which ones your database requires.

  3. Deploy your changeset by running the update command: liquibase update

Available attributes

For more information, see db.collection.insertOne().

Tip: You must specify all top-level attributes marked as required. If you specify an optional attribute, you must also specify any nested attributes that it requires.

Name

Type

Description

Requirement

collectionName

String

Name of the collection to insert an object into

Required

document

JSON object

JSON that defines the object to insert

Required

insertOne examples

databaseChangeLog:
  - changeSet:
      id: 2
      author: your.name
      changes:
        - insertOne:
            collectionName: towns_yaml
            document: |
              {
                name: "New York",
                population: 222000000,
                lastCensus: ISODate("2016-07-01"),
                famousFor: [ "the MOMA", "food", "Derek Jeter" ],
                mayor: {
                  name: "Bill de Blasio",
                  party: "D"
                }
              }
        - rollback: null