如何使用java在MongoDB中的嵌入式文档中插入嵌入式文档

时间:2017-08-08 12:47:38

标签: java mongodb

我有一个集合,我想在其中使用java添加嵌入式文档。我能够添加普通文档。但是如何添加嵌入式文档。集合的结构应该是这样的:

{
"_id" : ObjectId("591a93f7f0bf98b7253bf187"),
"INTEGRATION_ID" : null,
"STATUS_CD" : "Open",
"X_CUST_REF" : "AUTO ECOSSE",
"REQ_SHIP_DT" : "2014-03-21:00:00:00",
"QUOTE_ID" : null,
"ACCNT_ID" : "1-TGCG-1",
"ACTIVE_FLG" : "WQ==",
"PROCESS_TIMESTAMP" : "2017-04-19:15:27:34.786214000",
"CONTACT_ID" : "2-16D8S576",
"BU_ID" : "1-ENQCIQ",
"SHIP_CON_ID" : "No Match Row Id",
"LAST_UPD" : "2014-03-20:00:00:00",
"X_CLOSE_DT" : null,
"X_SUB_STAT" : "In Progress",
"ORDER_NUM" : "BTAQZ635",
"SOFT_DELETE" : "N",
"ROW_ID" : "2-16R75TLQ",
"LAST_UPD_BY" : "1-2SEO0FD",
"REV_NUM" : "1",
"ORDER_DT" : "2014-03-20:00:00:00",
"ORDERLINE" : [ 
    {
        "ASSET_ID" : null,
        "SERV_ACCNT_ID" : null,
        "REQ_SHIP_DT" : "2014-03-21:00:00:00",
        "X_PROD_DESC" : "Mongo Kafka",
        "SHIP_CON_ID" : null,
        "X_BES_STATUS" : "Not Possible To Be Fulfiled",
        "ROW_ID" : "2-16R75TM0",
        "STATUS_CD" : "FAILED",
        "ORDER_ID" : "2-16R75TLQ",
        "COMPLETED_DT" : null,
        "LAST_UPD" : "2014-03-20:00:00:00",
        "SOFT_DELETE" : "N",
        "INTEGRATION_ID" : "BTAQZ6351-1",
        "X_CDD" : "2014-03-21:00:00:00",
        "ACTION_CD" : "Add",
        "X_ORDER_ITEM_SUBSTATUS" : "Failed",
        "X_APPT_REF" : null,
        "X_CANCELLED_DT" : null,
        "PROD_ID" : "1-7OSVB0F",
        "SERVICE_NUM" : "01382459732",
        "MUST_DLVR_BY_DT" : null,
        "ROLLUP_FLG" : "N",
        "ROOT_ORDER_ITEM_ID" : "2-16R75TM0",
        "BILL_ACCNT_ID" : "2-1Y2ZD-101",
        "PROCESS_TIMESTAMP" : "2016-03-28:15:09:06.274774000",
        "QTY_REQ" : "1"
    }
]

}

如何在我的集合中获取ORDERLINE嵌入式数组?

1 个答案:

答案 0 :(得分:1)

连接数据库

   Mongo mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("yourdb");

获取/创建集合并添加父数据

DBCollection collection = db.getCollection("dummyColl");

BasicDBObject document = new BasicDBObject();
document.put("INTEGRATION_ID", "mkyongDB");
document.put("STATUS_CD" : "Open",);
........
........

将子集合推送到对象

BasicDBObject OrderLineDetail = new BasicDBObject();

OrderLineDetail .put("ASSET_ID", null);
OrderLineDetail .put("SERV_ACCNT_ID", null);
OrderLineDetail .put("REQ_SHIP_DT", "Mongo Kafka");

document.put("ORDERLINE", OrderLineDetail );

保存数据

collection.insert(document);