port 확인하는 법
mongo --eval "printjson(db.serverCmdLineOpts())" 명령으로 사용하는 포트를 확인한다
[root@server mongoDBtest]# mongo --eval "printjson(db.serverCmdLineOpts())"
MongoDB shell version v5.0.26
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ab826c11-d106-4751-bbf2-91b8a17d1de9") }
MongoDB server version: 5.0.26
{
"argv" : [
"/usr/bin/mongod",
"-f",
"/etc/mongod.conf"
],
"parsed" : {
"config" : "/etc/mongod.conf",
"net" : {
"bindIp" : "0.0.0.0",
"port" : 27017
},
"processManagement" : {
"timeZoneInfo" : "/usr/share/zoneinfo"
},
"storage" : {
"dbPath" : "/mongod/db",
"directoryPerDB" : true,
"journal" : {
"enabled" : true
}
},
"systemLog" : {
"destination" : "file",
"logAppend" : true,
"path" : "/mongod/log/mongod.log"
}
},
"ok" : 1
}
Collection 생성
> db.createCollection("xmlCollection")
{ "ok" : 1 }
Jar 파일 다운
Central Repository: org/mongodb/mongo-java-driver/3.9.1 (maven.org)
에서 jar 파일을 가져와서 java 코드가 있는 폴더에 넣어줬다.
json 라이브러리 다운
https://repo1.maven.org/maven2/org/json/json/20200518/json-20200518.jar
컴파일
[root@server mongoDBtest]# ls -al
total 2100
drwxr-xr-x. 2 root root 107 May 13 11:40 .
dr-xr-x---. 9 root root 4096 May 13 10:14 ..
-rw-r--r--. 1 root root 65966 May 13 11:39 json-20200518.jar
-rw-r--r--. 1 root root 2065743 May 13 11:29 mongo-java-driver-3.9.1.jar
-rw-r--r--. 1 root root 1937 May 13 11:40 XmlTest.class
-rw-r--r--. 1 root root 1263 May 13 11:40 XmlTest.java
[root@server mongoDBtest]# java -cp ./mongo-java-driver-3.9.1.jar:./json-20200518.jar XmlTest
실행
[root@sever mongoDBtest]# java -cp .:./mongo-java-driver-3.9.1.jar:./json-20200518.jar XmlTest
May 13, 2024 11:49:25 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
May 13, 2024 11:49:25 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
May 13, 2024 11:49:25 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:17}] to localhost:27017
May 13, 2024 11:49:25 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[5, 0, 26]}, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=5187702}
May 13, 2024 11:49:25 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:18}] to localhost:27017
XML 데이터가 MongoDB에 삽입되었습니다.
May 13, 2024 11:49:25 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Closed connection [connectionId{localValue:2, serverValue:18}] to localhost:27017 because the pool has been closed.
insert된 데이터 확인
[root@jiyeong-1 mongoDBtest]# mongo
MongoDB shell version v5.0.26
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("55c26273-2302-4245-8a54-777b95491e8f") }
MongoDB server version: 5.0.26
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
2024-04-25T08:28:59.502+00:00: Access control is not enabled for the database.Read and write access to data and configuration is unrestricted
2024-04-25T08:28:59.503+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
2024-04-25T08:28:59.503+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' in this binary version
---
> use sua_database
switched to db sua_database
> show collections
t1
xmlCollection
> db.xmlCollection.find()
{ "_id" : ObjectId("6641fe459bdadf5815c0cfa1"), "root" : { "name" : "John", "age" : 30} }
-- 안되는 쿼리
> db.xmlCollection.find({age:"30"})
> db.xmlCollection.find({"root.age":"30"})
-- 되는 쿼리
> db.xmlCollection.find({"root.age":30})
{ "_id" : ObjectId("6641fe459bdadf5815c0cfa1"), "root" : { "name" : "John", "age" : 30} }
> ^C
bye
java코드
[root@jiyeong-1 mongoDBtest]# cat XmlTest.java
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.json.JSONObject;
import org.json.XML;
public class XmlTest {
public static void main(String[] args) {
// MongoDB에 연결
try (MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"))) {
// 데이터베이스 및 컬렉션 선택
MongoDatabase database = mongoClient.getDatabase("sua_database");
MongoCollection<Document> collection = database.getCollection("xmlCollection");
// 예제 XML 데이터
String xmlData = "<root><name>John</name><age>30</age></root>";
// XML을 JSON으로 변환
JSONObject jsonObject = XML.toJSONObject(xmlData);
String jsonData = jsonObject.toString();
// JSON 데이터를 MongoDB에 삽입
Document doc = Document.parse(jsonData);
collection.insertOne(doc);
System.out.println("XML 데이터가 MongoDB에 삽입되었습니다.");
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
- xml 데이터 변수로 만들어서 json으로 변경한 다음 mongodb에 삽입