#1 2014-12-19 00:55:12

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

[mongo] insertjson usage

Hi AB,

I'm trying to insert this:

[{_id:1,name:"test1"},{_id:2,name:"test2"},{_id:3,name:"test3"}]

using TmongoCollection.insertJSON


But I find a single line in mongodb:

{
    "_id" : ObjectId("54937698dd6a6a28075ffb63"),
    "0" : {
        "_id" : 1,
        "name" : "test1"
    },
    "1" : {
        "_id" : 2,
        "name" : "test2"
    },
    "2" : {
        "_id" : 3,
        "name" : "test3"
    }
}

Why ?

Offline

#2 2014-12-19 08:55:07

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

Re: [mongo] insertjson usage

Your code above is incorrect.

What did you write?

Offline

#3 2014-12-19 09:37:50

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

Re: [mongo] insertjson usage

sql:='select * from mysqlitedb';
r.Prepare(demo.DB,sql);

json:=demo.ExecuteJSON(sql,TRUE);

after run:

json:='[{_id:1,name:"test1"},{_id:2,name:"test2"},{_id:3,name:"test3"}]';

after I send it to a rest server:


var
pu:Putf8char;
begin
  pu:=putf8char(json);

  collection.InsertJSON(pu);

end;

collection is a TmongoCollection


the aim was to read a db SqlLite , and add in mongodb.

I thought the json generated is converted into an array, instead he inserirsce an array in an element.

how do I do it ?

Offline

#4 2014-12-19 09:39:08

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

Re: [mongo] insertjson usage

InsertJSON() expects an array of PUTF8Char, not a single PUTF8Char.

If you specify a single PUTF8Char, you insert a single document.

Offline

#5 2014-12-19 10:52:35

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

Re: [mongo] insertjson usage

how ?

json-->array of PUTF8Char

Offline

#6 2014-12-19 11:24:38

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

Re: [mongo] insertjson usage

One JSON object per PUTF8Char.

Offline

Board footer

Powered by FluxBB