Suppose we have:
1 2 3 4 5 6 7 8 9
You can store the matrix as an inline array in mongodb in different ways:
1.Represent matrix as a one-dimensional array and save it as follows:
{ _id: "1", matrix: [1,2,3,4,5,6,7,8,9], width: 3, // or store just size in case of NxN height: 3, }
Then, to increase the third matrix element, you need the following update:
db.matrix.update({_id: 1}, { $inc : { "matrix.2" : 1 } }
This approach is very easy, because you save as little data as possible, but you always need to calculate the position of the element for updating, and you will need to write additional code to deserialize the matrix in your driver.
2. Store the matrix as follows:
{ _id: "1", matrix: [ {xy: "0-0", v: 1}, {xy: "1-0", v: 2}, {xy: "2-0", v: 3}, {xy: "0-1", v: 4}, ... ] }
Then, to increase the third element of the first row in the matrix, you need the following update:
db.matrix.update({_id: 1, "matrix.xy": 2-0 }, { $inc : { "matrix.$.v" : 1 } }
This approach should be easier on the driver side, but you will need to store more information in the database.
Choose what you like best.