command line argument to a javascript file that works on mongodb - javascript

I have a javascript file ( .js ) that works on MongoDB. I run the .js file as
mongo localhost:27017/dbname myjsfile.js .
How can I send command line arguments while running this JavaScript file ? I want to send database name and collection name as command line argument.

Well, you are already setting the database in use as you connect via:
mongo localhost:27017/dbname
So it is now on database "dbname". That is carried through to the db variable. Which is just a placeholder for the "current" database object.
That means that anything in your "script":
var results = db.collection.find().toArray();
For example is using the database you selected and the collection you named.
Need more? This is valid to:
db["mycollection"].find();
It's just JavaScript to the shell.
if you want a collection to be set as a variable then do something like this:
mongo localhost/mydb --eval "var users = db.users" myfile.js
Or otherwise just do that in you JavaScript file. You can test that by:
mongo localhost/mydb --eval "var users = db.users" --shell
And in the shell you now have a variable users that is "aliased" to the users collection.

cli argument no. But you can read a json file and parse it in your script.
// config.json - {"dbname":"dbname","collection":"mycollection"}
var args = JSON.parse(cat("config.json"));

Related

How to Import/Insert Entries from CSV/EXL into Mongo Collection in Mongo Shell Script

I'm writing a mongo shell script that will delete some entries in an existing DB, and then reload entries with data from a CSV (which was originally a spreadsheet file).
I'm at the point where I need to read the CSV file to make the entries to be inserted into the collection, but I can't figure how to read the CSV file while only using the js script.
I know there are solutions (like this one) that shows how to use javascript with jquery to read the csv file, but I'm only using a js shell script file so those solutions aren't working for me.
This is what my mongo shell script looks like as of now:
printjson("Start");
// I will be deleting all entries, but for now only one.
printjson("Deleting Tag");
db.Tag.deleteOne({ name: "Tag Name" });
// Here is where I want to add entries from CSV into Mongo Colletion
// For now, I'm just inserting 1 with hard-coded data
printjson("Inserting Tag");
db.Tag.insertOne({
name: "Tag name",
description: "A tag used for testing",
});
// To verfiy the object was inserted
printjson("\nFinding Inserted Tag");
cursor = db.Tag.find({ name: "Tag name" });
while (cursor.hasNext()) {
printjson(cursor.next());
}
printjson("End.");
And I run it in my terminal with a command like this:
mongo mongodb://{{mycreds}}#{{address of existing mongo}}:27017/{{DB I want}}?authSource=admin script.js
How do I read a csv file in a js shell script?
What I really want is to insert the data in the csv file into my mongo collection. If there's a better way than reading the file, creating entries from data, and then inserting, I'd love to know.
Thank you for any assistance.

Execute multiple mongo scripts from the commandline

For executing a single query script (query.js) from the command line the following code was enough.
mongo db-name < query.js
I would like to execute (or match) multiple query files, such as query1.js, query2.js and so on. I tried the following code with no success.
mongo db-name < query*
Please help me out here.
mongo will not allow you to do it you will get an amibgous redirect error. Do this
cat query* | mongo --nodb
the | takes the output of cat query* passes it as input to mongo which in turn executes any thing it gets. cat does not execute the queries it only outputs the content of the files and passes it to mongo.

Passing only the required rows of data from CSV file to Jmeter based on a criteria

I have a csv file which has the input in the below format (It has no headers as first row):
India,QA,1200,
India,QA1,1201,
India,QA2,1202,
USA,Dev1,5580,
USA,Dev2,5580,
AUS,Dev3,3300,
AUS,Dev4,3301,
I have configured the CSV Data Set Config component and have given the respective path and variable name details. Snapshot below:
from the command line argument, i will be invoking the Jmeter jmeter.bat -t C:\Users\Dev\Desktop\JI\testscript.jmx -JCountry=Indiawhich also has a parameter called JCountry=India.
Now, I have to use this value (India) and then search the csv file's first column and if it matches, I need to send only those particular rows matching to the country name given from the cmd to the script.
I thought of using If Controller but how can I check the csv files first row and when there is a match, send those details to the script.
The easiest option would be dynamically generating a CSV file with only India lines
Add setUp Thread Group to your Test Plan
Add Test Action sampler to the setUp Thread Group (this way you won't have an extra result in .jtl file)
Add JSR223 PreProcessor as a child of the Test Action Sampler
Put the following code into "Script" area:
String country = props.get('Country')
def originalCsvFile = new File('C:/Users/Dev/Desktop/JI/JVMDetails/Details.txt')
def countryCsvFile = new File('C:/Users/Dev/Desktop/JI/JVMDetails/Country.txt')
countryCsvFile.delete()
originalCsvFile.eachLine {line ->
if (line.startsWith(country)) {
countryCsvFile << line
countryCsvFile << System.getProperty('line.separator')
}
}
Configure your CSV Data Set Config to use C:\Users\Dev\Desktop\JI\JVMDetails\Country.txt as this file will have only those lines which start with what you have defined as Country property
More information:
Apache Groovy - Why and How You Should Use It
Groovy Goodness: Working with Files
You need to loop through CSV, see example or other examples.
As the second example use in while condition the variable from CSV: ${Country} .
Inside loop you need to add If Controller with condition to compare country variable against country property:
${__jexl3("${__P{Country}" == "${Country}")}
Checking this and using __jexl3 or __groovy function in Condition is advised for performances

command line argument in js file for mongodb

I am running a js script to work on mongodb as
mongo localhost:27017/dbname mongodump_isp.js
In the mongodump_isp.js file, I am doing at the very beginning
conn= new Mongo();
db=conn.getDb("dbname");
I have to again provide the database name inside the js file. How can I provide that dynamically?
The conn part is not necessary but you can just set the "database" name like this:
var db = db.getSiblingDB("dbname");
And then all references to db afterwards will use that database, or you can set another variable to another db connection
But if what you are talking about is specifying the db value on the command line, then you do not need any extra lines in your code as the value of db is already set to what you specified.

WMI query to Win32_Directory not returning all results

I'm running some WMI queries against a remote computer (from JavaScript/JScript, WMIC, a downloaded WMI query tool -- it makes no difference), to which I have adminstrator privileges.
The query is against the Win32_Directory class and it attempts to find all folders on the target server, called 'db', 'hooks', 'conf' or 'locks', but it is not returning all folders; it only gets about 150 of them.
The queries I have tried are:
SELECT drive, path, filename
FROM Win32_Directory
WHERE filename = 'db'
OR filename = 'conf'
OR filename = 'hooks'
OR filename = 'locks'
and:
SELECT drive, path, filename
FROM Win32_Directory
WHERE name LIKE '%\\db'
OR name LIKE '%\\conf'
OR name LIKE '%\\locks'
OR name LIKE '%\\hooks'
or, in WMIC (from the local machine):
wmic fsdir where (name like '%\\db' or name like '%\\conf' or name like '%\\hooks' or name like '%\\locks') get drive,path,name
I'm pretty sure I've got my escaping sorted using \' and \\ where appropriate for JScript, and I'm using a basic call to var wmiResults = wmi.ExecQuery(wql, 'WQL', 32); to get the results set.
The equivalent batch command, run on the local machine returns far more results:
for /r %A in (db,conf,hooks,locks) do #if exist "%~A" echo %~A
It's like there's some sort of caching or paging going on, or an index needs rebuilding, but I don't know where to start to tell it to refresh its cache or retrieve all results.
Help!!

Categories