使用JAVA将单个字段更新为mongodb中的数组

时间:2015-07-07 11:43:51

标签: arrays mongodb mongodb-query

我有以下格式的文件:

{
        "_id" : ObjectId("559bae5df3fcf46e468a9779"),
        "CPF" : "129-87-716",
        "NAME" : "Donald Carver",
        "CONTACT" : 1827626847,
        "ADDRESS" : "937 Hamburg Path",
        "EMAIL_ADDRESS" : "constpherd@yah00.net",
        "CARDS" : {
                "CREDIT_CARD_NUMBER" : "1678-768-420-4188",
                "CARD_TYPE" : "IVISA",
                "EXPIRY_DATE" : "Mon May 21 15:23:53 IST 2018"
        }
}

我想更新一个文档数组。

"TRANSACTIONS":[{"TRANSACTION_NUMBER":"1","TRANSACTION_DATE":"07/07/2015","BANK_ID":"200","AMOUNT":100},
                {"TRANSACTION_NUMBER":"2","TRANSACTION_DATE":"08/07/2015","BANK_ID":"0","AMOUNT":200},
                {"TRANSACTION_NUMBER":"3","TRANSACTION_DATE":"06/07/2015","BANK_ID":"200","AMOUNT":100}
                       ]
       }

它应该如下所示:

{
        "_id" : ObjectId("559bae5df3fcf46e468a9779"),
        "CPF" : "129-87-716",
        "NAME" : "Donald Carver",
        "CONTACT" : 1827626847,
        "ADDRESS" : "937 Hamburg Path",
        "EMAIL_ADDRESS" : "constpherd@yah00.net",
        "CARDS" : {
                "CREDIT_CARD_NUMBER" : "1678-768-420-4188",
                "CARD_TYPE" : "IVISA",
                "EXPIRY_DATE" : "Mon May 21 15:23:53 IST 2018"
        }
        "TRANSACTIONS":[{"TRANSACTION_NUMBER":"1","TRANSACTION_DATE":"07/07/2015","BANK_ID":"200","AMOUNT":100},
                        {"TRANSACTION_NUMBER":"2","TRANSACTION_DATE":"08/07/2015","BANK_ID":"0","AMOUNT":200},
                        {"TRANSACTION_NUMBER":"3","TRANSACTION_DATE":"06/07/2015","BANK_ID":"200","AMOUNT":100}
                       ]
       }
}

这就是我试图用java做的事情:

String[] creditcard = line.split(cvsSplitBy);



        DBObject listItem = new BasicDBObject("TRANSACTION", new BasicDBObject("TRANSACTION_NUMBER",creditcard[1])
                                                                       .append("TRANSACTION_DATE",creditcard[2])
                                                                       .append("TRANSACTION_TYPE",creditcard[3])
                                                                       .append("BANK_ID",creditcard[4])
                                                                       .append("AMOUNT",creditcard[5])
                                                                                );


        DBObject updateQuery = new BasicDBObject("$push", listItem);
        userimage.update(new BasicDBObject().append("CARDS.CREDIT_CARD_NUMBER", creditcard[0]), updateQuery);

当我第一次运行时,程序会更新一行。当我第二次运行程序时,程序不会将任何文档插入到数组中...... 我无法找到程序中的逻辑错误..

0 个答案:

没有答案