0

I have an array of ids stored in a file, and I want to retrieve their data from the mongdb so i looked into the mongoexport method. it seems --query option can only accept a json instead read a large json or array from a file. In my case, it is about 4000 ids stored in the file. Is there a solution to this?

I was able to use mongoexport --db db --collection collection --field name --csv -oout ~/data.csv but how to read query conditions from a file

for example, for mongoid in rails application, query like this is Data.where(:_id.in => array).

or is it possible to do from mongo shell by executing a javscript file

tks

4

1 回答 1

1

I believe you can use a javascript to output the array you need.

you can use "printjson" command in your script, for example:

create a script.js javascript file as following:

script.js:

printjson( db.albums.find({_id : 18}, {"images" : 1,"_id":0}).toArray() )

Call hi as follow:

mongo test script.js > out.txt

In my local environment albums collection has the following structure:

 db.albums.findOne({"_id":18
{
        "_id" : 18,
        "images" : [
                2926,
                5377,
                8036,
                9023,
                10119,
                11543,
                12305,
                12556,
                12576,
                13753,
                14414,
                14865,
                15193,
                15933,
                17156,
                17314,
                17391,
                20168,
                21705,
                22016,
                22348,
                23036,
                23452,
                24112,
                27086,
                27310,
                27864,
                28092,
                29184,
                29190,
                29250,
                29354,
                29454,
                29563,
                30366,
                30619,
                31390,
                31825,
                31906,
                32339,
                32674,
                33307,
                33844,
                37475,
                37976,
                38717,
                38774,
                39801,
                41369,
                41752,
                44977,
                45384,
                45643,
                46918,
                47069,
                50099,
                52755,
                54314,
                54497,
                62338,
                63438,
                63572,
                63600,
                65631,
                66953,
                67160,
                67369,
                69802,
                71087,
                71127,
                71282,
                73123,
                73201,
                73954,
                74972,
                76279,
                77054,
                78397,
                78645,
                78936,
                79364,
                79707,
                83065,
                83142,
                83568,
                84160,
                85391,
                85443,
                85488,
                86143,
                86240,
                86949,
                89406,
                89846,
                92591,
                92639,
                92655,
                93844,
                93934,
                94987,
                95324,
                95431,
                95817,
                95864,
                96230,
                96975,
                97026
        ]
}
>

, so the output I got was:

$ cat out.txt

MongoDB shell version: 2.2.1
connecting to: test
[
        {
                "images" : [
                        2926,
                        5377,
                        8036,
                        9023,
                        10119,
                        11543,
                        12305,
                        12556,
                        12576,
                        13753,
                        14414,
                        14865,
                        15193,
                        15933,
                        17156,
                        17314,
                        17391,
                        20168,
                        21705,
                        22016,
                        22348,
                        23036,
                        23452,
                        24112,
                        27086,
                        27310,
                        27864,
                        28092,
                        29184,
                        29190,
                        29250,
                        29354,
                        29454,
                        29563,
                        30366,
                        30619,
                        31390,
                        31825,
                        31906,
                        32339,
                        32674,
                        33307,
                        33844,
                        37475,
                        37976,
                        38717,
                        38774,
                        39801,
                        41369,
                        41752,
                        44977,
                        45384,
                        45643,
                        46918,
                        47069,
                        50099,
                        52755,
                        54314,
                        54497,
                        62338,
                        63438,
                        63572,
                        63600,
                        65631,
                        66953,
                        67160,
                        67369,
                        69802,
                        71087,
                        71127,
                        71282,
                        73123,
                        73201,
                        73954,
                        74972,
                        76279,
                        77054,
                        78397,
                        78645,
                        78936,
                        79364,
                        79707,
                        83065,
                        83142,
                        83568,
                        84160,
                        85391,
                        85443,
                        85488,
                        86143,
                        86240,
                        86949,
                        89406,
                        89846,
                        92591,
                        92639,
                        92655,
                        93844,
                        93934,
                        94987,
                        95324,
                        95431,
                        95817,
                        95864,
                        96230,
                        96975,
                        97026
                ]
        }
]

Regards, Moacy

于 2013-03-25T01:13:39.337 回答