Topics
All
MacOS
(Only)
Windows
(Only)
Linux
(Only, Not)
iOS
(Only, Not)
Components
Crossplatform Mac & Win
Server
Client
Old
Deprecated
Guides
Examples
Videos
New in version:
11.1
11.2
11.3
11.4
11.5
12.0
12.1
12.2
12.3
12.4
Statistic
FMM
Blog
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.
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 ) ]
See also
- MongoDB.Connect
- MongoDB.DeleteOne
- MongoDB.InsertMany
- MongoDB.New
- MongoDB.OpenCollection
- MongoDB.OpenDatabase
- MongoDB.Release
- MongoDB.SetURI
Example Databases
Created 22nd May 2022, last changed 26th July 2022
MongoDB.InsertMany - MongoDB.JSONtoBSON
Feedback: Report problem or ask question.
