How to return an object from a bank in QTL(Mongodb)?

Asked

Viewed 439 times

1

I currently have some objects in a Mongodb database. The structure is as follows.

{
    "_id" : ObjectId("55cb9c666c522cafdb053a68"),
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    -73.93383000695911,
                    40.81949109558767
                ],
                ...
            ]
        ]
    },
    "name" : "Central Harlem North-Polo Grounds"
}

Using Delphi I can get an entire object and remove the "_id", that is, my return is a Json with the "Geometry" and the "Name". Follow the code of Delphi.

   collection := FMongoConn.Databases['DataBase'].GetCollection('collec');

   mqry := TMongoQuery.Create(collection.Env);
   mqry := mqry.Project('{_id: false}').&End;  //Aqui eu removo o _id que não preciso

   Cursor := collection.Find(mqry);

   while Cursor.Next do
      Cursor.Doc.AsJSON  //Json de cada documento

I am trying to get only the object "Geometry" and return it to the Json, have to do this using the procedures of the driver of Delphi (Firedac) or through the find of Mongo?

The intended return would be only this

"geometry" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    -73.93383000695911,
                    40.81949109558767
                ],
                ...
            ]
        ]
    }

Through Mongodb himself, it can only bring an entire object?

1 answer

1


Declare a uses "System.JSON".

If "Cursor.doc.Asjson" is a Tjsonobject object:

var
  geometry: TJSONObject;
begin
  geometry := Cursor.Doc.AsJSON.GetValue('geometry') as TJSONObject;
  Memo1.Text := geometry.ToString;
end;

If "Cursor.doc.Asjson" is a string:

var
  j, geometry : TJSONObject;
begin
  j := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(Cursor.Doc.AsJSON), 0) as TJSONObject;
  geometry := j.GetValue('geometry') as TJSONObject;
  Memo1.Text := geometry.ToString;
end;
  • That’s exactly what I needed, thank you very much, Marcio!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.