#1 2015-01-15 00:57:21

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

[MONGODB] self-generation of _ID

Hi AB,


I'm trying to add users to authuser.
At the second start of the program I have four, but I can not add the fifth.

No error or exception, but in the end I found the answer in the log:

There is a problem with the self-generation of _ID

function TSQLRestStorageMongoDB.EngineNextID: TID;
procedure ComputeMax_ID;
var res: variant;
begin
  if not fIndexesCreated then
    CreateIndexes;
  res := fCollection.AggregateDoc('{$group:{_id:null,max:{$max:"$_id"}}}'); <----- WRONG, works only in shell
  if DocVariantType.IsOfType(res) then
    fEngineLastID := VariantToInt64Def(res.max,0);
  {$ifdef WITHLOG}
  fOwner.LogFamily.SynLog.Log(sllInfo,'Computed EngineNextID=%',[fEngineLastID],self);
  {$endif}
end;

Error from log

20150115 01380705 SQL   {collection:"MAINHEAD.$cmd",opCode:"opQuery",requestID:5,query:{aggregate:"AuthUser",pipeline:"{$group:{_id:0,max:{$max:\"$_id\"}}}"},numberToReturn:1}
20150115 01380746 DB    {ReplyHeader:{ResponseFlags:8,RequestID:49,ResponseTo:5,CursorID:0,StartingFrom:0,NumberReturned:1,ReplyDocuments:[{errmsg:"exception: wrong type for field (pipeline) 2 != 4",code:13111,ok:0}]}


Looking around I found these:
http://docs.mongodb.org/manual/referenc … op._S_type
http://stackoverflow.com/questions/1239 … 3111-error

Offline

#2 2015-01-15 07:54:56

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: [MONGODB] self-generation of _ID

I suspect there is something wrong with your framework units (SynMongoDB+mORMotMongoDB).
With the current version, I've got the following command:

{aggregate:"perftest",pipeline:[{$group:{_id:null,max:{$max:"$_id"}}}]

... which sounds correct to me, and returns

{"result":null,"ok":1}

.

Offline

#3 2015-01-15 09:44:56

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Re: [MONGODB] self-generation of _ID

I made a test project.
The program adds a user randomly at each start, the first works, but the second does not work.
I get the same result with xe2 and XE7, mongodb 2.6.1 and 2.6.6.


Demo

Offline

#4 2015-01-15 17:33:57

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Re: [MONGODB] self-generation of _ID

any news ?

works with your setup?

Offline

#5 2015-01-15 19:15:18

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Re: [MONGODB] self-generation of _ID

I found the problem!

These two functions are not equivalent:
1) function TMongoCollection.AggregateCall(const pipelineJSON: RawUTF8; var reply,res: variant): boolean;
2) function TMongoCollection.AggregateCall(const pipelineArray: variant; var reply,res: variant): boolean;

The first works well.
The second one does not insert properly the pipeline as an array.

Temporary patch:

function TMongoCollection.AggregateDoc(const pipelineArray: variant): variant;
var reply: variant;
begin
  if AggregateCall(RAWutf8(pipelineArray),reply,result) then   // I added RAWutf8 to force the use of the first
    TDocVariant.GetSingleOrDefault(result,result,result) else
    SetVariantNull(result);
end;

Last edited by Sabbiolina (2015-01-15 19:17:11)

Offline

#6 2015-01-15 19:25:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: [MONGODB] self-generation of _ID

It worked with Delphi 7, but not with Delphi XE7....
The overloaded methods were not interpreted the same.
sad

I've renamed TCollection.AggregateDoc() / AggregateJSON() overloaded methods to distinct methods specifying their input parameter type.
See http://synopse.info/fossil/info/a4ca5c4946

Should work as expected now.
Sorry for the delay, but I was focusing on implementing Intel AES-NI hardware acceleration to SynCrypto.pas - which works very nicely now - so it was a single task to write such low level assembler code.
See http://synopse.info/fossil/info/4b79a0d89e66f7

Offline

#7 2015-01-15 19:48:15

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Re: [MONGODB] self-generation of _ID

The first tests seems to work well.

Offline

Board footer

Powered by FluxBB