필터 지우기
필터 지우기

JSON insert to MongoDB

조회 수: 2 (최근 30일)
Seb
Seb 2017년 7월 23일
답변: Nikolaus Koopmann 2022년 4월 13일
I have a structure array. I am able to encode this to json formatted text using matlabs function jsonencode(). I now want to insert this into MongoDB using the java driver. This is what I have so far:
javaaddpath('/Users/seb/Documents/MATLAB/mongo-java-driver-3.4.2.jar')
import com.mongodb.*;
mongoClient = MongoClient();
db = mongoClient.getDB( 'mydb' );
colls = db.getCollectionNames();
coll = db.getCollection('testCollection');
doc = BasicDBObject();
doc.put(json_string);
wc = com.mongodb.WriteConcern(1);
coll.insert(doc, wc);
Problem is I cannot put the json_string into doc, as it needs to be parsed into a java object and then inserted. Any ideas....?
  댓글 수: 1
Sailesh Sidhwani
Sailesh Sidhwani 2017년 7월 27일
What is the exact error message that you receive when you try to put "json_string" into "doc"?

댓글을 달려면 로그인하십시오.

답변 (1개)

Nikolaus Koopmann
Nikolaus Koopmann 2022년 4월 13일
try this:
javaaddpath('/Users/seb/Documents/MATLAB/mongo-java-driver-3.12.10.jar') % NOTE: i changed the version
import com.mongodb.*;
mongoClient = MongoClient();
db = mongoClient.getDB( 'mydb' );
colls = db.getCollectionNames();
coll = db.getCollection('testCollection');
% changed code hereafter
doc = org.bson.Document.parse(java.lang.String(json_string)); % !!!
coll.insertOne(doc);

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by