sql >> Base de Datos >  >> NoSQL >> MongoDB

Impresión de la salida de consulta de Mongo en un archivo mientras está en el shell de mongo

AFAIK, no hay una opción interactiva para la salida al archivo, hay una pregunta SO anterior relacionada con esto:Impresión de la salida del shell mongodb a archivo

Sin embargo, puede registrar toda la sesión de shell si invocó el shell con el comando tee:

$ mongo | tee file.txt
MongoDB shell version: 2.4.2
connecting to: test
> printjson({this: 'is a test'})
{ "this" : "is a test" }
> printjson({this: 'is another test'})
{ "this" : "is another test" }
> exit
bye

Entonces obtendrás un archivo con este contenido:

MongoDB shell version: 2.4.2
connecting to: test
> printjson({this: 'is a test'})
{ "this" : "is a test" }
> printjson({this: 'is another test'})
{ "this" : "is another test" }
> exit
bye

Para eliminar todos los comandos y mantener solo la salida json, puede usar un comando similar a:

tail -n +3 file.txt | egrep -v "^>|^bye" > output.json

Entonces obtendrás:

{ "this" : "is a test" }
{ "this" : "is another test" }