1. What is MongoDB?

MongoDB is an open-source NoSQL database, implemented in C++.

Has drivers and client libraries for languages like C, C++, C# and .NET, Java , Node Js, Perl, PHP, Python, Ruby, Scala

1.1 Key features:

  • Document database
  • Schema-less
  • High availability (through replication facility, called replica sets)
  • Automatic failover
  • Automatic scaling
  • Automatic sharding distributes data across a cluster of machines.

2. MongoDB main use cases

  • Operational Intelligence
    • Storing Log Data
    • hierarchical reports (i.e. per-minute, per-hour, and per-day) from raw event data
    • Product Data Management
  • Product Catalog
    • Inventory Management
    • Category Hierarchy
  • Content Management Systems
    • Metadata and Asset Management
    • Storing Comments

3. Ways to interact

public class Person {

    private String id;
    private String firstname;
    private String lastname;

    // omit getters/setters
}

3.1 Person Repository (Spring Data)

public interface PersonRepository 
   extends PagingAndSortingRepository<Person, String> {

    Optional<Person> findByFirstname(String firstName);

    Optional<Person> findByLastname(String firstName);

}

3.2 Save

Person p = new Person();
p.setFirstname(firstName);
p.setLastname(lastName);

personRepository.save(p);

// or 

mongoTemplate.insert(p);

3.3 Read

personRepository.findOne(id);

mongoTemplate.findOne(
	query(where("lastname").is(lastName)),
	Person.class);

3.4 Update

mongoTemplate.updateFirst(
	query(where("lastname").is(lastName)),
	update("firstname", updateFirstNameTo),
	Person.class);

3.5 Delete

mongoTemplate.remove(
	query(where("lastname").is(lastName)));

4. GeoSpatial queries

mongoTemplate.indexOps(Venue.class).ensureIndex(
	new GeospatialIndex("location"));

mongoTemplate.insert(
	new Venue("Venue 1", 
		new Point(
		51.10682735591432, 
		-114.11773681640625)));

mongoTemplate.find(
	query(where("location")
	.near(new Point(51.00, -114.00))
	.maxDistance(1.00)), Venue.class);

5. Conclusion

This was a tiny introduction into the world of Spring Data MongoDB. You can check out the code in 

Github Repo

Thanks for reading.