How to return an object from a database in SQL (MongoDb)?

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", ie my return is a Json with "Geometry" and "Name". Follow the Delphi code.

   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'm trying to get only the geometry object and return it to Json, can you do this using the Delphi (FireDac) driver procedures or through Mongo's find?

The pretentious return would be just that

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

Through MongoDB itself, can you bring only an entire object?

    
asked by anonymous 28.06.2018 / 20:05

1 answer

1

Declare to use "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;
    
29.06.2018 / 04:30