更新MongoDB中数组中嵌套对象数组中的嵌套对象数组中的值

时间:2016-06-09 20:20:29

标签: node.js mongodb

{
  _id: ObjectId("5757646866e59fac8d1d8589"),
  updatedAt: ISODate("2016-06-09T02:49:09.792Z"),
  createdAt: ISODate("2016-06-08T00:18:48.147Z"),
  books: [
    {
      name: "book1",
      id: "1",
      chapters: [
        {
          id: "1",
          name: "CH1",
          details: "Chapter 1",
          tables: [
            {
              table : 1, 
              heading : 'heading 1' // update this
            },
            {
              table: 2,
              heading: 'heading 2'
            }
          ]
        }
      ]
    }
  ]
}

如何以编程方式更新表1的标题。

这是一种静态的方法:

db.books.update({_id: ObjectId("5757646866e59fac8d1d8589")},
{$set: {"books.0.chapters.0.tables.0.heading": "new Heading"}})

我想做的是这样的事情:

db.books.update({_id: ObjectId("5757646866e59fac8d1d8589"), 
  'books.id': '1',
  'books.chapters.id':'1', 
  'books.chapters.tables.table':'1' }, 
{$set: {<what>}})

0 个答案:

没有答案
相关问题