user3067684
user3067684

Reputation: 1258

Lithium & Mongo inserting subdocuments

Working on a project that uses Lithium and Mongo DB. Spent the day trying to insert into existing documents new sub documents. A typical document structure is

{
"_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'
    }
 ]}

I am trying to add additional 'rows' to the history field using Lithium.

I can run the following in a Mongo shell....

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

         }
     },{ upsert: true });

That gets me the following....

{
"_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'
    }
 ]}

But how to do this with Lithium? Best guess so far is

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

I get back 'true' but no database changes.

Thanks in advance for any help.

Upvotes: 0

Views: 58

Answers (0)

Related Questions