The MongoDB Connector service enables you to connect to MongoDB database servers to read and write information that can be used in your applications. It works with on-premise installations, as well as MongoDB Atlas.
It is generally expected that you understand MongoDB and the syntax for MongoDB queries when using this connector, for more information about MongoDB's syntax, see here: MongoDB Documentation.
The MongoDB Connector service contains the information required to establish a service connection to your database.
mongodb+srv://<user>:<password>@tartabit-serverless.axsmook.mongodb.net/?retryWrites=true&w=majority
Internal URLs only work for privately hosted instances of the IoT Bridge.
mongodb://root:<password>@10.100.10.13
Insert documents into a collection.
// insert a single record into a collection named 'demo'
mongodb.insert('mongodb-atlas','demo',{abc:'def456', def: 567})
// insert multiple records into a collection named 'demo'
mongodb.insert('mongodb-atlas','demo',[{abc:'def456', def: 567},{abc:'ghi789', xyz: true}])
Update documents that match the filter with the specified changes. Refer to mongodb's documentation about how to structure an update request.
// update records with 'abc' equal to 'abc123' and set a new field 'test' to true.
mongodb.update('mongodb-atlas','demo',{abc:'abc123'}, {"$set":{"test":true}})
// update records with 'abc' equal to 'abc123' and set a new field 'test' to true, if the record doesn't exist, insert it and add 'newRec'=true.
mongodb.update('mongodb-atlas','demo',{abc:'abc123'}, {"$set":{"test":true}, "$setOnInsert":{"newRec":true}},{upsert: true)
Find a single document in the database, if multiple documents match the filter, only the first will be returned.
// update records with 'abc' equal to 'abc123' and set a new field 'test' to true.
var doc = mongodb.findOne('mongodb-atlas','demo',{abc:'abc123'})
// doc will contain the document that was retrieved.
// use try/catch incase documents are not found
try {
var res = mongodb.findOne('mongodb-atlas','demo',{abc:'abc124'})
log.trace('findOne', res)
} catch(e) {
log.trace('findOne failed', e)
}
Find multiple documents in a collection. Currently a maximum limit of 1000 documents can be retrieved in a single call.
// update records with 'abc' equal to 'abc123' and set a new field 'test' to true.
var result = mongodb.findAll('mongodb-atlas','demo',{abc:'abc123'})
// result will contain an object with the documents and a count.
var firstDoc = result.documents[0]
// use try/catch incase documents are not found
try {
var res = mongodb.findAll('mongodb-atlas','demo',{abc:'abc124'})
log.trace('findAll', res)
} catch(e) {
log.trace('findAll failed', e)
}
Delete multiple documents in the database, if no documents match the filter, it is not considered an error.
// delete documents with 'abc' equal to 'abc123'.
mongodb.delete('mongodb-atlas','demo',{abc:'abc123'})
The following events are considered billable: