How to write mongodb code in 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 TMongWire as a MongoDB wrapper 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 seems that the above codes do the job


but when I request "tags" it seems like this is not working for me

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

How to write two lines with * asterisks?

+4
source share
2 answers

The error is not in the request, a bit in the creation of fields.

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

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

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

Edited: introduced VarArrayOf()

+6
source

TMongoWire is trying to use OleVariant to the fullest, so you are passing arrays as array variants, for example. using VarArrayOf :

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

and there is no line parsing in javascript, so write:

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

All Articles