这是我试过的原始代码:
obj = {
sentence: "this is a sentece",
tags: [ "some", "indexing", "words"]
}
和
findOne({tags: "words"}).name);
我使用 TMongWire 作为 Delphi 的 MongoDB 包装器 我写了这个:
//var
// d:IBSONDocument;
d:=BSON([
'id',mongoObjectID,
'sentence', 'this is a sentece',
'tags','["some", "indexing", "words"]'
]);
FMongoWire.Insert(theCollection,d);
看起来上面的代码可以完成工作
但是当我用“标签”查询时,它似乎对我不起作用
//var
//q:TMongoWireQuery;
//qb:IBSONDocument
qb:=BSON(['tags', '"words"']); //***
q:=TMongoWireQuery.Create(FMongoWire);
q.Query(mwx2Collection, qb); //***
如何写出带有 * 星号的两行?
最佳答案
错误不在查询中,而是在字段创建中。
在您编写它时,您将 tags 字段创建为字符串属性,而不是字符串数组。
d:=BSON([
'id',mongoObjectID,
'sentence', 'this is a sentece',
'tags',VarArrayOf(['some', 'indexing', 'words'])
]);
FMongoWire.Insert(theCollection,d);
您必须调用 VarArrayOf()
来创建一个字符串数组。
已编辑:介绍 VarArrayOf()
关于delphi - 如何在delphi中编写mongodb代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11380576/