【问题标题】:Can't insert object into object array in Mongodb无法将对象插入到MongoDB中的对象数组中
【发布时间】:2013-10-03 13:14:23
【问题描述】:

我一直试图在这段代码中加入某些元素。 (请忽略愚蠢的虚拟数据)

{
"Order_Number": "1",
"Status": "Pending",
"Customer":{
    "First_Name": "Juan",
    "Second_Name": "De La Cruz",
    "Email": "juandelacruz@gmail.com",
    "contact": "0923275937"
           },
"Shipping_Address": "291,Ominous Building, Quezon City, Philippines",
"Billing_Address": "IOU, Iowa, USA",
"Weight": "200 lbs.",
"Description": "Joots",
"Tracking Information": {
    "Tracking Number": "000000001",
    "Shipping History": {
        "Shipping Transactions": [
            {
                "Date Received": "10/1/2013",
                "Date Released": "10/10/2013",
                "Source": "Center of the earth",
                "Control Number": "100010001"
            },
            {
                "Date Received": "10/2/2013",
                "Date Released": "10/12/2013",
                "Source": "Center of Venus",
                "Control Number": "100010002"
            },
            {
                "Date Received": "10/3/2013",
                "Date Released": "10/13/2013",
                "Source": "San Francisco",
                "Control Number": "100010003"
            }
        ]
    }
}
}

现在,我一直在尝试将这些元素纳入“航运交易”。

{

        "Date Received": "10/2/2013",
        "Date Released": "10/11/2013",
        "Source": "Center of Mars",
        "Control Number": "100010005"
}

我尝试过 push ,但它甚至似乎都没有将自己添加到数组中。

{$push:{"Tracking Information":{"Shipping History":{"Shipping Transactions":
   [{"Date Received": "10/4/2013",
    "Date Released": "10/11/2013",
    "Source": "Center of Mars",
    "Control Number": "100010005"}]
 }

这段代码可能有缺陷还是我做错了什么?顺便说一句,我只使用 MongoDb 的 web shell。

【问题讨论】:

    标签: arrays mongodb push


    【解决方案1】:

    应该这样做:

    {
      $push:{"Tracking Information.Shipping History.Shipping Transactions": {"Date Received": "10/4/2013",
        "Date Released": "10/11/2013",
        "Source": "Center of Mars",
        "Control Number": "100010005"}}
    }
    

    它叫做dot notation。真的很有用。

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 1970-01-01
      • 2020-07-16
      • 2019-01-17
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      相关资源
      最近更新 更多