没有办法做(不
$rename
)引用现有字段时更新文档上的操作--
MongoDB: Updating documents using data from the same document
find
&安培;
each
)重新创建具有正确的\u id类型的文档。要查找要更新的子文档,可以使用
$type
操作员。像这样的查询
db.coll.find({nestedField._id: {$type: 'string' }})
$match
&安培;
$unwind
只获取子文档
db.coll.aggregate([
{ $match: {'nestedField._id': {$type: 'string' }}}, // limiting to documents that have any bad subdocuments
{ $unwind: '$nestedField'}, // creating a separate document in the pipeline for each entry in the array
{ $match: {'nestedField._id': {$type: 'string' }}}, // limiting to only the subdocuments that have bad fields
{ $project: { nestedId: 'nestedField._id' }} // output will be: {_id: documentedId, nestedId }
])
我试图避免在应用服务器上写一系列循环来进行数据库调用-所以我希望直接在“mongo”中使用一些东西
你呢
直接在mongo上运行js代码以避免调用api,但我认为没有任何方法可以避免在文档上循环。