使用mongoDB插入子文档

编程入门 行业动态 更新时间:2024-10-17 15:25:39
本文介绍了使用mongoDB插入子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在集合中有以下文件:

I have the following document in the collection:

"_id" : "2", "workspace" : [{ "name" : "1", "widgets" : [ ] },{ "name" : "2", "widgets" : [ ] },{ "name" : "3", "widgets" : [ ] },{ "name" : "4", "widgets" : [ ] } ]}

如何在小部件中为名称3插入 {id:1,blabla:blabla} ?

How can I insert {id: "1", blabla: "blabla"} in "widgets" for the "name" 3?

推荐答案

与之前的答案相比,只需将所有内容插入到文档的根目录中,这是正确的方法与位置运营商:

In comparison to a previous answer which just inserts everything into the root of the document, here is a correct way to do this with positional operator:

db.t.update({ "_id" : "2", "workspace.name" : "3" },{ $push: { 'workspace.$.widgets' : { id: "2", blabla: "blabla" } } });

更多推荐

使用mongoDB插入子文档

本文发布于:2023-11-22 13:40:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   mongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!