Mongoexport gets property of nested objects in CSV output

I am trying to get CSV from mongo-db using mongoexport.

My data in this format:

{
    "_id": "99",
    "page_id": NumberLong(1122334455),
    "data": {
        "0": {
            "item_id": NumberLong(123456789),
            "item_name": "item1"

        },
        "1": {
            "item_id": NumberLong(987654321),
            "item_name": "item2"
        },
     },
    "last_updated_utc": ISODate("2013-12-19T13:17:43.994Z")

}

To do this, I use the following command:

mongoexport -f _id,page_id,last_updated_utc --query {page_id:1122334455} -d mydatabase -c mycollection --csv

This gives the result:

"99",1122334455,2013-12-19T13:17:43.994Z
exported 1 record

The problem is that I need an element item_namefrom the dataelements in the output. This is a dynamic array that cannot contain elements or many elements.

If I add datafields (-f) to the parameter, it will simply output it as a JSON string in CSV for each object, which will not help to use the data in the future.

What I'm going to get is something like:

"99",1122334455,2013-12-19T13:17:43.994Z,item1
"99",1122334455,2013-12-19T13:17:43.994Z,item2

Almost denormalized or similar to an outer join in SQL. So its just element identifiers data.

Is it possible? How can I get item_idCSV output?

+4
2

Mongoexport - , JSON , , CSV. , JSON, , . , .

- , CSV-.

, . data . , , , , .

{
    "_id": "99",
    "page_id": NumberLong(1122334455),
    "data": [
    {
            "item_id": NumberLong(123456789),
            "item_name": "item1"

        },
        {
            "item_id": NumberLong(987654321),
            "item_name": "item2"
        },
     ],
    "last_updated_utc": ISODate("2013-12-19T13:17:43.994Z")

}

:

db.sample.aggregate([
    {$unwind: "$data"},
    {$project: { 
       page_id: 1,
       item_name: "$data.item_name",
       last_updated_utc: 1
     }}
])

[
     {
         "_id" : "99",
         "page_id" : NumberLong(1122334455),
         "last_updated_utc" : ISODate("2013-12-19T13:17:43.994Z"),
         "item_name" : "item1"
     },
     {
         "_id" : "99",
         "page_id" : NumberLong(1122334455),
         "last_updated_utc" : ISODate("2013-12-19T13:17:43.994Z"),
         "item_name" : "item2"
     }
 ],

CSV.

, data , data , . , , .

, , . , .

+5

MongoExport

{
"_id": "99",
"page_id": NumberLong(1122334455),
"data": {
    "0": {
        "item_id": NumberLong(123456789),
        "item_name": "item1"

    },
    "1": {
        "item_id": NumberLong(987654321),
        "item_name": "item2"
    },
 },
"last_updated_utc": ISODate("2013-12-19T13:17:43.994Z")

} MongoExport

mongoexport --host <hostname> --db <Database Name> --collection <collection Name> --csv --fields fieldname1,fieldname2 --out fileName.csv

: CSV

mongoexport --host localhost --db xyz --collection abc --csv --fields data.0.item_id,data.0.item_name,data.1.item_id,data.1.item_name --out important.csv

: JSON

mongoexport --host localhost --db xyz --collection abc --fields data.0.item_id,data.0.item_name,data.1.item_id,data.1.item_name --out important.csv

, , .

$: . , .

.

https://docs.mongodb.org/v3.0/reference/operator/aggregation/unwind/

+4

Source: https://habr.com/ru/post/1524741/


All Articles