繁体   English   中英

如何在delphi中编写mongodb代码

[英]how to write the mongodb code in delphi

这是我尝试的原始代码:

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

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

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

//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()

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']));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM