0

Hello gurus out there.

I am trying to add subdocuments through a dynamic array. Following is something I want to achieve. Not able to understand how to get array create subdocuments instead of getting it to print JSON code written in double quotes. I understand why it is printing code but not able tell the program to convert the string into subdocuments.

Thank you for looking.

rec = {
    '_id' : 1,
    'class' : 'Python'
}

rec['students'] = "[{'name':'Jack','marks':90},{'name':'Jill', marks: 80}]"

db.class.insert(rec)

1 Answer 1

1

you have to quote keys in your dictionary, key marks in second element of list refer to variable marks which is not defined. Try this:

rec = {
    '_id' : 1,
    'class' : 'Python'
}

rec['students'] = [{'name': 'Jack', 'marks': 90}, {'name': 'Jill', 'marks': 80}]

db['class'].insert(rec)
Sign up to request clarification or add additional context in comments.

1 Comment

It worked !!!!!! Thank you very much for quick help. How stupid of me for not thinking of removing double quotes. Again thank you. Have a good day ahead.

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.