Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongo shell startup configuration file .mongorc.js (IV)

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Mongo shell startup configuration file .mongorc.js (IV)

The ~ / .mongorc.js file is described as follows:

# mongorc.js

=

My mongorc.js file.

Provide:

-``pretty () `is queried by default using the pretty () help method

-`help method ugly () `

-prompt displays information related to the server type

# # Pretty

Mongo shell has a helper function called `pretty () `to beautify the result set. Using this mongorc.js file, the pretty behavior is enabled by default.

> db.marioGames.find ()

{

"_ id": ObjectId ("507333d49c25fa3b6e62174d")

"name": "Super Mario Bros"

"super": true

Release: ISODate ("1985-09-13T07:00:00Z")

}

{

"_ id": ObjectId ("5073347b9c25fa3b6e62174e")

"name": "Super Mario Bros 2"

"super": true

Release: ISODate ("1988-10-09T07:00:00Z")

}

{

"_ id": ObjectId ("5073348f9c25fa3b6e62174f")

"name": "Super Mario Bros 3"

"super": true

Release: ISODate ("1990-02-09T08:00:00Z")

}

# # Ugly

Now that we get the result set of pretty by default, we occasionally need the previous behavior (printing the document to a single line).

By using the `helper () `ugly method. Methods:

> db.marioGames ({super: true}) .ugly ()

{"_ id": ObjectId ("507333d49c25fa3b6e62174d"), "name": "Super Mario Bros", "super": true, "release": ISODate ("1985-09-13T07:00:00Z")}

{"_ id": ObjectId ("5073347b9c25fa3b6e62174e"), "name": "Super Mario Bros 2", "super": true, "release": ISODate ("1988-10-09T07:00:00Z")}

{"_ id": ObjectId ("5073348f9c25fa3b6e62174f"), "name": "Super Mario Bros 3", "super": true, "release": ISODate ("1990-02-09T08:00:00Z")}

# # Prompt

The default prompt now displays information about the server side of the connection.

# replSet

`

ReplSetName:ServerState | database >

`

# mongos

`

Mongos | host:port | database >

`

# mongod

`

Mongod | host:port | database >

`

# # Installation

Git clone git@github.com:aheckmann/mongorc.js.git

Cd mongorc.js

Make install

It copies the .mongorc.js file to your HOME path home directory.

If another .mongorc.js file already exists, it will be renamed .mongorc.js.old

# # Uninstall

Cd mongorc.js

Make uninstall

If ~ / .mongorc.js.old exists, it will be renamed to ~ / .mongorc.js

# # Licence

MIT

The contents of ~ / .mongorc.js file are as follows:

; (function () {/ * Make all queries pretty print by default. * / DBQuery.prototype._prettyShell = true/** * Allow opting into the default ugly print mode. * / DBQuery.prototype.ugly = function () {this._prettyShell = false; return this} / * * Override the default prompt to display info related * totype of server we connected to. * * @ return {String} * / prompt = function () {var res = rs.status (); if (! res | | res.errmsg) {/ / not in a replica set var status = db.serverStatus (); return status.process + "|" + status.host + "|" + db + ">";} return replsetPrompt () } / * * Creates a prompt string for replSets * * @ return {String} * / function replsetPrompt () {var status; var admin = db.getSiblingDB ("admin"); var info = admin.runCommand ({replSetGetStatus: 1, forShell: 1}); if (info.ok) {var state = ""; / / do we need this? Info.members.some (function (member) {if (member.self) {state = member.stateStr; return true;}}); status = info.set + ":" + (state | | info.myState);} else if (info.info & & info.info.length

< 20) { // < 20 seems like a hack ?? status = info.info; } return status + "|" + db + ">

"}) ()

See: https://github.com/aheckmann/mongorc.js

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report