简体   繁体   English

pymongo建议的MongoDB推送

[英]MongoDB push with pymongo advice

I'm trying to learn some basic MongoDB and I'm confused about how to use $push to add more data to my document. 我正在尝试学习一些基本的MongoDB,并且对如何使用$ push向文档中添加更多数据感到困惑。 Here's the code that i have: 这是我的代码:

from pymongo import MongoClient
client = MongoClient()
db = client.classes
collection = db.StudentsExample
student1 = {
         'name': 'adam',
         'year': 'sophomore',
         'age': 19,
         'class':[
                  {
                  'className': 'cse131',
                  'time': '2:30',
                  'finalGrade': 'A'
                  },
                  {
                  'className': 'cse240',
                  'time': '9:30',
                  'finalGrade': 'B'
                    }
                  ]
       } 
student2 = {
         'name': 'bob',
         'year': 'sophomore',
         'age': 19,
         'class':[
                  {
                  'className': 'cse131',
                  'time': '2:30',
                  'finalGrade': 'A'
                  },
                  {
                  'className': 'cse240',
                  'time': '9:30',
                  'finalGrade': 'B'
                    }
                  ]
       }

num = int(input("How many more classes?: "))

for x in range(0, num):
    classNameInput = str(input("Class name?: "))
    timeInput = str(input("Time of class?: "))
    finalGradeInput = str(input("Final grade in class?: "))
    db.StudentsExample.update(
        {'name': "adam"},
        {'$push': {'class.className': classNameInput, 'class.time': timeInput, 'class.finalGrade':finalGradeInput}}
        )



cursor = collection.find({})
for document in cursor: print(document)

what i thought this would do is add another document inside the class document in student1. 我认为这将是在student1的班级文档内添加另一个文档。 So for example, if the user had input when prompted: Class name: bio time: 2:30 final grade: C 因此,例如,如果用户在提示时输入了内容:班级名称:生物时间:2:30最终成绩:C

the result would be: 结果将是:

student1 = {
         'name': 'adam',
         'year': 'sophomore',
         'age': 19,
         'class':[
                  {
                  'className': 'cse131',
                  'time': '2:30',
                  'finalGrade': 'A'
                  },
                  {
                  'className': 'cse240',
                  'time': '9:30',
                  'finalGrade': 'B'
                    },
                  {
                   'className': 'bio
                   'time': 2:30
                   'finalGrade': 'C'
                   }
                  ]
       } 
student2 = {
         'name': 'bob',
         'year': 'sophomore',
         'age': 19,
         'class':[
                  {
                  'className': 'cse131',
                  'time': '2:30',
                  'finalGrade': 'A'
                  },
                  {
                  'className': 'cse240',
                  'time': '9:30',
                  'finalGrade': 'B'
                    }

                  ]
       }

I've already inserted both of these elements into the database, so they are in there even though insert method isn't in this code. 我已经将这两个元素都插入到数据库中,因此即使此代码中没有insert方法,它们也位于其中。

However, it's not working-- it gives the error statement "cannot use the part (class of class.time) to traverse the element ({class: [ { finalGrade: "A", time: "2:30", className: "cse131" }, { finalGrade: "B", time: "9:30", className: "cse240" } ]}) 但是,它不起作用-它给出了错误声明“无法使用零件(class.time的类)遍历元素({class:[{finalGrade:“ A”,time:“ 2:30”,className: “ cse131”},{finalGrade:“ B”,time:“ 9:30”,className:“ cse240”}]})

Can anyone tell me what I've done wrong? 谁能告诉我我做错了什么? I can't find clear instructions on how to use push with python.. Thanks! 我找不到有关如何在python中使用push的明确说明。。谢谢!

You're almost there: 你快到了:

Change 更改

{'$push': {'class.className': classNameInput, 'class.time': timeInput, 'class.finalGrade':finalGradeInput}

to

{'$push': {class: {'className': classNameInput, 'time': timeInput, 'finalGrade':finalGradeInput}}

The general format is 通用格式为

{'$push': {<array to push to>: <value to push onto array>}}

If we look at your code, you can see you are currently trying to push classNameInput onto array class.className . 如果我们查看您的代码,您会看到您当前正在尝试将classNameInput推送到数组class.className Mongo then errors out becase class.className is not an array! 然后Mongo会出错,因为class.className不是数组!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM