Hello,
I had the need to connect Xbase++ with mongodb.
There are ODBC Drivers on the market to emulate SQL instructions.
But it is much better to connect with the mongo-c drivers.
Given my limited knowledge to be able to do it from the Xbase++ API,
I am creating the connection via ActiveX,
This is my first test program.
// program: test.prg
// Author : (c)Osvaldo Ramirez
// Date : 2024-09-20
//
// Notes:
//
// This program make some operation on MongoDB via OLE/ActiveX
#include "activex.ch"
PROCEDURE main
LOCAL oMongoDB := NIL
local i , cJson
local aListCollections
? time()
? 'OS.........: ' , OS()
? 'Version....: ' , VERSION()
?
? "Test MongoDB "
?
oMongoDB := CreateObject("mongodb")
if valtype( oMongoDB ) = 'O'
? 'Connecting to "mongodb://localhost:27017" '
IF oMongoDb:connect("mongodb://localhost:27017")
? 'Conected! ... Creating testdb ..." '
?
IF oMongoDB:CreateDatabase( "testdb")
? "DB created, ... list all collection ..."
?
//
// the best way to create a collection/table is insertDocument
// because if you try to create an empty( collection ), it is necesary to insert a least one documento/record
//
for i := 1 to 5
oMongoDB:CreateCollection( "testCollection_"+alltrim(str(i)))
next
aListCollections := oMongoDB:listCollection() // it return a jsonencode
if ! empty( aListCollections )
aListCollections := JsonDecode( aListCollections ) // convert jsonencode to array
else
aListCollections := {}
endif
if len( aListCollections ) > 5
? "Removing the last one " + aListCollections[len(aListCollections)]
? oMongoDB:removeCollection(aListCollections[len(aListCollections)])
? oMongoDB:response()
endif
?
? 'Creating 200 Documents on clients collection ...'
? 'if the collection client doesnot exists, it create automaticaly ...'
?
for i := 1 to 200
cJson := '{' +;
'"record":' + alltrim(str(i)) + ',' + ;
'"name":"this record number ' + alltrim(str(i)) + '"' + ;
'}'
oMongoDB:InsertDocument( "clients", cJson )
next
// find all documents that "record" are equal 1
? "Finding documents ... "
cJson := '[{"$match":{"record":1}}]'
? oMongoDB:aggregate( "clients",cJson )
? oMongoDB:response()
?
? "Import DBF to MongoDB " , "articul.dbf" , "productos"
?
? oMongoDb:importDBF( "articul.dbf" , "productos")
? oMongoDb:response()
?
?
ENDIF
ENDIF
oMongoDB:close()
? "Thanks for using .. "
else
? "MongoDB ActiveX isn't installed!"
endif
?
? time()
?
RETURN
Let me know your comments
Best Regards
Osvaldo Ramirez
MongoDB
Nutzung, Komponenten, .NET
Moderator: Moderatoren
Gehe zu
- Allgemeines und Ankündigungen
- ↳ Ankündigungen
- Xbase++
- ↳ Allgemeine Fragen
- ↳ Installation und Redistribution
- ↳ Unterschiede Version 1.9 <-> 2.0
- ↳ Migration
- ↳ GUI
- ↳ Daten und Tabellen
- ↳ SQL-Server
- ↳ SQL (Sprache)
- ↳ Postgre Server
- ↳ mySql/MariaDb
- ↳ MS-SQL
- ↳ SQLite
- ↳ Programmierung allgemein
- ↳ Drucken und Export
- ↳ OOP
- ↳ Windows, API
- ↳ ActiveX
- ↳ MapPoint
- ↳ MS Office
- ↳ OpenOffice
- ↳ Acrobat & PDF
- ↳ Web
- ↳ Workbench (ab 2.0), VX (bis 1.9 SL1)
- ↳ IDEs und Editoren
- ↳ Misc.
- ↳ Newbie
- ↳ Foren-Gesamtindex
- 3rd Party
- ↳ eXpress++
- ↳ TopDown
- ↳ XClass++
- ↳ List & Label
- ↳ Fast Report / FRAX
- ↳ Andere Reportgeneratoren
- ↳ Andere 3rd-Party-Tools
- ↳ VDBU
- ↳ See4XB
- ↳ XbZLib
- ↳ LibXL
- ↳ RMChart
- ↳ Multiedit
- ↳ XppPdf
- ↳ TXControl
- ↳ InnoSetup
- ↳ ADSClass++
- ↳ ADS
- ↳ ARC
- ↳ SQLExpress
- ↳ Xb2.Net
- ↳ HBPrint
- ↳ Power libraries
- Sonstiges rund um die Programmierung
- ↳ Clipper
- ↳ Links und Demos
- ↳ Andere Sprachen
- ↳ Visual Objects/Vulcan .Net
- ↳ FoxPro/Visual FoxPro
- ↳ harbour/xHarbour
- ↳ Flagship/Visual Flagship
- ↳ WinDev
- ↳ X# XSharp
- ↳ Sonstige (technische) Fragen
- Community
- ↳ XUGs
- ↳ Marktplatz
- ↳ Rund ums Forum
- ↳ DevCon 2007
- ↳ DevCon 2012