英文:
How to insert in MongoDB
问题
我在MongoDB中执行CRUD操作。我尝试了这个示例,但它不起作用。我遇到了访问违规错误,然后是流读取错误。以下是我的代码:
var
fdCon: TFDConnection;
fdMongQuery: TFDMongoQuery;
mDoc: TMongoDocument;
begin
try
fdCon := TFDConnection.Create(nil);
fdMongQuery := TFDMongoQuery.Create(nil);
try
fdCon.Params.Clear;
fdCon.Params.DriverID := 'Mongo';
fdCon.Params.Add('Server=' + 'localhost');
fdCon.Params.Add('Port=' + '27017');
fdCon.Open;
fdMongQuery.Close;
fdMongQuery.FieldDefs.Clear;
fdMongQuery.FormatOptions.StrsTrim2Len := True;
fdMongQuery.Connection := fdCon;
fdMongQuery.DatabaseName := 'HR';
fdMongQuery.CollectionName := 'employee';
mDoc :=
TMongoDocument.Create(TMongoEnv(fdCon))
.Add('name', 'ago')
.Add('age', 12)
.Add('address', 'PH')
;
fdMongQuery.Collection.Insert(mDoc); // 访问违规 > 流读取错误
mDoc.Free;
finally
fdMongQuery.Free;
fdCon.Free;
end;
except
end;
end;
我哪里做错了?
英文:
I'm doing CRUD in MongoDB. I tried this example but I'm its not working. I'm getting access violation error then stream read error. Here is my code:
var
fdCon : TFDConnection;
fdMongQuery : TFDMongoQuery;
mDoc : TMongoDocument;
begin
try
fdCon := TFDConnection.Create(nil);
fdMongQuery := TFDMongoQuery.Create(nil);
try
fdCon.Params.Clear;
fdCon.Params.DriverID := 'Mongo';
fdCon.Params.Add('Server=' + 'localhost');
fdCon.Params.Add('Port=' + '27017');
fdCon.Open;
fdMongQuery.Close;
fdMongQuery.FieldDefs.Clear;
fdMongQuery.FormatOptions.StrsTrim2Len := True;
fdMongQuery.Connection := fdCon;
fdMongQuery.DatabaseName := 'HR';
fdMongQuery.CollectionName := 'employee';
mDoc :=
TMongoDocument.Create(TMongoEnv(fdCon))
.Add('name', 'ago')
.Add('age', 12)
.Add('address', 'PH')
;
fdMongQuery.Collection.Insert(mDoc); // access violation > stream read error
mDoc.Free;
finally
fdMongQuery.Free;
fdCon.Free;
end;
except
end;
end;
Where did I go wrong?
答案1
得分: 4
这段构造是错误的:
mDoc :=
TMongoDocument.Create(TMongoEnv(fdCon))
...
;
在创建 TMongoDocument 对象时,您将 TFDConnection 对象进行了类型转换为 TMongoEnv,但这是非法的类型转换(如果您在转换时使用 as 运算符,它会在运行时引发 EInvalidCast 异常)。TMongoDocument 需要一个实际的 TMongoEnv 对象。
相反,您需要在连接到 MongoDB 服务器后,将 TFDConnection.CliObj 属性类型转换为 TMongoConnection,然后可以使用 TMongoConnection.Env 属性来获取 TMongoEnv 对象。 这甚至在 Embarcadero 的文档中有解释:
FireDAC.Phys.MongoDBWrapper.TMongoEnv
不要直接创建 TMongoEnv 的实例。相反,使用MongoDB连接的Env属性中的一个。
FireDAC.Phys.MongoDBWrapper.TMongoConnection
要获取TMongoConnection的实例,不要直接创建一个。相反,配置一个TFDConnection组件以连接到您的 MongoDB 服务器,确保它已连接,然后将其CliObj属性转换为TMongoConnection。
因此,请尝试以下方式:
mDoc :=
TMongoDocument.Create(TMongoConnection(fdCon.CliObj).Env)
...
;
英文:
This construction is wrong:
mDoc :=
TMongoDocument.Create(TMongoEnv(fdCon))
...
;
You are type-casting the TFDConnection object to TMongoEnv when creating the TMongoDocument object, but that is an illegal type-cast (if you had used the as operator for the cast, it would have raised an EInvalidCast exception at runtime). TMongoDocument expects an actual TMongoEnv object.
You need to instead type-cast the TFDConnection.CliObj property to TMongoConnection after connecting to a MongoDB server, and then you can use the TMongoConnection.Env property to get the TMongoEnv object. This is even explained in Embarcadero's documentation:
FireDAC.Phys.MongoDBWrapper.TMongoEnv
> Do not create an instance of TMongoEnv directly. Instead, use the one from the Env property of a MongoDB connection.
FireDAC.Phys.MongoDBWrapper.TMongoConnection
> To obtain an instance of TMongoConnection, do not create one directly. Instead, configure a TFDConnection component to connect to your MongoDB server, ensure that it is connected, and cast its CliObj property to TMongoConnection
So, try this instead:
mDoc :=
TMongoDocument.Create(TMongoConnection(fdCon.CliObj).Env)
...
;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论