question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

This library is nice, but we can not easily use classes like Point, MultiPoints… in our POCO mapped to MongoDB document. Imagine a document like that :

{
  "_id": "5a7b8a6f70217a0c44b0ecaa",
  "name": "Foo",
  "position": {
    "type": "Point",
    "coordinates": ["2.879", "45.5931"]
  }
}

Deserialization of document by C# MongoDB driver fails because it cannot deserialize the position embedded document as the type field does not exists in the Point class : there is no type attribute, neither setter for Type property. Error message is : FormatException: An error occurred while deserializing the Position property of class MyProject.MyClass : Element 'type' does not match any field or property of class GeoJSON.Net.Geometry.Point.

Can we consider to have a private setter on Type property. The property could be available in the abstract base class GeoJSONObject, and its constructor could take the type as parameter and set it.

public abstract class GeoJSONObject : ...
{
  public GeoJSONObject(GeoJSONObjectType type)
  {
    this.Type = type;
  }

  public GeoJSONObjectType Type { get; private set }
  .....
}

So geometry classes could be simplified like this :

public class Point : GeoJSONObject, ...
{
  public Point(IPosition coordinates) : base(GeoJSONObjectType.Point)
  {
    this.Coordinates = coordinates ?? throw new ArgumentNullException(nameof(coordinates));
  }

  // This property is already available in base class
  // public override GeoJSONObjectType Type => GeoJSONObjectType.Point;

  ....
}

What do you think ?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
antonsimolacommented, Jun 19, 2018

I’m struggling with this as well. I’m experimenting registering class maps (BsonClassMap.RegisterClassMap) for each relevant class in GeoJSON.NET package.

I would like to have House come through WebAPI and serialize it straight like that into MongoDB:

class House { public Point Location {get;set;}; }

However some fields such as type and coordinates are not being stored in Mongodb.

EDIT: ended up writing my own POCOs for GeoJSON. If I tried using MongoDB C# driver provided GeoJSON, WebAPI wouldn’t work. With these, Mongo serialization wouldn’t work.

0reactions
wassim-kcommented, Nov 28, 2022

For those still looking for a solution, I have created a MongoDB.NetTopologySuite.Serialization package which adds support for serializing/deserializing NetTopologySuite models to BSON and back.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Use MongoDB And When To Use It?
MongoDB is a document-oriented NoSQL database that is used to store large amounts of data. Find out other reasons to use MongoDB and...
Read more >
Getting Started — MongoDB Manual
This tutorial walks you through inserting test data into a MongoDB database and querying that data using the documentation's embedded web shell.
Read more >
MongoDB Getting Started
There are many ways to connect to your MongoDB database. We will start by using the MongoDB Shell, mongosh . Use the official...
Read more >
How to get started with MongoDB in 10 minutes
mongod stands for “Mongo Daemon”. mongod is a background process used by MongoDB. The main purpose of mongod is to manage all the...
Read more >
Use Database Commands — MongoDB Manual
The MongoDB command interface provides access to all non CRUD database operations. Fetching server statistics, initializing a replica set, and running an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found