How can I change the .dbshell history file location - mongodb

With cmd in Win10, after terminate the mongo service, I got the error:
Error saving history file: FileOpenFailed Unable to fopen() file
C:\Users\鍒樺簡鏂嘰.dbshell: Access is denied.
If use Powershell with Admin, I will get this strange file in C:\Users:
鍒樺簡鏂嘰.dbshell
So, the error occurs for the reason of my non-English name directory, so how to change the location of the .dbshell file in MongoDB? Thanks!

You can set it with environment variable USERPROFILE:
c:\>set USERPROFILE=C:\Temp Files\IMP
c:\>mongo --norc
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b242c05d-68d5-48a5-ba06-43665f3eb2e9") }
MongoDB server version: 4.4.1
MongoDB Enterprise > db
test
MongoDB Enterprise > print("Hello World")
Hello World
MongoDB Enterprise > exit
bye
c:\>cat "C:\Temp Files\IMP\.dbshell"
db
print("Hello World")
c:\>
see How to get to the Mongo shell history file or all history on Windows
Note, at startup the mongo shell reads file %USERPROFILE%\.mongorc.js, so you may have to move this file as well.

Related

How to execute mongo commands through shell scripts? (mine does not work)

I am trying to use scripts js in the mongo shell. I am reading a book on the topic. I have tried several options, such as opening the cmd at a specific place at my computer.
I found here the question "How to execute mongo commands through shell scripts?", but nothing seems to work for me. My mongo is working properly and I can enter mongo simply typing mongo at cmd. However, it cannot find the js files.
I am using Windows; anything has an idea what may be happening under the hood?
An example after:
mongo demo.js
2020-02-20T11:03:33.098-0300 E - [main] file [demo.js] doesn't exist
2020-02-20T11:03:33.098-0300 F - [main] failed to load: demo.js
2020-02-20T11:03:33.098-0300 E - [main] exiting with code -3
Create your my_script.js file with this one command:
db.testColl.insertOne( { a: "hello" } )
Place the script file in your current directory.
1. Run JS Script from OS Command-line:
From OS prompt do this:
> mongo localhost/testDB my_script.js
Once the above command is run, you will see the output as follows (similar, depending upon your MongoDB version and the OS (Windows, in this case)):
MongoDB shell version v4.2.3
connecting to: mongodb://localhost:27017/testdb?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("456b350f-668f-4389-9901-7c456e2c50fe") }
MongoDB server version: 4.2.3
Now, from the Mongo Shell (i.e., do mongo, and then from the mongo prompt):
mongo > use testDB
mongo > show collections
You will see the testColl listed.
mongo > db.testColl.find()
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
This is the document created in the testDB database and the collection testColl as per the command in the my_script.js.
2. Run JS Script from Mongo Shell:
Also, you can run the my_script.jsfrom within the Mongo Shell.
mongo > load("my_script.js")
true
mongo > db.test.find()
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
{ "_id" : ObjectId("5e4ea10f276cde8fc5fedec5"), "a" : "hello" }
See there are two documents with different _id field values.
NOTE: I think you can run only some commands from the .js file.
3. Another Example:
Create a JS file named script2.js with the following content:
db.test.find().forEach(printjson)
Note the printjson shell method prints a document to the shell output.
mongo > load("script2.js")
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
{ "_id" : ObjectId("5e4ea10f276cde8fc5fedec5"), "a" : "hello" }
References:
load()
Print document value in
MongoShell

CREATE EXTENSION pljava failed on PostgreSql 9.6

I try to install pljava for postgesql 9.6 in Ubuntu 16.04, but it fails with error.
A gradlew script makes an installation, but it fails with the following error"
Failed to execute: CREATE EXTENSION pljava; because: ERROR: could not access file "pljava-so-1.5.1-BETA1": No such file or directory
A gradlew source:
Sql sql = establishConnection()
try {
sql.execute("SET pljava.libjvm_location TO '${javaNative}';")
sql.execute("SET pljava.vmoptions TO '-Xshare:on -XX:+DisableAttachMechanism';")
sql.execute("ALTER DATABASE postgres SET pljava.libjvm_location FROM CURRENT;")
sql.execute("ALTER DATABASE postgres SET pljava.vmoptions FROM CURRENT;")
sql.execute("CREATE EXTENSION pljava;")
} finally {
sql.close()
}
As i understand, it fails during a creation of extension pljava, commands above was executed without any errors.
According to #ChapmanFlack advice:
I have no any mentions about pljava-so-1.5.1-BETA1 file at pg_config --libdir directory. Also i didn't find that file at pg_config --sharedir
grep LOAD pljava--1.5.1-BETA1.sql output following:
touched off by the LOAD command, making possible a decent installation
LOAD command, but finds the CREATE EXTENSION command instead). So,
temporarily LOAD 'pljava-so-1.5.1-BETA1'; Ok, the LOAD succeeded, so
everything happened ... unless ... the same PostgreSQL turns LOAD into
a (successful) no-op in that case, meaning To fail fast in that case,
expect that the LOAD actions should have
I think PL/Java builds from a prebuilt, because of that line at gradlew:
commandLine 'java', '-jar', '../installer/build/dependencies/pljava-pg9.6.jar'
I would be appreciate for any advice

