How to run scripts in mongoDB - mongodb

I have the following scripts, which can be run in Sublime text built-in build.
I want to know how to run the sample script in command line
like mongo -f sample_script.js, thanks
sample_script.js
use security_development;
Object.bsonsize(db.grouped.findOne({"_id":"00063acf1078671c6b90d46effad474b"}))

You don't need the -f. Move database selection to command-line argument, though (use and show helpers don't work when executed from a file)
mongo -d security_development sample_script.js

Related

Postgres: Run some hard-coded SQLs then drop to interactive terminal

When you start interactive bash, it runs .bashrc first and then it gives you an interactive prompt. Very handy to setup bash in the right way.
I'm trying to do the same with Postgres client (psql). I want to set some session configuration parameters before I run SQL statements interactively. Does psql let me do that?
The -c option and the -f option are the standard ways to run a pre-canned SQL statements, but the man page clearly states that those options are incompatible with the interactive mode.
The analogy to .bashrc extend to the name as well. You want .psqlrc

How to run Algolia Firestore extension import script?

I'm installing the Algolia extension for Firestore. Setup works just fine and it updates indices on add delete and update. But now I want to backfill it with existing data.
The following steps are provided in the setup guide but I have no clue on HOW to run that script. I've tried pasting it directly in node shell and powershell, adding it to a js or ps1 file and running that but I don't know what kind of script this is.
How do I run this script? (I have a service account json next to it)
It's bash...
It works when pasted directly in bash with spaces on each newline after the line terminator. Or as a .sh file from the commandline.
#!/bin/bash
LOCATION=europe-west3\
ALGOLIA_APP_ID=xxx\
ALGOLIA_API_KEY=xxx\
ALGOLIA_INDEX_NAME=organizations\
COLLECTION_PATH=organizations\
FIELDS=name,address,city\
GOOGLE_APPLICATION_CREDENTIALS=./xxx.json\
npx firestore-algolia-search

Difference between psql command and psql interactive terminal

I've tried various combinations of search terms and I can't find an answer to this...
I'm new to Postgres, and I'm enjoying using the psql interactive terminal to run SQL commands. But often when I look things up, I find people using psql as a command rather than as a terminal.
For instance, you can restore a database using this command:
psql database-name < path/to/backup.dmp
My question is, are they the same thing or different things? When I run psql as a standalone command, am I effectively running up an interactive terminal for just that one command? And if so, does that mean that anything which goes after psql will also work as a command typed into the psql terminal? So in the example above, I could also just start up a psql terminal and then run the following command?
postgres=# database-name < path/to/backup.dmp
This is actually basic bash stuff. You should read up on Unix shells to understand that better.
Each process has a standard input, a standard output and a standard error.
By default, the interactive terminal where you started a program will be used for these, so the text you type will be the input of the program, and the output of the program will be shown on your screen.
Bot you can also redirect standard input with
command < file
Then the input for the program will be taken from file rather than from the interactive terminal.
That's one of the ideas in Unix: the user is just another file from which you can read and to which you can write.
So everything before the < is part of the command invocation, and everything after the < is the file to read.
If you want to read and execute an SQL script while in a psql interactive session, use
\i file.sql

Launch external program with multiple commands via Powershell

I am currently attempting to launch a different console (.exe) and pass multiple commands; while starting and entering a command works just fine, I haven't been able to find out how multiple ones can be entered via powershell.
& "C:\Program Files\Docker Toolbox\start.sh" docker-compose up -d --build
The given command works fine, but as mentioned I need to pass more than one command - I tried using arrays, ScriptBlocks and different sequences, though to no avail.
Edit:
Noticed that the docker build has a -f tag which allows me to specify a file; however, the issue now seems to be that the executed cmd removes all backslashes & special characters, rendering the path given useless.
Example:
&"C:\Program Files\Docker Toolbox\start.sh" 'docker-compose build -f
path\to\dockerfile'
will result in an error stating that "pathtodockerfile" is an invalid path.
Your start.sh needs to be able to handle multiple arguments. This doesn't look like a PowerShell question
Turns out that it was easier than expected; Solved it by executing a seperate file that contained the two commands needed and passing it to the start.sh file.
&"C:\Program Files\Docker Toolbox\start.sh" './xyz/fileContainingCommands.sh'

how to invoke unix command in perl

i am trying to execute following unix command but its not getting executed
$array_of_tables= `dbsmp $srv_name`;
print "$array_of_tables\n";
please help me to find out list of tables in a data base through perl scripting.
Also i am trying to copy a file from a path to different path by using following command:-
copy(`cd /osp/slee/service/$srv_name/bin/exec/script.txt`,`cd /osp/local/home/linus/amit/scripts`);
but getting an error:-
Usage: copy(FROM, TO [, BUFFERSIZE])
please provide some solution
Thanks
Use doublequotes instead of back ticks.
copy("/osp/slee/service/$srv_name/bin/exec/script.txt","/osp/local/home/linus/amit/scripts");
and remove the cd
In Perl, the preferable way to capture the output of a system (shell) command is the qx() operator. See http://perldoc.perl.org/perlop.html#Quote-Like-Operators.
$array_of_tables = qx(dbsmp $srv_name);
print("$array_of_tables\n");
Actually, backticks should also work, so the problem must lie with your dbsmp command. I'm not sure what that command is; you'll have to provide more information about the utility and what error you're seeing.
For comparison, I can retrieve the list of tables in my local postgres database as a pipe-separated table using this shell command:
> psql -tAXq main postgres <<<\\d;
And this can be run from Perl as follows:
> perl -e 'print(qx(psql -tAXq main postgres <<<\\\\d;));'