4

I want to add json in this format what I do for this

student{
      nmae:testing
    marks:[{
            subject:{
                class1:2,
                name:testing,
                number:56,
                grade:b,
            },
    {
            subject:{
                class1:2,
                name:testg,
                number:54,
                grade:b,
            }
        }],
}

I was use

db.students.update({"name":"testing"},{$push:{"marks":{"subject":{"class1":1,name:"math","number":12,"garde":"B"}}}});

but it was not work or getting an error. I don't know where I'm wrong please help me

4
  • What about posting the error? Commented Jul 25, 2015 at 14:07
  • this is error "code" : 16837, "errmsg" : "The field 'marks' must be an array but is of type Object in document {_id: ObjectId('55b38136c645304214249b68')}" Commented Jul 25, 2015 at 14:57
  • can you please make this query and post the result? db.students.find({_id: ObjectId('55b38136c645304214249b68')}) Commented Jul 25, 2015 at 17:26
  • after the quering the result is { "_id" : ObjectId("55b38136c645304214249b68"), "name" : "testing", "rollnumber" : "12345", "password" : "testing", "issuebook" : [ ], "marks" : { "subject" : { "class1" : 1, "name" : "math", "number" : 12, "garde" : "B" } }, "__v" : 0, "status" : "Active", "status1" : "Active" } Commented Jul 26, 2015 at 3:54

1 Answer 1

2

As you can see in the resulting document, you have an object as the value of marks fields:

{
    "_id" : ObjectId("55b38136c645304214249b68"), 
    "name" : "testing", 
    "rollnumber" : "12345", 
    "password" : "testing", 
    "issuebook" : [], 
    "marks" : {
        "subject" : {
            "class1" : 1, 
            "name" : "math", 
            "number" : 12, 
            "garde" : "B"
        } 
    }, 
    "__v" : 0, 
    "status" : "Active", 
    "status1" : "Active"
}

So you get the error.

Do the following to recover:

db.student.remove({_id: ObjectId("55b38136c645304214249b68")})
db.student.insert({
        "_id" : ObjectId("55b38136c645304214249b68"), 
        "name" : "testing", 
        "rollnumber" : "12345", 
        "password" : "testing", 
        "issuebook" : [], 
        "marks" : [
            {
                "subject" : {
                    "class1" : 1, 
                    "name" : "math", 
                    "number" : 12, 
                    "garde" : "B"
                }
            }
        ] 
        "__v" : 0, 
        "status" : "Active", 
        "status1" : "Active"
    }
)
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.