2

Hello all am working on mongodb with java, i have a scenario where if the outer document is matched then i have to update/add count in an nested array for eg i want to do something like this :

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

if again i send a document with same fields like id:1, type:a,mark1:1,mark2:2 then i have to get my document as

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

But i am getting some thing like this :

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

        }
}`

My java code is

 `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")));
        }
        ` 

i know we cannot have duplicate keys i tried $set and $push as well but i am stuck. Any help ?

3
  • problem is you are "setting" the field with $set. You should push with $push operator. The $set operator replaces the value of a field with the specified value. The $push operator appends a specified value to an array. Commented Aug 21, 2015 at 13:04
  • No i am not able to do with $push either, i tried with both $set and $push. Commented Aug 21, 2015 at 13:08
  • 2
    It is not about "No, I am not able". You are. But, you should use an array. "count" filed is not array. That is why push is not working. stackoverflow.com/questions/10522347/… Commented Aug 21, 2015 at 13:19

1 Answer 1

4

The main issue is that, according to the code, the "count" key is an object not an array. So the correct JSON representation is:

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

Note the square brackets "[]" after the "count" key.

I had the same question regarding the new mongo java driver (3.0). Here is how I solved it:

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);
}

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.