2012-07-08 85 views
4

MongoDB的代码这是原来的代码我想:如何写在delphi

obj = { 
    sentence: "this is a sentece", 
    tags: [ "some", "indexing", "words"]  
} 

findOne({tags: "words"}).name); 

我用TMongWire如MongoDB中的包装德尔福 和我写这个:

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

它似乎是c上述颂歌做的工作


但是当我用“标签”查询,它似乎不适合我

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

我如何写两行*星号?

回答

6

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

就像你写的那样,你创建了tags字段作为字符串属性,而不是字符串数组。

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

您必须致电VarArrayOf()来创建一个字符串数组。

被修改:引入VarArrayOf()

+0

我会尝试通过本地德尔福'Array'类型。如果这不起作用,你总是可以在github上发布一个[TMongoWire问题](https://github.com/stijnsanders/TMongoWire/issues),并希望原作者将回应/记录需要什么。 – Stennie 2012-07-09 00:40:44

+0

VarArrayOf()而不是BSONArray()....我离确定的答案并不遥远! :) OleVariant有点费时,并且依赖于平台......原生Delphi数组可能更有意义。 – 2012-07-09 06:20:41

3

TMongoWire尝试使用OleVariant到其最大程度,所以传递数组作为变体阵列,例如使用VarArrayOf

FMongoWire.Insert(theCollection,BSON([ 
    'id',mongoObjectID, 
    'sentence', 'this is a sentece', 
    'tags',VarArrayOf(['some', 'indexing', 'words']) 
]); 

,并有串没有JavaScript的符号解析,这么写:

q.Query(mwx2Collection, BSON(['tags','words']));