File write operations in mongo script?

后端 未结 6 1574
执念已碎
执念已碎 2020-12-07 22:22

Is this possible to write the query result to the file from mongo js script. I have searched a lot, but I didn\'t find any solution.

ex:-

cursor = db         


        
6条回答
  •  一生所求
    2020-12-07 23:07

    Wouldn't it be simpler to use one of the Mongo drivers for a general purpose language (such as Python, Ruby, Java, etc) and write your results to a file that way, in a format you can use (such as CSV, etc.)?

    UPDATE: According to the documentation for mongodump you can export a collection with a query:

    $ ./mongodump --db blog --collection posts
    -q '{"created_at" : { "$gte" : {"$date" : 1293868800000},
                          "$lt"  : {"$date" : 1296460800000}
                        }
        }'
    

    However you would need to import that collection back into MongoDB to operate on it or use mongoexport to export as JSON or CSV using the same query flag (-q) as mongodump.

提交回复
热议问题