insertMany
insertMany
is a Change Type in the Liquibase Open Source and Liquibase Pro extensions for MongoDB that inserts multiple documents 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.
insertMany
is one such Change Type. You can use it to insert multiple documents into a collection in your database.
If you just want to insert a single document, use insertOne
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:
- Add the Change Type to your changeset, as shown in the examples on this page.
- Specify any required attributes. Use the table on this page to see which ones your database requires.
- Deploy your changeset by running the
update
command:
liquibase update
Available attributes
For more information, see db.collection.insertMany().
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 objects into | Required |
documents
|
JSON object | JSON that defines the objects to insert | Required |
Examples
databaseChangeLog:
- changeSet:
id: 2
author: your.name
changes:
- insertMany:
collectionName: countries_yaml
documents: |
[
{
_id : "us",
name : "United States",
exports : {
foods : [
{name : "bacon", tasty : "true" },
{name : "burger"}
]
}
},
{
_id : "ca",
name : "Canada",
exports : {
foods : [
{name : "bacon", tasty : false },
{name : "syrup", tasty : true}
]
}
},
{
_id : "mx",
name : "Mexico",
exports : {
foods : [
{name : "salsa", tasty : true, condiment : true}
]
}
}
]
- rollback:
runCommand:
command: |
{
delete: "countries_yaml",
deletes: [ { q: { }, limit: 0 } ]
}
{
"databaseChangeLog": [
{
"changeSet": {
"id": "2",
"author": "your.name",
"changes": [
{
"insertMany": {
"collectionName": "countries_json",
"documents": {
"$rawJson": [
{
"_id" : "us",
"name" : "United States",
"exports" : {
"foods" : [
{"name" : "bacon", "tasty" : "true" },
{"name" : "burger"}
]
}
},
{
"_id" : "ca",
"name" : "Canada",
"exports" : {
"foods" : [
{"name" : "bacon", "tasty" : false },
{"name" : "syrup", "tasty" : true}
]
}
},
{
"_id" : "mx",
"name" : "Mexico",
"exports" : {
"foods" : [
{"name" : "salsa", "tasty" : true, "condiment" : true}
]
}
}
]
}
}
}
],
"rollback": {
"runCommand": {
"command": "{ delete: \"countries_json\", deletes: [{q: { }, limit: 0}] }"
}
}
}
}
]
}
The Liquibase MongoDB Open Source extension uses a unique mongodb
XML namespace and XSD files in the changelog header. However, the ext
prefix used with other extensions is backwards-compatible:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongodb="http://www.liquibase.org/xml/ns/mongodb"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/mongodb
http://www.liquibase.org/xml/ns/mongodb/liquibase-mongodb-latest.xsd">
<changeSet id="2" author="your.name">
<mongodb:insertMany collectionName="countries">
<mongodb:documents>
[
{
_id : "us",
name : "United States",
exports : {
foods : [
{name : "bacon", tasty : "true" },
{name : "burger"}
]
}
},
{
_id : "ca",
name : "Canada",
exports : {
foods : [
{name : "bacon", tasty : false },
{name : "syrup", tasty : true}
]
}
},
{
_id : "mx",
name : "Mexico",
exports : {
foods : [
{name : "salsa", tasty : true, condiment : true}
]
}
}
]
</mongodb:documents>
</mongodb:insertMany>
<rollback>
<mongodb:runCommand>
<mongodb:command>
{
delete: "countries",
deletes: [ { q: { }, limit: 0 } ]
}
</mongodb:command>
</mongodb:runCommand>
</rollback>
</changeSet>
</databaseChangeLog>
Database support
This Change Type is only supported for MongoDB. It does not support auto rollback.