2012-07-08 54 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']));