How to send the xml to mongoDb?

i am trying to send an xml.bz2 file to mongodb and i am using Mac and writing everything in the terminal.
In the https://github.com/spencermountain/wikipedia-to-mongodb there is a step 4 that i stucked,
4 get going
load it into mongo (10-15 minutes)
wikipedia-to-mongodb ./afwiki-latest-pages-articles.xml.bz2
I have a directory called wikipedia-to-mongodb and i downloaded file with wget. I connected mongod,
when i write the terminal that $ wikipedia-to-mongodb ./afwiki-latest-pages-articles.xml.bz2 it is not working. What am i doing wrong? I am kind of new this environment.
I tried 2 inputs below you can see it
1)
input= ./afwiki-latest-pages-articles.xml.bz2
[thread1] SyntaxError: missing name after . operator #(shell):1:22
2)
input= wikipedia-to-mongodb ./afwiki-latest-pages-articles.xml.bz2
-bash: wikipedia-to-mongodb: command not found

mongodb create database with dot/dash symbols

I need to create database named "my-database" (yes, with "-" or "." sym). I tried default syntax to achive that (http://docs.mongodb.org/manual/tutorial/getting-started/):
1) use my-database
2) db['test'].insert( {'foo':'bar'} )
3) show dbs // oh, God! I have done it!
But after first line I get
Error: Line 1: Unexpected identifier
How can I create database with name "my.db" / "my-db" via mongo shell?
UPDATE: this error occurs if I try to use commands from list above in Robomongo console... In mongo shell all works well.
> use test-db
switched to db test-db
> db['test-collection'].insert( {'foo':'bar'} );
> db['test-collection'].find();
{ "_id" : ObjectId("53f36049812d284697e80ffd"), "foo" : "bar" }
It's because Robomongo doesn't use the same V8 engine that mongo uses - it uses Spidermonkey. (thanks Lix)

mongo --shell file.js and "use" statement

can't find solution for simple question:
I have file text.js
use somedb
db.somecollection.findOne()
When I run this file in cmd with redirection command from file:
"mongo < text.js"
it's work properly
But when I try this way
"mongo text.js" or "mongo --shell test.js"
I got this error message
MongoDB shell version: 2.2.0
connecting to: test
type "help" for help
Wed Dec 05 16:05:21 SyntaxError: missing ; before statement pathToFile\test.js.js:1
failed to load: pathToFile\test.js.js
It's fail on "use somedb". If I remove this line, it's run without error, but console is clear.
is there any idea, what is this and how to fix?
I'm tying to find sollution for this, to create build tool for Sublime Text 2.
default build file was
{
"cmd": ["mongo","$file"]
}
but in this case I get the error above
PS. right after posting this question I find sollution for SublimeText2:
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo < ${file}"]
}
PSS. right after posting this question I find sollution for SublimeText3:
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo","<", "$file"]
}
this build tool work properly
use dbname is a helper function in the interactive shell which does not work when you are using mongo shell with a JS script file like you are.
There are multiple solutions to this. The best one, IMO is to explicitly pass the DB name along with host and port name to mongo like this:
mongo hostname:27017/dbname mongoscript.js // replace 27017 with your port number
A better way to do this would be to define the DB at the beginning of your script:
mydb=db.getSiblingDB("yourdbname");
mydb.collection.findOne();
etc.
The latter is preferable as it allows you to interact with multiple DBs in the same script if you need to do so.
You can specify the database while starting the mongo client:
mongo somedb text.js
To get the output from the client to stdout just use the printjson function in your script:
printjson(db.somecollection.findOne());
Mongo needs to be invoked from a shell to get that mode, with Ansible you would have this:
- name: mongo using different databases
action: shell /usr/bin/mongo < text.js
Instead of this:
- name: mongo breaking
command: /usr/bin/mongo < text.js
This is what finally worked for me on Windows + Sublime Text 2 + MongoDB 2.6.5
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo","<", "$file"],
"working_dir" : "C:\\MongoDB\\bin"
}