<commit_before>## MongoDB in Spring Boot using Spring Data  

The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. MongoDB is a document based storage, which stores data in BSON format which is similar to JSON(WIP).  
Code reference: [https://github.com/GlueCoders/springboot-guide/releases/tag/mongo-springdata](https://github.com/GlueCoders/springboot-guide/releases/tag/mongo-springdata)

#### Spring Data MongoDB Starter POM
```
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-mongodb</artifactId>
  <version>${spring.version}</version>
</dependency>
```
After adding this dependency Spring Boot will automatically configure defaults for connecting to MongoDB. By default it will try to connect localhost MongoDB at port 27017. To change this configuration, one can modify `spring.data.mongodb.uri` in `application.properties`.  

#### Entity Models - Book

```
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
import java.time.YearMonth;

@Document(collection = "books")
public class Book implements Serializable {

    @Id @JsonIgnore
    private String id;
    private String title;
    private String author;
    private YearMonth publishedYear;
    private List<String> categories;
    private String publisher;
    private long isbnCode;
```  
`@Document` defines the collection name in MongoDB in which `Book` documents will be saved.  
`@Id` maps the `_id` field to `id` in `Book`. In this application, `id` is generated automatically by MongoDB.  
`@JsonIgnore` is the Jackson annotation, this is to avoid serialization of id field in json since `isbnCode` is the natural id with which clients will work.  

In MongoDB this object's representation will be as shown below  
```
{
        "_id" : ObjectId("59414737007f68148802a13a"),
        "_class" : "org.gluecoders.library.models.Book",
        "title" : "Effective Java",
        "author" : "<NAME>",
        "publishedYear" : {
                "year" : 2011,
                "month" : 8
        },
        "categories" : [
                "Programming",
                "Java"
        ],
        "publisher" : "noidea",
        "isbnCode" : NumberLong(1234567890)<commit_msg>Updated mongodb-basics.md<commit_after>## MongoDB in Spring Boot using Spring Data  

The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. MongoDB is a document based storage, which stores data in BSON format which is similar to JSON(WIP).  
Code reference: [https://github.com/GlueCoders/springboot-guide/releases/tag/mongo-springdata](https://github.com/GlueCoders/springboot-guide/releases/tag/mongo-springdata)

#### Spring Data MongoDB Starter POM
```
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-mongodb</artifactId>
  <version>${spring.version}</version>
</dependency>
```
After adding this dependency Spring Boot will automatically configure defaults for connecting to MongoDB. By default it will try to connect localhost MongoDB at port 27017. To change this configuration, one can modify `spring.data.mongodb.uri` in `application.properties`.  

#### Entity Models - Book

```
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
import java.time.YearMonth;

@Document(collection = "books")
public class Book implements Serializable {

    @Id @JsonIgnore
    private String id;
    private String title;
    private String author;
    private YearMonth publishedYear;
    private List<String> categories;
    private String publisher;
    private long isbnCode;
```  
`@Document` defines the collection name in MongoDB in which `Book` documents will be saved.  
`@Id` maps the `_id` field to `id` in `Book`. In this application, `id` is generated automatically by MongoDB.  
`@JsonIgnore` is the Jackson annotation, this is to avoid serialization of id field in json since `isbnCode` is the natural id with which clients will work.  

In MongoDB this object's representation will be as shown below  
```
{
        "_id" : ObjectId("59414737007f68148802a13a"),
        "_class" : "org.gluecoders.library.models.Book",
        "title" : "Effective Java",
        "author" : "<NAME>",
        "publishedYear" : {
                "year" : 2008,
                "month" : 5
        },
        "categories" : [
                "Programming",
                "Java"
        ],
        "publisher" : "Addison-Wesley",
        "isbnCode" : NumberLong(1234567890)
{'fraction_non_alphanumeric': 0.09769417475728155, 'fraction_numerical': 0.016585760517799353, 'mean_word_length': 3.643192488262911, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1500434', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1314, 'n_words': 464}