Search code examples
mongodbjpageospatialmongodb-javajava-ee-7

Relational or Mondo Db in a network of location application?


I'm trying to start a project where a user can add a location and each location can have a relation to each other. It will be a web application and I'm planning to use javaee7 + jpa + wildfly.

Basically a location will have at least a name, text, longitude and latitude fields. Some other entities will be linked to this location. There will also be query like, search he nearest x location to y.

I've encountered mongodb several times before but, I'm always on the other part of the system, so I'm not really sure if there's a big benefit in using it in this kind of system. The application is not a store so there's no checkout process and therefore no transaction.

If ever I decided to use mongodb I'm looking at: http://hibernate.org/ogm/ and mongo db geospatial queries

I would like to ask for opinions. Thanks.


Solution

  • From your description, I think MongoDB is a pretty good choice:

    Your data model is fairly simple. You might even think of embedding the other entities in the location documents, reducing the number of queries needed to retrieve these entities which are near to a specific point to exactly 1 by doing

    db.collection.ensureIndex({fieldHoldingGeoJSON:"2dsphere"})
    
    db.collection.find({
      $near:{
        $geometry:{
          type:"Point",
          coordinates: [latitude, longitude]
        },
        $maxDistance: distanceInMeters
      }
    })
    

    Keep in mind though that the maximum size of a BSON document is limited to 16MB. So if there are a lot of related entities, it might be worth to actually embed the locations into the entities eliminating the need of a dedicated locations collection, though those still can be created on demand using the aggregation framework. With embedding the locations into the entities, you can scale basically indefinitely and you have all properties of said entities stored where they belong to (very NoSQLish).

    Although the second approach imposes redundancy, we are only talking a a few bytes, and even with 100M records, that would be less than 1Gb, with disk space being cheap.

    Also keep in mind that MongoDB comes with powerful features such as ( relatively ) easy scaling and an awesome aggregation framework, to name a few.