B
B
Bogdan Pasechnik2013-04-17 16:05:54
PHP
Bogdan Pasechnik, 2013-04-17 16:05:54

Get a list of values ​​from an array field

I'll show you an example because the title was not successful. Working with MongoDB database. Here is the table.

{
    "numbers": {
        "0": "10",
        "1": "11",
        "2": "12"
    },
    "type": "1",
    "otherBigVal":"очень много текста..."
},
{
    "numbers": {
        "0": "1",
        "1": "2",
        "2": "11",
        "3": "10"
    },
    "type": "1",
    "otherBigVal":"очень много текста..."
},
{
    "numbers": {
        "0": "18",
        "1": "14",
    },
    "type": "2",
    "otherBigVal":"очень много текста..."
}

You need to select all numbers where type=="1" and combine the values ​​with numbers. As a result, we must learn this [10,11,12,1,2]
And there are a lot of records in the database. Therefore, it is important to optimize the execution of the task as much as possible.
So far I've come up with a solution.
We make a request {type:"1"}
Then in PHP we loop through and combine the arrays.
But firstly, we pull out all the fields from the base, although we only need numbers. Secondly, we once again go through them in the code.
With a large amount of data, this is sad. Tell me how you can improve the optimization.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2013-04-17
@7workers

look in this direction (the example has not been tested): docs.mongodb.org/manual/core/aggregation/
db.collectionFoo.aggregate(
{
{ $match: {type:"1"} },
{ $project: {numbers: 1} },
{ $unwind: "numbers" },
}
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question