Question

Some document has the following schema:

{
  doc: {
    matrix: [
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
     [{..}, {..}, {..}, {..}, {..}, {..}, {..},],
    ]
  }
}

Can I remove the column with the index = 2 from doc.matrix by using only one query?

I have a solution of this task that uses N queries (where N = count of rows in the matrix). But this solution is not optimum..

Perhaps you can suggest another schema to solve this task.

Suggestions like 'use a SQL database' will be ignored :)

Any ideas?

Was it helpful?

Solution

So I guess what you are referring to is the second "column" as opposed to the "row". Considering the following form which is a little better for illustration:

{
    "matrix": [
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
        [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
    ]
}

So the one query form is really:

db.matrix.update(
    {},
    {
        "$pull": {
            "matrix.0": 2,
            "matrix.1": 2,
            "matrix.2": 2,
            "matrix.3": 2,
            "matrix.4": 2,
            "matrix.5": 2,
            "matrix.6": 2,
            "matrix.7": 2,
            "matrix.8": 2,
            "matrix.9": 2,
        }
    }
)

Which is presumably something like what you refer to doing in "N" statments but this just does it on one:

{
    "matrix" : [ 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ], 
        [ 0, 1, 3, 4, 5, 6, 7, 8, 9 ] 
    ] 
}

So of course your whole "index of" preposition defines that you have some way to identify which element that actually is within the sub-document, but the basic premise stays the same.

So it generally is up to you have such identifiers in your sub-documents, or in a wider sense, something along these lines:

{
    "matrix": [
        [
            { "index": 0, "ident": "A" },
            { "index": 1, "ident": "B" },
            { "index": 2, "ident": "C" }
        ],
        [
            { "index": 0, "ident": "A" },
            { "index": 1, "ident": "B" },
            { "index": 2, "ident": "C" }
        ]

    ]
}

And the statement:

db.matrix.update(
    {},
    {
        "$pull": {
            "matrix.0": { "index": 1 },
            "matrix.1": { "index": 1 },
        }
    }
)

With this as the result:

{
    "matrix" : [
        [
            { "index" : 0, "ident" : "A" },
            { "index" : 2, "ident" : "C" }
        ],
        [
            { "index" : 0, "ident" : "A" },
            { "index" : 2, "ident" : "C" }
        ]
    ]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top