javascript - 插入mongodb中的一系列文件

标签 javascript arrays json mongodb mongoose

使用哪个运算符将新问题推送到问题数组中?我得到了以下格式的文档。使用哪个 mongo db 运算符将新问题插入到问题数组中,前提是我在提前。

例如,我想在 subchapterId“1”中插入一个新问题。

  {

    "chapterId": "38",
    "subChapter": [
      {

        "subchapterId": "1",
        "questions": [
          {

            "title": "Either his however modern. Stop central owner color type out. Interview five beyond interesting type suddenly.",

          },
          {

            "title": "Amount himself foreign color moment gun together sit. Deal race range heart despite several. Rather activity eat dinner save mission western. Civil past public enter four then.",

          },
          {

            "title": "Four former operation. Class continue away treatment.\nResponsibility condition dinner realize everything. Sign scene order quality yet. Within sing statement skill popular southern whole."

          },
          {

            "title": "Where of coach nature ask page allow.\nType exist hotel time. Central site policy everyone safe. Official administration family somebody.",

          },
          {

            "title": "Necessary dark these much region. Form sometimes seek. Future according detail piece section.\nNear everything admit. Senior Republican draw as expert market.",

          }
        ]
      },
      {

        "subchapterId": "2",
        "questions": [
          {

            "title": "Audience still use group. Yourself building police. Play imagine serious reality population reach.\nHerself without member must think concern window finish."

          },


          {

            "title": "Rule trip manage still. Imagine religious above race something successful.\nOnce base American series. Low page quite allow. Customer maybe base leave way under blood.",

          },
          {

            "title": "Church audience anyone garden. Federal when individual style.Value billion morning need box whether. Coach traditional cold each us truth.",

          }
        ]
      }
    ]
  }

最佳答案

你可以试试这个

const title = "this is the new question"

db.collection.update(
  { "subChapter.subchapterId": "1" },
  { "$push": { "subChapter.$.questions": { title } }}
)

关于javascript - 插入mongodb中的一系列文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51146516/

相关文章:

javascript - 为什么我们可以在不指定 window 对象的情况下访问 window.document 属性?

javascript - 用于打印的 Drupal 弹出窗口

java - 在 jython 中为 java interop 创建数组数组

json - jq 从两个键中获取唯一值

javascript - 如何根据所使用的 URL 使用 javascript 更改页面内容?

javascript - Angular 2 中的绑定(bind)数据

arrays - 解码 map 和 array json

javascript - 需要修复 JavaScript 以便数组从正确的位置开始

json - 在 Swift 中将一个 JSON 对象作为值发布

javascript - 在 Highstock 中加载 JSON 时出现问题