MongoDB.InsertMany
------------------

Insert documents into collection.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [MongoDB](component_MongoDB.md) | [12.4](newinversion124.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "MongoDB.InsertMany"; MongoDBRef; documentJSON { ; OptionsJSON } ) 

**MBS**( **"MongoDB.InsertMany";** /\* Insert documents into collection. \*/  
**$MongoDBRef**; /\* The reference number for the mongo connection.e.g. $MongoDB \*/   
**$documentJSON**; /\* The JSON with an array of the records to save. \*/   
**$OptionsJSON**) /\* Optional; The options to use. \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| MongoDBRef | The reference number for the mongo connection. | $MongoDB |  |
| documentJSON | The JSON with an array of the records to save. |  |  |
| OptionsJSON | The options to use. |  | Optional |

### Result

Returns OK or error.

### Description

Insert documents into collection.  
Please pass a JSON array as string for all documents. Each entry in the array should be a JSON object with the document data.  
  
For any document that does not have an "\_id" field, a bson\_oid\_t will be generated locally and added to the document. If you must know the inserted document's \_id, generate it in your code and include it in the document. The \_id you generate can be a bson\_oid\_t or any other non-array BSON type.  
  
The reply is filled out with an "insertedCount" field. If there is a server error then reply may contain a "writeErrors" array and/or a "writeConcernErrors" array.  
### Examples

Insert two records in one call:

 Set Variable \[ $JSON ; Value: "\[{\\"ID\\": 1, \\"Name\\": \\"John\\"}, {\\"ID\\": 2, \\"Name\\": \\"Miller\\"}\]" \]   
Set Variable \[ $r ; Value: MBS ( "MongoDB.InsertMany"; $Mongo ; $JSON ) \]  
<small>  
Example result:   
{ "insertedCount" : 2 } </small>### See also

- [MongoDB.DeleteMany](MongoDBDeleteMany.md)
- [MongoDB.InsertOne](MongoDBInsertOne.md)

### Release notes

- **Version 12.4**
    - Added [MongoDB.InsertMany](https://www.mbsplugins.eu/MongoDBInsertMany.shtml) function to insert multiple records in one operation.

### Blog Entries

- [CSV in FileMaker with MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2022-10-11/CSV_in_FileMaker_with_MBS_File/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 12.4](https://www.mbsplugins.de/archive/2022-09-27/MBS_FileMaker_Plugin_124/monkeybreadsoftware_blog_filemaker)
- [Neues MBS Plugin 12.4 für Claris FileMaker](https://www.mbsplugins.de/archive/2022-09-06/Neues_MBS_Plugin_124_für_Clar/monkeybreadsoftware_blog_filemaker)
- [MBS Plugin 12.4 for Claris FileMaker](https://www.mbsplugins.de/archive/2022-09-06/MBS_Plugin_124_for_Claris_File/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.4pr1](https://www.mbsplugins.de/archive/2022-08-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 26th July 2022 , last changed 24th September 2022

  
[MongoDB.InTransaction](MongoDBInTransaction.md) - [MongoDB.InsertOne](MongoDBInsertOne.md)

[HTML Version](MongoDBInsertMany.shtml)