Sully
Sully

Reputation: 179

Store large Image Files coded as base64 in MongoDB

I use a spring boot application with mongoDB as storage and want to store large image-files coded as base64 strings. As i know, mongoDB can only store files up to 16MB so i have to use gridfs. In multiple pages and tutorials they store files but i never found an example for storing objects with gridfs.

Basic example:

public class Student {

private String firstName;
private String lastName;
private String image;

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getImage() {
    return image;
}
public void setImage(String image) {
    this.image = image;
}
}

Do i have to store image-value seperately as an inputStream like mentioned here:

https://www.baeldung.com/spring-boot-mongodb-upload-file

Or are there other approaches?

Upvotes: 1

Views: 1554

Answers (1)

Sushil Mittal
Sushil Mittal

Reputation: 524

you can store meta data with gridfs

OR

if you want to store complete object then follow below steps:

  1. store only file in gridfs it will return a refernceid
  2. add referenced id to your object and store it into different document in mongoDB

Upvotes: 2

Related Questions