锂和锂Mongo插入子文档

时间:2016-04-07 15:05:23

标签: mongodb mongodb-query lithium

处理使用Lithium和Mongo DB的项目。花了一天时间试图在现有文件中插入新的子文件。典型的文档结构是

{
"_id" : ObjectId("55f82265b2fa09a5195a98df"),
"deleted" : NumberLong(0),
"active" : NumberLong(1),
"company_id" : "554876ced85dec8c72a0a7fd",
"name" : "WizzBang",
"description" : "Wizzard hats",
"history" : [ 
    {
        "payment" : "Quarterly",
        "who" : 'nerds'
    }, 
    {
        "payment" : "Monthly",
        "who" : 'sporty'
    }, 
    {
        "payment" : "Yearly",
        "who" : 'stoners'
    }
 ]}

我正在尝试添加额外的'行'使用锂的历史领域。

我可以在Mongo shell中运行以下内容....

db.getCollection('products').update( 
        {_id: ObjectId("55f82265b2fa09a5195a98df") },
        {$push: 
        { history:
                    {
            "payment" : "Quarterly",
            "who" : 'Cheer Leaders'
            }

         }
     },{ upsert: true });

这让我得到了以下......

{
"_id" : ObjectId("55f82265b2fa09a5195a98df"),
"deleted" : NumberLong(0),
"active" : NumberLong(1),
"company_id" : "554876ced85dec8c72a0a7fd",
"name" : "WizzBang",
"description" : "Wizzard hats",
"history" : [ 
    {
        "payment" : "Quarterly",
        "who" : 'nerds'
    }, 
    {
        "payment" : "Monthly",
        "who" : 'sporty'
    }, 
    {
        "payment" : "Yearly",
        "who" : 'stoners'
    },
    {
        "payment" : "Quarterly",
        "who" : 'Cheer Leaders'
    }
 ]}

但是如何用锂电池做到这一点?到目前为止最好的猜测是

 $products = Products::update(
        array( '_id' => $id ),
        array( '$push' => array( 'history' => array('payment'=>'Quarterly', 'who' => 'Cheer Leaders') ) )
        );

我回来了,真的'但没有数据库更改。

提前感谢您的帮助。

0 个答案:

没有答案