Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Heroku - mLab MongoDB - Spring Boot - How to start example
# MONGODB
spring.data.mongodb.uri=${MONGODB_URI}
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "stats")
public class StatEntry {
@Id
public String id;
private long time;
private String referrer;
private String site;
...
}
...
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
...
</dependencies>
...
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface StatEntryRepo extends MongoRepository<StatEntry, String> {
StatEntry findOneById(String id);
List<StatEntry> findAll();
void delete(StatEntry entry);
void deleteAll();
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.UUID;
@Component
public class StatService {
@Autowired
public StatEntryRepo statsRepo;
public void addStatEntry(String site, String referrer) {
StatEntry entry = new StatEntry();
entry.setSite(site);
entry.setReferrer(referrer);
entry.setTime(Instant.now().toEpochMilli());
entry.setId(UUID.randomUUID().toString());
statsRepo.save(entry);
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.