通过mongoose为mongodb中collection的文档中的每个嵌套对象创建id

问题描述

我有一个用户架构。保存文档时,对于文档中的每个嵌套对象(quizHistory,记录和响应),猫鼬会自动添加_id字段。 For ref- quizHistory path

for i in cur:
    print(i)
    age = int(input("age for "+i[0]))
    query = 'INSERT INTO student (student_name,age) values (%s,%d);'
    val = (i[0],age)
    cur.execute(query,val)
    con.commit()


student_name was already in the table.

query = 'INSERT INTO student (student_name,age) values ({0},{1}) on duplicate key update student_name = {0},age = {1};'.format(i[0],age)

解决方法

猫鼬默认情况下会创建虚拟ID(guide id)。 将此行添加到您的架构。

 _id : {id:false}