Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old


Inserts a document into collection.

Component Version macOS Windows Linux Server iOS SDK
MongoDB 12.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "MongoDB.InsertOne"; MongoDBRef; documentJSON { ; OptionsJSON } )   More


Parameter Description Example Flags
MongoDBRef The reference number for the mongo connection. $MongoDB
documentJSON The JSON with the record to save.
OptionsJSON The options to use. Optional


Returns JSON or error.


Inserts a document into collection.
To insert an array of documents, see MongoDB.InsertMany.
If no _id element is found in document, then an oid 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 an old or any other non-array BSON type.
The reply is filled out with an “insertedCount” field. If there is a server error then reply contains either a “writeErrors” array with one subdocument or a “writeConcernErrors” array.

OptionsJSON may be empty or a JSON document with additional command options:

writeConcern: the write concern.
sessionId: The session ID.
validate: The validation flags.
bypassDocumentValidation: Set to true to skip server-side schema validation of the provided BSON documents.

Use MongoDB.InsertMany to insert multiple documents.

Binary Data can be passed with {"$binary"} JSON like showing in the example below:

0Generic binary subtype
1Function data
2Binary (old)
3UUID (old)
6Encrypted BSON value
7Compressed time series data


Insert a Record:

# start a new session
Set Variable [ $Mongo ; Value: MBS( "MongoDB.New" ) ]
# where is the server?
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb://localhost/" ) ]
# connect
Set Variable [ $r ; Value: MBS( "MongoDB.Connect"; $Mongo) ]
# open the database
Set Variable [ $r ; Value: MBS( "MongoDB.OpenDatabase"; $Mongo; "local" ) ]
# open the collection (table)
Set Variable [ $r ; Value: MBS( "MongoDB.OpenCollection"; $Mongo; "test" ) ]
# now insert a record with passing a JSON
Set Variable [ $r ; Value: MBS( "MongoDB.InsertOne"; $Mongo; "{\"Hello\": \"FileMaker\"}" ) ]
# and show answer/error
Show Custom Dialog [ "Insert Reply" ; $r ]
# cleanup
Set Variable [ $r ; Value: MBS( "MongoDB.Release"; $Mongo ) ]

Insert an image as binary data:

Set Variable [ $data ; Value: JSONSetElement ( "{}" ; ["subType"; 0; JSONString]; ["base64"; Base64Encode ( test::Image ); JSONString]) ]
Set Variable [ $binary ; Value: JSONSetElement ( "{}" ; ["$binary"; $data; JSONRaw]) ]
Set Variable [ $json ; Value: JSONSetElement ( "{}" ; ["FileID" ; 123 ; JSONNumber] ; ["filename" ; "test.png" ; JSONString] ; ["Data" ; $binary ; JSONRaw] ) ]
# JSON like this: {"Data":{"$binary":{"base64":"iVBORw0K...ElFTkSuQmCC\r\n","subType":0}},"FileID":123,"filename":"test.png"}
# show in field
Set Field [ test::Insert JSON ; $json ]
# and insert to database
Set Variable [ $r ; Value: MBS( "MongoDB.InsertOne"; $Mongo; $json) ]
Show Custom Dialog [ "Result" ; $r ]

See also

Example Databases

Blog Entries

This function checks for a license.

Created 22nd May 2022, last changed 29th August 2022

MongoDB.InsertMany - MongoDB.IsTracing