2012-11-22 3 views
0

다음과 같이 UI에서 데이터를 가져오고 있습니다. 아래의 모든 입력 데이터는 문자열입니다.mongo db에 json 객체로 문자열 값을 저장하는 방법은 무엇입니까?

CUST_ID : temp001 cust_chart_id : 테스트 default_chart : 거짓 chart_pref_json : {범위 : 6m, CHART_TYPE : 촛대, 지표 : {}, 기간 : 매일, futurepadding : 20}}

내가 저장하기 위해 노력하고 mongodb의 chart_pref_json. 이 chart_pref_json 객체는 실제로 아래 문자열로 DB에

{ "_id" : ObjectId("50aca4caf5d0b0e4d31ef239"), "cust_id" : "temp001", "cust_chart_id" : "testing", "default_chart" : "false", "created_at" : NumberLong("1353491658551"), **"chart_pref_json" : "{range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20}" }** 

를 저장하지만 난 사실이 chart_pref_json이 아니라 아래 JSON 오브젝트로 저장하고 싶습니다.

{ "_id" : ObjectId("50aca4caf5d0b0e4d31ef239"), "cust_id" : "temp001", "cust_chart_id" : "testing", "default_chart" : "false", "created_at" : NumberLong("1353491658551"), **"chart_pref_json" : {range:6m,chart_type:candlestick,indicators:{},period:Daily,futurepadding:20} }** 

누구든지이 문제에 대해 도움을 줄 수 있습니까?

+0

mongodb에서 "chart_pref_json"을 (를) 저장하려고 할 때 : {range : 6m, chart_type : 촛대, 표시기 : {}, 기간 : Daily, futurepadding : 20}} 문자열로 저장됩니다. "{range : 20 분, 20 초} "로 저장하고 싶지 않은 채로 json {range : 6m, chart_type : 촛대, 지표 : {}, 기간 : Daily, futurepadding : 20} . – Sandy

답변

1

에 설명되어 있습니다

BasicDBObject obj = JSON.parse(varContainingJson); 

먼저 JSON 코드를 구문 분석 한 다음 결과 JSON 객체를 BSON 객체로 변환해야합니다.

MongoDB와 함께 제공되는 com.mongodb.util.JSON 클래스를 사용할 수 있습니다. Here is a tutorial.

0

응용 프로그램의 필드로 설정하기 전에 언어의 JSON 디코딩 기능을 사용하여 객체로 인코딩해야합니다.

$db->collection->insert(array(
    'cust_id' => 'temp001', 
    … your other fields … 
    'chart_pref' => json_decode($varContainingJson) 
)); 

자바를 들어, 다음 예제가 도움이 될 것입니다 : PHP, 당신은 그와 같은 할 것입니다 당신이 JSON 코드가있는 경우 문자열로 https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/Y3KyG_ZSfJg

관련 문제