Coronium LS MongoDB - Edit embedded documents

Is there an easy way to edit embedded MongoDB documents using Coronium LS?

Here is a lua version of what I want to do:

--Let's say I had this table/document in mongoDB dataTable = {data1="None",data2={info1="None",info2="None"}} --I want to do the following (using mongo with Coronium LS) dataTable.data2.info1 = "New Info" --or dataTable["data2"]["info1"] = "New Info"

I’ve tried to issue commands like

db.collection.update( { \<query selector\> }, { \<update operator\>: { "array.$.field" : value } } )

However, I can’t get it to work. I’m thinking my syntax might be wrong, though I’ve tried a few different things. An example (with Coronum LS) that will run (No internal error), but mongo can’t process (-1 as error returned) is:

db.collection.update:({ \_id=idNum}, {["$set"] ={["data2.$.info1"]= "New Info" }})

Can anyone help?

Try typing in cc logs in your ssh and spit the results here

2016/08/09 07:21:01 [error] 1923#0: \*44 lua entry thread aborted: runtime error: error loading module 'testApp.api' from file '/usr/local/cloud/apps/testApp/api.lua': &nbsp;&nbsp; &nbsp;/usr/local/cloud/apps/testApp/api.lua:45: ']' expected near 'info1' stack traceback: coroutine 0: &nbsp;&nbsp; &nbsp;[C]: in function 'require' &nbsp;&nbsp; &nbsp;/usr/local/cloud/lualib/cloud/input.lua:109: in function 'request' &nbsp;&nbsp; &nbsp;content\_by\_lua(cloud\_server.conf:116):5: in function \<content\_by\_lua(cloud\_server.conf:116):1\>, client: 192.168.71.1, server: localhost, request: "POST /testApp/updateData HTTP/1.1", host: "192.168.71.73" 2016/08/09 07:22:11 [error] 1923#0: \*45 lua entry thread aborted: runtime error: /usr/local/cloud/apps/testApp/api.lua:45: attempt to call global 'data2' (a nil value) stack traceback: coroutine 0: &nbsp;&nbsp; &nbsp;/usr/local/cloud/apps/testApp/api.lua: in function 'api\_func' &nbsp;&nbsp; &nbsp;/usr/local/cloud/lualib/cloud/input.lua:149: in function 'request' &nbsp;&nbsp; &nbsp;content\_by\_lua(cloud\_server.conf:116):5: in function \<content\_by\_lua(cloud\_server.conf:116):1\>, client: 192.168.71.1, server: localhost, request: "POST /testApp/updateData HTTP/1.1", host: "192.168.71.73" 2016/08/09 07:23:17 [error] 1923#0: \*47 lua entry thread aborted: runtime error: error loading module 'testApp.api' from file '/usr/local/cloud/apps/testApp/api.lua': &nbsp;&nbsp; &nbsp;/usr/local/cloud/apps/testApp/api.lua:45: '}' expected near 'info1' stack traceback: coroutine 0: &nbsp;&nbsp; &nbsp;[C]: in function 'require' &nbsp;&nbsp; &nbsp;/usr/local/cloud/lualib/cloud/input.lua:109: in function 'request' &nbsp;&nbsp; &nbsp;content\_by\_lua(cloud\_server.conf:116):5: in function \<content\_by\_lua(cloud\_server.conf:116):1\>, client: 192.168.71.1, server: localhost, request: "POST /testApp/updateData HTTP/1.1", host: "192.168.71.73"

The last one should be the same as the one I put in the post. Not sure about the others. Line 45 is the line I call the update method.

Steven, 

Thanks for posting this in the forums so all could benefit from the question.

Looking at your data here 

--Let's say I had this table/document in mongoDB dataTable = {data1="None",data2={info1="None",info2="None"}}

I think you have the wrong syntax in your selector as data2 is not an array but an object with info1 and info2 as its properties. Try this and see if it works for you.

db.{{collection}}:update( { \_id=idNum, data2.info1="New Info" } )

