You are not logged in.
Pages: 1
I thought that the two lines of code are equivalent, however it seems that the first is not interpreted correctly, the call does not return.
doc:=Coll.FindDoc('{Name:?}',['{"$regex": "test.*", $options: "i"}']); // infinite loop
doc:=Coll.FindDoc('{Name:{"$regex": "test.*", $options: "i"}}',[]); // ok
Offline
You are confusing {Name:?} and {Name:%}
{Name:?} means that ? will be replaced by a value - and here your value is a string, not an object.
FindDoc('{Name:?}',['{"$regex": "test.*", $options: "i"}'])
will search for
{Name:"{\"$regex\": \"test.*\", $options: \"i\"}"}
Which is not the same as
FindDoc('{Name:{"$regex": "test.*", $options: "i"}}',[])
You may write:
FindDoc('{Name:?}',[BSONVariant('{"$regex": "test.*", $options: "i"}')])
to search for an object.
But there is no benefit at all in respect to "pure JSON"
FindDoc('{Name:{"$regex": "test.*", $options: "i"}}',[])
But the fact that there is an infinite loop is not expected.
It should return no match, not loop.
Where in the code does it loop for ever?
Offline
doc:=Coll.FindDoc('{Name:%}',['"$regex": "test.*", $options: "i"']);
function TBSONWriter.BSONWriteDocFromJSON(JSON: PUTF8Char; aEndOfObject: PUTF8Char;
out Kind: TBSONElementType; DoNotTryExtendedMongoSyntax: boolean): PUTF8Char;
in this point:
'{': begin
Kind := betDoc;
Start := BSONDocumentBegin;
repeat inc(JSON) until not(JSON^ in [#1..' ']);
repeat
// see http://docs.mongodb.org/manual/reference/mongodb-extended-json
Name := GetJSONPropName(JSON); // BSON/JSON accepts "" as key name
BSONWriteFromJSON(Name,JSON,@EndOfObject,DoNotTryExtendedMongoSyntax);
if (JSON=nil) or (EndOfObject=#0) then
exit; // invalid content
until EndOfObject='}';
end;
EndOfObject is always ':'
Last edited by Sabbiolina (2014-05-22 07:55:26)
Offline
You are absolutely right.
But I'm a little concerned by the queries dynamically created with parts supplied by the user.
there is some function of input validation in this fantastic library?
Offline
Update. This is the string that blocks the parser:
doc:=Coll.FindDoc('{Name:?}',['{"$regex": "test.*", $options: "i"}']);
Your first
Offline
after formatUTF8:
'{Name:"{""$regex"": ""test.*"", $options: ""i""}"}'
Offline
Pages: 1