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?
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()
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']));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With