Wednesday, November 26, 2025

.::: Testing Script Lucee using Driver MongoDB Database (NoSQL) :::.

  

1. run mongo.cfm
 

2. run mongo_find as console
 

3. run mongo_table as table 
 



4. script lucee mongo.cfm 

[root@teguhth ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth ROOT]#
[root@teguhth ROOT]# cat mongo.cfm
<cfscript>
mongoClient = createObject("java", "com.mongodb.client.MongoClients")
    .create("mongodb://admin:admin@10.10.10.90:27017/admin");

db = mongoClient.getDatabase("test");

// Get MongoDB Version
cmd = createObject("java","org.bson.Document").init("buildInfo", 1);
info = db.runCommand(cmd);
version = info.getString("version");
writeOutput("<b>MongoDB Version: " & version & "</b><br><br>");

// Insert Data
DocumentClass = createObject("java", "org.bson.Document");
doc = DocumentClass.init();
doc.put("name", "Teguh");
doc.put("role", "Admin");

collection = db.getCollection("users");
collection.insertOne(doc);

writeOutput("Insert OK!");
mongoClient.close();
</cfscript>

[root@teguhth ROOT]#


5. script lucee mongo_find.cfm

[root@teguhth ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth ROOT]#
[root@teguhth ROOT]# cat mongo_find.cfm
<cfscript>
mongoClient = createObject("java", "com.mongodb.client.MongoClients")
    .create("mongodb://admin:admin@10.10.10.90:27017/admin");

db = mongoClient.getDatabase("teguhth");
collection = db.getCollection("pembelian");

// --- Ambil versi MongoDB dari buildInfo ---
cmd = createObject("java","org.bson.Document").init("buildInfo", 1);
info = db.runCommand(cmd);
version = info.getString("version");
writeOutput("<b>MongoDB Version: " & version & "</b><br><br>");

// --- Query data pembelian ---
cursor = collection.find();
iterator = cursor.iterator();

// Header Judul
writeOutput("<h2>Data Pembelian</h2><br>");

while ( iterator.hasNext() ) {
    doc = iterator.next();
    jsonStr = doc.toJson();
    writeOutput("<pre>" & jsonStr & "</pre><hr>");
}

mongoClient.close();
</cfscript>

[root@teguhth ROOT]#


6. script lucee mongo_table.cfm

[root@teguhth ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth ROOT]#
[root@teguhth ROOT]# cat mongo_table.cfm
<cfscript>
mongoClient = createObject("java", "com.mongodb.client.MongoClients")
    .create("mongodb://admin:admin@10.10.10.90:27017/admin");

db = mongoClient.getDatabase("teguhth");
collection = db.getCollection("pembelian");

// --- Ambil versi MongoDB ---
cmd = createObject("java","org.bson.Document").init("buildInfo", 1);
info = db.runCommand(cmd);
version = info.getString("version");

// Tampilkan header
writeOutput("<h3>MongoDB Version: #version#</h3><br>");

cursor = collection.find();
iterator = cursor.iterator();

writeOutput('<table border="1" cellpadding="6" cellspacing="0">');
writeOutput('<tr style="background-color:##ddd; font-weight:bold;">');
writeOutput('<th>Kode Pembelian</th>');
writeOutput('<th>Kode Barang</th>');
writeOutput('<th>Kode Customer</th>');
writeOutput('<th>Tanggal</th>');
writeOutput('<th>Jumlah</th>');
writeOutput('</tr>');

while ( iterator.hasNext() ) {
    doc = iterator.next();

    kodePembelian = doc.getString("KODE_PEMBELIAN");
    kodeBarang    = doc.getString("KODE_BARANG");
    kodeCustomer  = doc.getString("KODE_CUSTOMER");
    tanggal       = doc.getString("TANGGAL_PEMBELIAN");
    jumlah        = doc.getString("JUMLAH_PEMBELIAN");

    writeOutput('<tr>');
        writeOutput('<td>' & kodePembelian & '</td>');
        writeOutput('<td>' & kodeBarang & '</td>');
        writeOutput('<td>' & kodeCustomer & '</td>');
        writeOutput('<td>' & tanggal & '</td>');
        writeOutput('<td>' & jumlah & '</td>');
    writeOutput('</tr>');
}

writeOutput('</table>');
mongoClient.close();
</cfscript>

[root@teguhth ROOT]#


No comments:

Post a Comment

Popular Posts