Last updated on
Apr 27, 2021
Cosmos DB is a distributed database engine with a core set of features, like.
- Ability to distribute your database globally
- Ability to scale both storage and throughput
- Low latency with enterprise grade security features
Cosmos DB can reshape data into formats that you may already be using. You can create a data model and then expose your model using one of many Cosmos DB API's.
For more information, see Azure Cosmos DB.
Integration with ColdFusion
Application.cfc
component {
this.name = "mongotests";
this.serialization.preservecaseforstructkey=true
this.enableNullSupport=true
this.cosmosconfig = deserializejson(fileread(expandPath(".\cosmosconfig.json"))) //storing credentials outside web root
this.datasources = {
"local"= {
type="mongodb"
},
"cosmos"= {
type="mongodb",
host="mongodb://" & this.cosmosconfig.host & "/?ssl=true&retrywrites=false",
"init": true,
"username": this.cosmosconfig.username,
"password": this.cosmosconfig.password
}
}
}
component {
this.name = "mongotests";
this.serialization.preservecaseforstructkey=true
this.enableNullSupport=true
this.cosmosconfig = deserializejson(fileread(expandPath(".\cosmosconfig.json"))) //storing credentials outside web root
this.datasources = {
"local"= {
type="mongodb"
},
"cosmos"= {
type="mongodb",
host="mongodb://" & this.cosmosconfig.host & "/?ssl=true&retrywrites=false",
"init": true,
"username": this.cosmosconfig.username,
"password": this.cosmosconfig.password
}
}
}
component { this.name = "mongotests"; this.serialization.preservecaseforstructkey=true this.enableNullSupport=true this.cosmosconfig = deserializejson(fileread(expandPath(".\cosmosconfig.json"))) //storing credentials outside web root this.datasources = { "local"= { type="mongodb" }, "cosmos"= { type="mongodb", host="mongodb://" & this.cosmosconfig.host & "/?ssl=true&retrywrites=false", "init": true, "username": this.cosmosconfig.username, "password": this.cosmosconfig.password } } }
cosmosconfig.json
{
"host": "host:port",
"username": "username",
"password": "password",
"replicaSet": "globaldb"
}
{
"host": "host:port",
"username": "username",
"password": "password",
"replicaSet": "globaldb"
}
{ "host": "host:port", "username": "username", "password": "password", "replicaSet": "globaldb" }
demo.cfm
<cfscript>
// Retrieve database
db = getmongoservice("cosmos").db("mydb")
collection = db.collection
collection.drop()
writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>")
writeOutput("<b> Insert a document </b><br/>")
// Insert many documents
res = collection.insertMany([
{
enrollno: "1001",
name: "John Doe",
college: "Any college",
course: {
courseName: "Any course",
duration: "4 Years"
},
address: {
city: "Any city",
state: "Any state",
country: "Any country"
}
},
{
enrollno: "1002",
name: "Jane Doe",
college: "Some college",
course: {
courseName: "Some course",
duration: "4 Years"
},
address: {
city: "Some city",
state: "Some state",
country: "Some country"
}
}
])
//count number of documents in the collection
writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>")
writeOutput("<b> Delete a document </b><br/>")
collection.deleteOne({name: MongoRegExp("Rushikesh Vekariya","i")})
writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>")
</cfscript>
<cfscript>
// Retrieve database
db = getmongoservice("cosmos").db("mydb")
collection = db.collection
collection.drop()
writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>")
writeOutput("<b> Insert a document </b><br/>")
// Insert many documents
res = collection.insertMany([
{
enrollno: "1001",
name: "John Doe",
college: "Any college",
course: {
courseName: "Any course",
duration: "4 Years"
},
address: {
city: "Any city",
state: "Any state",
country: "Any country"
}
},
{
enrollno: "1002",
name: "Jane Doe",
college: "Some college",
course: {
courseName: "Some course",
duration: "4 Years"
},
address: {
city: "Some city",
state: "Some state",
country: "Some country"
}
}
])
//count number of documents in the collection
writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>")
writeOutput("<b> Delete a document </b><br/>")
collection.deleteOne({name: MongoRegExp("Rushikesh Vekariya","i")})
writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>")
</cfscript>
<cfscript> // Retrieve database db = getmongoservice("cosmos").db("mydb") collection = db.collection collection.drop() writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>") writeOutput("<b> Insert a document </b><br/>") // Insert many documents res = collection.insertMany([ { enrollno: "1001", name: "John Doe", college: "Any college", course: { courseName: "Any course", duration: "4 Years" }, address: { city: "Any city", state: "Any state", country: "Any country" } }, { enrollno: "1002", name: "Jane Doe", college: "Some college", course: { courseName: "Some course", duration: "4 Years" }, address: { city: "Some city", state: "Some state", country: "Some country" } } ]) //count number of documents in the collection writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>") writeOutput("<b> Delete a document </b><br/>") collection.deleteOne({name: MongoRegExp("Rushikesh Vekariya","i")}) writeOutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>") </cfscript>