InvalidDocument:无法编码对象:

时间:2022-12-03 08:40:43

I'm trying to store one documents objectID into another as an attribute (linking) but mongo keeps giving me this error. What is wrong with this line's syntax?

我试图将一个文档作为一个属性(链接)存储到另一个文档中,但是mongo一直给我这个错误。这一行的语法有什么问题?

for u in self.request.db.lyrics.find():
    u['forSong'] = self.request.db.song.find({}, {'_id': 1})
    self.request.db.lyrics.save(u)

1 个解决方案

#1


3  

The problem is that the result of find method is a cursor, not a list of objects

问题是,find方法的结果是一个游标,而不是一个对象列表。

u['forSong'] = self.request.db.song.find({}, {'_id': 1})

is cursor, not an object. So you must convert returned cursor to list for doing your task:

是游标,不是对象。因此,必须将返回的游标转换为执行任务的列表:

u['forSong'] = list(self.request.db.song.find({}, {'_id': 1}))

That will save list of dicts like {'_id': object-id} into "forSong" field. To actually receive list of object ids you must make further conversion, e.g:

这将保存像{'_id': object-id} into“forSong”字段的列表。要实际接收对象id列表,必须进行进一步的转换,例如:

from operator import itemgetter
...
u['forSong'] = map(itemgetter('_id'),
                   list(self.request.db.song.find({}, {'_id': 1})))

#1


3  

The problem is that the result of find method is a cursor, not a list of objects

问题是,find方法的结果是一个游标,而不是一个对象列表。

u['forSong'] = self.request.db.song.find({}, {'_id': 1})

is cursor, not an object. So you must convert returned cursor to list for doing your task:

是游标,不是对象。因此,必须将返回的游标转换为执行任务的列表:

u['forSong'] = list(self.request.db.song.find({}, {'_id': 1}))

That will save list of dicts like {'_id': object-id} into "forSong" field. To actually receive list of object ids you must make further conversion, e.g:

这将保存像{'_id': object-id} into“forSong”字段的列表。要实际接收对象id列表,必须进行进一步的转换,例如:

from operator import itemgetter
...
u['forSong'] = map(itemgetter('_id'),
                   list(self.request.db.song.find({}, {'_id': 1})))