Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write the mongodb code in delphi

Tags:

mongodb

delphi

This is the original code I tried:

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

and

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

I used the TMongWire as the wrapper of MongoDB for Delphi and I wrote this:

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

it seem the codes above do the work


but when I query with the 'tags', it seems to not work for me

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

How do I write the two lines with * asterisks?

like image 448
user1262167 Avatar asked Jan 17 '23 04:01

user1262167


2 Answers

The error is not in the query, bit in the fields creation.

As you wrote it, you created the tags field as a string property, not an array of strings.

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

You have to call VarArrayOf() to create an array of strings.

Edited: introduced VarArrayOf()

like image 96
Arnaud Bouchez Avatar answered Jan 25 '23 15:01

Arnaud Bouchez


TMongoWire tries to use OleVariant to their full extent, so you pass arrays as variant arrays, e.g. using VarArrayOf:

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

and there's no javascript notation parsing of strings, so write:

q.Query(mwx2Collection, BSON(['tags','words']));
like image 34
Stijn Sanders Avatar answered Jan 25 '23 16:01

Stijn Sanders