MongoDB

Nutzung, Komponenten, .NET

Moderator: Moderatoren

Antworten
jobbisoft
Rookie
Rookie
Beiträge: 16
Registriert: Mi, 12. Jan 2022 20:13
Kontaktdaten:

MongoDB

Beitrag von jobbisoft »

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
Grüße
Osvaldo Ramirez
Antworten