Navigation

insert

On this page

Definition

insert

Inserts the stage’s input documents into the specified MongoDB collection.

Important

In order to use MongoDB service actions, you must have rules set up for each namespace included in the operation.

The insert action cannot be in the first stage of a pipeline. The stage preceding the insert action stage must output documents; for example, a MongoDB service action find stage or a built-in action literal stage.

The insert action stage has the following syntax:

{
   "service": "mongodb-atlas",
   "action": "insert",
   "args": {
      "database": <dbname>,
      "collection": <collection name>
   }
}

The insert action takes the following arguments:

Argument Type Description
database string Name of the database.
collection string Name of the collection.

Example

Consider the following pipeline of two stages: The first stage uses the built-in literal action to pass an array of documents to a second pipeline stage. The second stage uses the insert action to add the documents to a MongoDB collection called orders in the my_db database.

[
  {
    "service":"","action":"literal",
    "args":{ "items":[
       {"customer":"A","item": "oranges","qty":10},
       {"customer":"B","item":"grapes", "qty": 50},
       {"customer":"A","item": "oranges","qty":10},
       {"customer":"B","item":"apples", "qty": 10}
    ] }
  },
  { "service":"mongodb-atlas","action":"insert","args":{"database":"my_db","collection":"orders"} }
]

Stage1: The literal stage outputs the following documents:

{"customer":"A","item": "oranges","qty":10}
{"customer":"B","item":"grapes", "qty": 50}
{"customer":"A","item": "oranges","qty":10}
{"customer":"B","item":"apples", "qty": 10}

The documents are input to Stage 2.

Stage2: The MongoDB insert stage inserts the documents into the specified collection my_db.orders.

←   find update  →