Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
MongoDB.InsertOne
Inserts a document into collection.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
MongoDB | 12.3 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
Parameters
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 |
Result
Returns JSON or error.
Description
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:
Number | Subtype |
0 | Generic binary subtype |
1 | Function data |
2 | Binary (old) |
3 | UUID (old) |
4 | UUID |
5 | MD5 |
6 | Encrypted BSON value |
7 | Compressed time series data |
Examples
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
- MongoDB.AbortTransaction
- MongoDB.CommitTransaction
- MongoDB.Connect
- MongoDB.DeleteOne
- MongoDB.InsertMany
- MongoDB.New
- MongoDB.OpenCollection
- MongoDB.OpenDatabase
- MongoDB.Release
- MongoDB.SetURI
Example Databases
Blog Entries
- OnWindowTransaction and MongoDB
- CSV in FileMaker with MBS FileMaker Plugin
- Store containers in a MongoDB
- New MongoDB Example
This function checks for a license.
Created 22nd May 2022, last changed 29th August 2022
