使用mongodb在嵌套对象中推送数组

时间:2017-06-10 05:10:51

标签: mongodb-query

我想通过推入嵌套对象来更新文档:

文档的布局如下:

{
   "id": 456,
   "nickname": "xxx",
   "pf": {
        "id": 123,
        "acc": [
            {
                "accid": 1494235749,
                "status": "New",
                "accnbr": 12345,
                "cyc": [
                   {
                    "cycid": 1494404053,
                    "active": true,
                    "status": "New",
                    "name": "QST192017",
                    "det" : [
                       {
                          "dcycid": 2422,
                          "status": "First"
                       }
                     ]
                   }
                 ],

            },
            {
                "accid": 1494403399,
                "status": "New",
                "accnbr": 915177,
                "cyc": [
                  {
                    "cycleid": 1494406299,
                    "active": true,
                    "status": "Closed",
                    "name": "QSL342014",
                    "det" : [
                       {
                          "dcycid": 2423,
                          "status": "First"
                       }
                     ]
                  }
                ]
            }
        ]
    },
 }

正如我所说,我想在这个结构中推送一个对象,这就是我试图做的事情:

db.getCollection('fxh').updateOne({"id": 456, "pf.acc.accid" : 1494235749, "pf.acc.cyc.cycid": 1494404053 },{  $push : {"pf.acc.$.cyc.x.detail":  { dcycid: 123461, status: "test"}}})

第一个$是acc,但我需要指定cyc的索引。 任何想法怎么做?

我的查询中的Remarque我有3'其中'

1 - id

2 - pf.acc.accid

3 - pf.acc.cyc.cycid

1 个答案:

答案 0 :(得分:0)

根据上述问题中的描述,请尝试在MongoDB shell中执行以下更新操作。

/admin/countries/{countryId}:
    get:
      tags:
        - AdminCountries
      summary: Find a country by ID
      operationId: adminCountriesGetById
      security:
        - Bearer: []
      parameters:
        - in: path
          type: integer
          format: int64
          name: countryId
          required: true
      responses:
        '200':
          description: Success
          schema:
            type: object
            properties:
              id:
                type: integer
                format: int64
              name:
                type: string
                example: The Netherlands
              code:
                type: string
                example: NL
              created_at:
                type: string
                example: 2017-06-01 13:37:00
              updated_at:
                type: string
                example: 2017-06-01 14:00:00