I've recently re-installed Mongodb switching from the enterprise to the community version. Nevertheless, when I start mongo, this is the prompt format I get:
MongoDB Enterprise >
How can I change it to the standard prompt version? (i.e., > if I am not wrong)
You can change the prompt from within a shell session by setting the prompt variable. For example, issuing the following command within a Mongo shell ...
var prompt="this_prompt >"
... will result in the shell's prompt changing to:
this_prompt >
You can change the default prompt for all future sessions by updating your .mongorc.js (you'll find this in your $HOME directory and if it does not exist then just create it). The following addition to your .mongorc.js ...
var prompt=function() {
return ISODate().toLocaleTimeString() + " > ";
}
... will result in this prompt:
:~/dev/tools/mongodb/mongodb-osx-x86_64-3.4.7/bin$ ./mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
16:09:43 >
16:09:57 >
Or, to get this prompt: > just add the following to your .mongorc.js:
var prompt=">"
There remains the sub text to your question, namely; where is the existing MongoDB Enterprise > prompt coming from?. It's hard to say but ...
Perhaps you have a Global mongorc.js; have a look in /etc/mongorc.js if you are on a *nix system or %ProgramData%\MongoDB if you are on Windows
Mongo behavour when finding a rc file is also subject to environment variables, more details in the docs.
When you search the internet for this topic then often you find the function below. If you like to include the existing prompt then use function defaultPrompt(), e.g.
prompt = function() {
if (typeof db == 'undefined')
return '(nodb)> ';
// Check the last db operation
try {
db.runCommand( {getLastError:1} );
} catch (e) {
print(e);
}
var user = db.runCommand({connectionStatus : 1}).authInfo.authenticatedUsers[0]
if (user) {
return user.user + "#" + db + " " + defaultPrompt();
} else {
return "(anonymous)#" + db + " " + defaultPrompt();
//return db + " " + defaultPrompt(); // if you prefer
}
}
Prompts will be like this:
admin#mip mongos>
mip shard001:ARBITER>
admin#mip configRepSet:PRIMARY>
Related
i am trying to pull records form mongo data base with json format to do that i am running below js :
conatin of get_DRMPhysicalresources_Data.js
cursor = db.physicalresources.find().limit(10)
while ( cursor.hasNext() ){
print( JSON.stringify(cursor.next()) );
}
the command i run to get the records :
mongo host_name:port/data_base_name -u user -p 'password' --eval "load(\"get_DRMPhysicalresources_Data.js\")" > DRMPhysicalresources.json
and i am able to get the result as josn formate inside DRMPhysicalresources.json , now i want to switch to other data base using use command i try add use db as below :
conatin of get_DRMPhysicalresources_Data.js
use db2_test
cursor = db.physicalresources.find().limit(10)
while ( cursor.hasNext() ){
print( JSON.stringify(cursor.next()) );
}
the command i run to get the records :
mongo host_name:port/data_base_name -u user -p 'password' --eval "load(\"get_DRMPhysicalresources_Data.js\")" > DRMPhysicalresources.json
but i am getting below errors :
MongoDB shell version v4.2.3
connecting to: "some data base info"
Implicit session: session { "id" : UUID("8c85c6af-ebed-416d-9ab8-d6739a4230cb") }
MongoDB server version: 4.4.11
WARNING: shell and server versions do not match
2022-04-11T13:39:30.121+0300 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell eval):1:1
2022-04-11T13:39:30.122+0300 E QUERY [js] Error: error loading js file: get_DRMPhysicalresources_Data.js :
#(shell eval):1:1
2022-04-11T13:39:30.122+0300 E - [main] exiting with code -4
is there are any way to add use db2_test without break it ?
You can try this:
db = new Mongo().getDB("myOtherDatabase");
or
db = db.getSiblingDB('myOtherDatabase')
( this is the exact js equivalent to the 'use database' helper)
docs
It is a mongo terminal problem,
#!/bin/bash
echo
echo "Hello $USER."
echo "--------------------------"
mongo < myScript.js
with myScript.js as
if (hereAFunctionCheckExistsDatabase('myDatabase'))
doSomething()
exit
Of course show dbs work, but is a echo, I need a function.
My mongo:
MongoDB shell version: 2.4.10
connecting to: test
You need
function hereAFunctionCheckExistsDatabase(db) {
return (db.getMongo().getDBNames().indexOf(db)!=-1);
}
Mongo have also a catalog of namespaces, try this
db.system.namespaces.find( { name: /myDatabase\./ } );
...
New to NixOS, and trying out a basic setup, including adding a new user. I'm sure this is a simple fix. I just need to know what setting to put in. Pastebin details are here.
These are the partial contents of /etc/nixos/configuration.nix. I created my nixos from this stock vagrant box: https://github.com/zimbatm/nixbox .
{
...
users = {
extraGroups = [ { name = "vagrant"; } { name = "twashing"; } { name = "vboxsf"; } ];
extraUsers = [ {
description = "Vagrant User";
name = "vagrant";
...
}
{ description = "Main User";
name = "user1";
group = "user1";
extraGroups = [ "users" "vboxsf" "wheel" "networkmanager"];
home = "/home/user1";
createHome = true;
useDefaultShell = true;
} ];
};
...
}
And these are the errors when calling nixos-rebuild switch , to rebuild environment. user1 doesn't seem to get added properly. And I certainly can't su to it after the command is run. How do I declaratively create users, and set their groups?
$ sudo nixos-rebuild switch
building Nix...
building the system configuration...
updating GRUB 2 menu...
stopping the following units: local-fs.target, network-interfaces.target, remote-fs.target
activating the configuration...
setting up /etc...
useradd: group 'networkmanager' does not exist
chpasswd: line 1: user 'user1' does not exist
chpasswd: error detected, changes ignored
id: user1: no such user
id: user1: no such user
/nix/store/1r443r7imrzl4mgc9rw1fmi9nz76j3bx-nixos-14.04.393.6593a98/activate: line 77: test: 0: unary operator expected
chown: missing operand after ‘/home/user1’
Try 'chown --help' for more information.
/nix/store/1r443r7imrzl4mgc9rw1fmi9nz76j3bx-nixos-14.04.393.6593a98/activate: line 78: test: 0: unary operator expected
chgrp: missing operand after ‘/home/user1’
Try 'chgrp --help' for more information.
starting the following units: default.target, getty.target, ip-up.target, local-fs.target, multi-user.target, network-interfaces.target, network.target, paths.target, remote-fs.target, slices.target, sockets.target, swap.target, timers.target
How do I show the current user that I'm logged into the mongo shell as? This is useful to know because it is possible to change the user that you are logged in as—e.g. db.auth("newuser", "password")—while in the interactive shell. One can easily lose track.
Update
Using the accepted answer as a base, I changed the prompt to include user, connection, and db:
Edit .mongorc.js in your home directory.
function prompt() {
var username = "anon";
var user = db.runCommand({connectionStatus : 1}).authInfo.authenticatedUsers[0];
var host = db.getMongo().toString().split(" ")[2];
var current_db = db.getName();
if (!!user) {
username = user.user;
}
return username + "#" + host + ":" + current_db + "> ";
}
Result:
MongoDB shell version: 2.4.8
connecting to: test
anon#127.0.0.1:test> use admin
switched to db admin
anon#127.0.0.1:admin> db.auth("a_user", "a_password")
1
a_user#127.0.0.1:admin>
The connectionStatus command shows authenticated users (if any, among some other data):
db.runCommand({connectionStatus : 1})
Which results in something like bellow:
{
"authInfo" : {
"authenticatedUsers" : [
{
"user" : "aa",
"userSource" : "test"
}
]
},
"ok" : 1
}
So if you are connecting from the shell, this is basically the current user
You can also add the user name to prompt by overriding the prompt function in .mongorc.js file, under OS user home directory. Roughly:
prompt = function() {
user = db.runCommand({connectionStatus : 1}).authInfo.authenticatedUsers[0]
if (user) {
return "user: " + user.user + ">"
}
return ">"
}
An example:
$ mongo -u "cc" -p "dd"
MongoDB shell version: 2.4.8
connecting to: test
user: cc>db.auth("aa", "bb")
1
user: aa>
I am trying to get variety working with db.eval() instead of commandline --eval. This is because I get some problems with authentication.
The normal use is from the commandline:
$ mongo test --eval "var collection = 'users', maxDepth = 3" /path/to/variety.js
What I am trying to do is this:
$ mongo
>> use admin
>> db.auth("foo", "bar")
>> use anotherColl
>> db.eval("/path/to/variety.js", "var collection = 'users', maxDepth = 3")
>> Thu Mar 7 13:19:10 uncaught exception: {
"errmsg" : "compile failed: JS Error: SyntaxError: invalid flag after regular expression nofile_a:0",
"ok" : 0
}
Is there a way to have db.eval() eat an javascript file instead of an string?
According to #Sammaye and my gut feeling you can't put in .js files in db.eval(). However for variety there is an solution described here.
mongo -u USER-p PASS admin --eval "var collection = 'COL', db_name='DB_NAME'" variety.js
And now the authentication problem :(