Delphi에서 UniDAC 컴포넌트로 MongoDB에 연결하고 질의하여 그 결과를 가져오는 간단한 예제이다. UniDAC은 상용이므로 델파이를 구매한 경우 FireDAC을 이용하거나 Open Source인 GitHub의 DelphiMongoDB를 활용한다. 참고로 C#에서의 활용법은 IAmTimCorey, Intro to MongoDB with C# 동영상 강좌를 추천한다.
Delphi-UniDAC
//UniConnection1.SpecificOptions.Values['MongoDB.BSONLibrary'] :=
// 'C:\...\libbson-1.0.dll';
//UniConnection1.SpecificOptions.Values['MongoDB.ClientLibrary'] :=
// 'C:\...\libmongoc-1.0.dll';
//UniQuery1 : Data Type Mapping : string(Database Type) - WideString(Field Type)
//UniQuery1 : Data Type Mapping : null(Database Type) - String(Field Type)
procedure TForm1.Button1Click(Sender: TObject);
var
doc: TMongoDocument; // uses MongoObjectsUni
begin
try
UniQuery1.Close;
UniQuery1.SQL.Clear;
UniQuery1.SQL.Text :=
'{"find":"TestCollection", "filter":{"age":{"$gte": 31}}}';
UniQuery1.Open;
while not UniQuery1.Eof do
begin
doc := UniQuery1.GetObject('TestCollection') as TMongoDocument;
Memo1.Lines.Add(doc.FieldByName['name'].GetData.AsString + ' / '
+ doc.FieldByName['age'].GetData.AsString);
UniQuery1.Next;
end;
except
on E: Exception do
Memo1.Text := E.Message;
end;
end;
MongoDB CRUD Operations
- Create Operations : Insert Documents
- Read Operations : Query Documents
- Update Operations : Update Documents
- Delete Operations : Delete Documents
- Bulk Write : Bulk Write Operations
Reference
- SQL문법과 MongoDB 문법 비교 분석
- SQL to MongoDB Mapping Chart
- How to Install MongoDB on CentOS 8
- Using MongoDB data access provider with UniDAC in Delphi
- MongoDb client : A library of visual and non-visual components for Delphi.