Thanks for the help. I figured out how it works now and I’ll share what I learned below.

When I ran the command

db.{{collection}}:update( { \_id=idNum, data2.info1="New Info" } )

I got the error

2016/08/09 23:05:53 [error] 1923#0: \*51 lua entry thread aborted: runtime error: error loading module 'testApp.api' from file '/usr/local/cloud/apps/testApp/api.lua': /usr/local/cloud/apps/testApp/api.lua:45: '}' expected near '=' stack traceback: coroutine 0: [C]: in function 'require' /usr/local/cloud/lualib/cloud/input.lua:109: in function 'request' content\_by\_lua(cloud\_server.conf:116):5: in function \<content\_by\_lua(cloud\_server.conf:116):1\>, client: 192.168.71.1, server: localhost, request: "POST /testApp/updateData HTTP/1.1", host: “192.168.71.73"

In addition, I couldn’t get even this to run (same error)
 

db.{{collection}}:update( { \_id=idNum, data1=“New Info”} )

As far as I can tell the example in the MongoDB Corundum LS Docs has a typo, though I think the description above it is accurate. It seems that the “update” method requires 2 tables, one query and one update. However the example only uses one table, which I couldn’t get to work. For Docs reference, if we assume the player=“Sally” exists, then the input could be {player=“Sally”},{player=“Sally”, score = 300}.

In addition, it is worth noting that a command like

db.{{collection}}:update( { \_id=idNum}, updateTable )

Replaces the entire document with updateTable. For example

