Docs Home / mongosh / Reference / Logs

View Shell Command History查看Shell命令历史记录

The MongoDB Shell saves a history of all commands you've run across sessions. When a new command is issued, it is added to the beginning of the history file.MongoDB Shell保存您在会话中运行的所有命令的历史记录。发出新命令时,它将添加到历史文件的开头。

Steps步骤

To view the MongoDB Shell command history, open the following file in a text editor:要查看MongoDB Shell命令历史记录,请在文本编辑器中打开以下文件:

Operating System操作系统Path to History File历史文件路径
macOS and Linux~/.mongodb/mongosh/mongosh_repl_history
Windows%UserProfile%/.mongodb/mongosh/mongosh_repl_history

Starting in MongoDB Shell 2.4.0, you can use the history() command to view a list of previously executed commands. For example:从MongoDB Shell 2.4.0开始,您可以使用history()命令查看以前执行的命令列表。例如:

history()

The following example output shows a list of commands in an array of strings:以下示例输出显示了字符串数组中的命令列表:

[
'db.pizzaOrders.explain()',
'db.pizzaOrders.find()',
'db.pizzaOrders.explain.find()',
'db.pizzaOrders.explain.find( {} )',
'db.pizzaOrders.explain().find( {} )',
'db.pizzaOrders.explain().find( orderDate, totalNumber )',
'db.pizzaOrders.explain().find( orderDate: Date( "2024-03-20T10:01:12Z" ), totalNumber: 20 )',
'db.pizzaOrders.explain().find( totalNumber: 20 )',
'db.pizzaOrders.explain().find( { orderDate: Date( "2024-03-20T10:01:12Z" ) }, { totalNumber: 20 } )',
'db.pizzaOrders.find( { orderDate: Date( "2024-03-20T10:01:12Z" ) }, { totalNumber: 20 } )',
...
]

The history is returned in chronological order.历史按时间顺序返回。

history() supports JavaScript array methods. You can use slice() to return a section of an array. For example, to view the last 10 commands, run:history()支持JavaScript数组方法。您可以使用slice()返回数组的一部分。例如,要查看最后10个命令,请运行:

history().slice(-10)