如何使用java在mongodb中添加嵌套数组中的元素

时间:2015-08-21 12:59:03

标签: java mongodb mongodb-query

大家好我正在使用java工作mongodb,我有一个场景,如果外部文档匹配,那么我必须在嵌套数组中更新/添加计数,例如我想做这样的事情:

`"_id" : ObjectId("55d71603aed7562284e5df95"),
"id" : "1",
"type" : "a",
"score" : {
        "mark1" : "1",
        "mark2" : "2",
        "count" : { "one","two"                
        }
}`

如果我再次发送一个文件,其中包含相同的字段,如id:1,请输入:a,mark1:1,mark2:2然后我必须将我的文档作为

`"_id" : ObjectId("55d71603aed7562284e5df95"),
"id" : "1",
"type" : "a",
"score" : {
        "mark1" : "1",
        "mark2" : "2",
        "count" : { "one","two","three"                
        }
}`

但我得到的是这样的事情:

`"_id" : ObjectId("55d71e42aed7560e8c9d02e4"),
"id" : "1",
"type" : "a",
"score" : {
        "mark1" : "1",
        "mark2" : "2",
        "count" : {
                "count" : "one",

        }
}`

我的java代码是

 `mongoDatabase=mongoClient.getDatabase("TestNestedInsert");
        Document sourceDocument=mongoDatabase.getCollection("entity").find(new Document("id",1).append("score.mark1", "1").append("score.mark2", "2")).first();
        if(sourceDocument==null){
            Document entity=new Document();
            entity.append("id", "1");
            entity.append("type", "a");
            entity.append("score", new Document("mark1","1").append("mark2", "2").append("count", new Document("count","one")));
            mongoDatabase.getCollection("entity").insertOne(entity);
        }
        else{
            mongoDatabase.getCollection("entity").findOneAndUpdate(new Document("id",1).append("score.mark1", "1").append("score.mark2", "2"), new Document("$set",new Document("score.count","three")));
        }
        ` 

我知道我们不能重复按键我试过$ set和$ push但是我被卡住了。有什么帮助吗?

1 个答案:

答案 0 :(得分:3)

主要问题是,根据代码," count" key是一个对象而不是一个数组。所以正确的JSON表示是:

{
    "_id": ObjectId("55d71603aed7562284e5df95"),
    "id": "1",
    "type": "a",
    "score": {
        "mark1": "1",
        "mark2": "2",
        "count": [
            "one",
            "two",
            "three"
        ]
    }
}

注意方括号" []"在"计数"之后键。

我对新的mongo java驱动程序(3.0)有同样的问题。 以下是我解决它的方法:

MongoDatabase mongoDatabase = mongoClient.getDatabase("TestNestedInsert");
MongoCollection<Document> entityCollection = mongoDatabase.getCollection("entity");

Document queryDocument = new Document("id", 1).append("score.mark1", "1").append("score.mark2", "2");
Document sourceDocument = entityCollection.find(queryDocument).first();

if (sourceDocument == null) {
    Document entity = new Document();
    entity.append("id", "1");
    entity.append("type", "a");

    // Create the score nested object
    String[] countArray = { "one", "two" }; // create the count array
    Document scoreObject = new Document()
            .append("mark1", "1")
            .append("mark2", "2")
            .append("count", countArray); // append the count array

    entity.append("score", scoreObject); // append the score object.
    entityCollection.insertOne(entity);
}
else{
    // push the new element to the count array.
    // see: https://docs.mongodb.org/v3.0/reference/operator/update/push/#append-a-value-to-an-array
    Document elementToArray = new Document("score.count", "three");
    Document pushElement = new Document("$push", elementToArray);
    entityCollection.updateOne(queryDocument, pushElement);
}

希望它有所帮助!