—Let’s say we have this table in the “data” collection {\_id=1,data1=“None”,data2=“None”} —If we call local updateTable = {data1=“New Info"} db.data:update( { \_id=1}, updateTable ) —Our new table will be {\_id=1,data1=“New Info”}

If instead you want to replace the value of data1 then you need to use a command like this

db.data:update({ \_id=1}, {["$set"]= {data1="New Info"}})

Putting Steven Warren’s suggestion together with that information, we can replace subtable data as follows:

—Let’s say we have this table in the “data” collection {\_id=1,data1=“None”,data2=“None”,data3={data1="None"}} —Let's replace the data inside the subtable data3 --Note that ["data3.data1"] must be used instead of data3.data1 --for the system to interpret the "." correctly db.data:update({ \_id=1}, {["$set"]= {["data3.data1"]="New Info"}}) —Our new table will be the desired table {\_id=1,data1=“None”,data2=“None”,data3={data1="New Info"}}

I hope this helps anyone else who may have been confused.

Try typing in cc logs in your ssh and spit the results here

2016/08/09 07:21:01 [error] 1923#0: \*44 lua entry thread aborted: runtime error: error loading module 'testApp.api' from file '/usr/local/cloud/apps/testApp/api.lua': &nbsp;&nbsp; &nbsp;/usr/local/cloud/apps/testApp/api.lua:45: ']' expected near 'info1' stack traceback: coroutine 0: &nbsp;&nbsp; &nbsp;[C]: in function 'require' &nbsp;&nbsp; &nbsp;/usr/local/cloud/lualib/cloud/input.lua:109: in function 'request' &nbsp;&nbsp; &nbsp;content\_by\_lua(cloud\_server.conf:116):5: in function \<content\_by\_lua(cloud\_server.conf:116):1\>, client: 192.168.71.1, server: localhost, request: "POST /testApp/updateData HTTP/1.1", host: "192.168.71.73" 2016/08/09 07:22:11 [error] 1923#0: \*45 lua entry thread aborted: runtime error: /usr/local/cloud/apps/testApp/api.lua:45: attempt to call global 'data2' (a nil value) stack traceback: coroutine 0: &nbsp;&nbsp; &nbsp;/usr/local/cloud/apps/testApp/api.lua: in function 'api\_func' &nbsp;&nbsp; &nbsp;/usr/local/cloud/lualib/cloud/input.lua:149: in function 'request' &nbsp;&nbsp; &nbsp;content\_by\_lua(cloud\_server.conf:116):5: in function \<content\_by\_lua(cloud\_server.conf:116):1\>, client: 192.168.71.1, server: localhost, request: "POST /testApp/updateData HTTP/1.1", host: "192.168.71.73" 2016/08/09 07:23:17 [error] 1923#0: \*47 lua entry thread aborted: runtime error: error loading module 'testApp.api' from file '/usr/local/cloud/apps/testApp/api.lua': &nbsp;&nbsp; &nbsp;/usr/local/cloud/apps/testApp/api.lua:45: '}' expected near 'info1' stack traceback: coroutine 0: &nbsp;&nbsp; &nbsp;[C]: in function 'require' &nbsp;&nbsp; &nbsp;/usr/local/cloud/lualib/cloud/input.lua:109: in function 'request' &nbsp;&nbsp; &nbsp;content\_by\_lua(cloud\_server.conf:116):5: in function \<content\_by\_lua(cloud\_server.conf:116):1\>, client: 192.168.71.1, server: localhost, request: "POST /testApp/updateData HTTP/1.1", host: "192.168.71.73"

The last one should be the same as the one I put in the post. Not sure about the others. Line 45 is the line I call the update method.

Steven, 

Thanks for posting this in the forums so all could benefit from the question.

Looking at your data here 

--Let's say I had this table/document in mongoDB dataTable = {data1="None",data2={info1="None",info2="None"}}

I think you have the wrong syntax in your selector as data2 is not an array but an object with info1 and info2 as its properties. Try this and see if it works for you.

db.{{collection}}:update( { \_id=idNum, data2.info1="New Info" } )

Thanks for the help. I figured out how it works now and I’ll share what I learned below.

When I ran the command

db.{{collection}}:update( { \_id=idNum, data2.info1="New Info" } )

I got the error

2016/08/09 23:05:53 [error] 1923#0: \*51 lua entry thread aborted: runtime error: error loading module 'testApp.api' from file '/usr/local/cloud/apps/testApp/api.lua': /usr/local/cloud/apps/testApp/api.lua:45: '}' expected near '=' stack traceback: coroutine 0: [C]: in function 'require' /usr/local/cloud/lualib/cloud/input.lua:109: in function 'request' content\_by\_lua(cloud\_server.conf:116):5: in function \<content\_by\_lua(cloud\_server.conf:116):1\>, client: 192.168.71.1, server: localhost, request: "POST /testApp/updateData HTTP/1.1", host: “192.168.71.73"

In addition, I couldn’t get even this to run (same error)
 

db.{{collection}}:update( { \_id=idNum, data1=“New Info”} )

As far as I can tell the example in the MongoDB Corundum LS Docs has a typo, though I think the description above it is accurate. It seems that the “update” method requires 2 tables, one query and one update. However the example only uses one table, which I couldn’t get to work. For Docs reference, if we assume the player=“Sally” exists, then the input could be {player=“Sally”},{player=“Sally”, score = 300}.

In addition, it is worth noting that a command like

db.{{collection}}:update( { \_id=idNum}, updateTable )

Replaces the entire document with updateTable. For example

—Let’s say we have this table in the “data” collection {\_id=1,data1=“None”,data2=“None”} —If we call local updateTable = {data1=“New Info"} db.data:update( { \_id=1}, updateTable ) —Our new table will be {\_id=1,data1=“New Info”}

If instead you want to replace the value of data1 then you need to use a command like this

db.data:update({ \_id=1}, {["$set"]= {data1="New Info"}})

Putting Steven Warren’s suggestion together with that information, we can replace subtable data as follows:

—Let’s say we have this table in the “data” collection {\_id=1,data1=“None”,data2=“None”,data3={data1="None"}} —Let's replace the data inside the subtable data3 --Note that ["data3.data1"] must be used instead of data3.data1 --for the system to interpret the "." correctly db.data:update({ \_id=1}, {["$set"]= {["data3.data1"]="New Info"}}) —Our new table will be the desired table {\_id=1,data1=“None”,data2=“None”,data3={data1="New Info"}}

I hope this helps anyone else who may have been confused.