Mongo and GridFS Java Driver with MATLAB
조회 수: 15 (최근 30일)
표시 이전 댓글
I am trying to upload and download pictures from MATLAB to MongoDB using the Mongo Java Driver.
I am following below tutorial on GridFS java:
In MATLAB I did :
javaaddpath("C:\Users\****\Documents\jar_files\mongo-java-driver-3.11.0-beta3.jar")
import com.mongodb.Block;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.*;
import com.mongodb.client.gridfs.model.*;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.io.*;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import com.mongodb.client.model.Filters.eq;
mongoClient = MongoClients.create();
myDatabase = mongoClient.getDatabase("TestData");
gridFSFilesBucket = GridFSBuckets.create(myDatabase, "PicturesTest");
streamToUploadFrom = FileInputStream(File("C:\Users\****\Pictures\MyPicture.png"));
Until that point it works but then I can't do the following line:
fileId = gridFSBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom)
I get follwing error message ;
Undefined variable "gridFSBucket" or class "gridFSBucket.uploadFromStream".
I do not get why it is not able to find gridFSBucket function in the java driver.
I tried to play with the jar files it worked once but is not working anymore (do not know what I did exactly)...
Any help is much appreciated.
Thanks.
댓글 수: 0
채택된 답변
Debasish Samal
2019년 6월 7일
The error occurs because there is no such object "gridFSBucket".
You need to create an object "gridFSBucket" of type GridFSBucket. Just add this line to your code:
gridFSBucket = GridFSBuckets.create(myDatabase);
Then add this:
fileId = gridFSBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom);
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!