如何在delphi中编写mongodb代码

use*_*167 4 delphi mongodb

这是我尝试的原始代码:

obj = {
    sentence:  "this is a sentece", 
    tags: [ "some", "indexing", "words"]     
}
Run Code Online (Sandbox Code Playgroud)

findOne({tags: "words"}).name);
Run Code Online (Sandbox Code Playgroud)

我使用TMongWire作为MongoDB for Delphi的包装器,我写了这个:

//var
//  d:IBSONDocument;
d:=BSON([
    'id',mongoObjectID,
    'sentence', 'this is a sentece',
    'tags','["some", "indexing", "words"]'
]);
FMongoWire.Insert(theCollection,d);
Run Code Online (Sandbox Code Playgroud)

似乎上面的代码完成了工作


但是当我用'标签'查询时,它似乎对我不起作用

//var 
//q:TMongoWireQuery;
//qb:IBSONDocument 
qb:=BSON(['tags', '"words"']); //***
q:=TMongoWireQuery.Create(FMongoWire);
q.Query(mwx2Collection, qb); //***
Run Code Online (Sandbox Code Playgroud)

如何用*星号写两行?

Arn*_*hez 6

错误不在查询中,在字段创建中位.

在您编写它时,您将tags字段创建为字符串属性,而不是字符串数组.

d:=BSON([
    'id',mongoObjectID,
    'sentence', 'this is a sentece',
    'tags',VarArrayOf(['some', 'indexing', 'words'])
]);
FMongoWire.Insert(theCollection,d);
Run Code Online (Sandbox Code Playgroud)

您必须调用VarArrayOf()以创建字符串数组.

编辑:介绍 VarArrayOf()