mongodb - 如何更新嵌套数组以向每个元素添加唯一的 ObjectId?
问题描述
我尝试了 2 种方法,但都没有奏效
db.exams.updateMany({},{$set: {"exams.$[].questions.$[].id" : new ObjectId() } })
和
db.exams.aggregate([
{$unwind:"$exams"},
{$unwind:"$exams.questions"},
{$unwind:"$exams.questions.question"},
{$set:{"exams.questions.id" : new ObjectId() }}
])
我不断为所有元素获得相同的 ObjectId,但我希望它为每个元素生成一个新的 objectId
解决方案
像这样的东西应该工作。我没有测试过...
db.exams.aggregate([
{$map: {$input: "exams.questions.id", as: "id" in: new ObjectId()} },
{$out: "your collection"}
])
推荐阅读
- python - 在破折号中,选择单个单选按钮时,如何使用回调更新图形?
- python - 线性回归模型(使用梯度下降)不收敛于波士顿住房数据集
- reverse-proxy - Firebase 托管是否需要反向代理?
- c - 如何像 Linux 命令 ls 一样格式化输出以打印
- html - 如何在横幅上显示文本叠加图像
- r - 在不知道变量名称的情况下将数据框重塑为三列宽
- php - 创建一个目录,将另一个目录的文件复制到其中,然后在 phpMyAdmin 中创建一个表?
- javascript - 如何从 $.post() 请求中获取数据?
- ionic-framework - Ionic 4选项卡,编译失败
- javascript - 一组项目的总数是多少?