How can I initialize MongoDB -object db in a browser console? - javascript

Suppose I want to execute 'db.things.insert({colors : ["blue", "black"]})' in browser. I can execute it in the Mongodb -shell but not yet understanding how to execute it like: open up the Google Chrome Console, initialize the DB -object with some connection and execute the command. Does there exist some plugin? Sorry I am totally new to MongoDB, trying to test just things fast with Browser shell. How can I do the initialization like that?
Trial 0: perhaps with REST -interface?
I have enabled the REST with "$ echo 'rest=true' > /etc/mongodb.conf;
$ sudo restart mongodb", works in Ubuntu. More about rest
here, not sure
yet whether needed here but perhaps with some POST/REST -method I can
do the init.
Trial 1: Oreilly's book about MongoDB and 50 Tips (page 47)
The book has some example
> db = connect ("ny1a:27017/foo")
> db = connect ("ny1a:27017/admin")
so now
> db=connect("localhost:27017/test")
ReferenceError: connect is not defined
Yes because I need to source the connect -command, some further examples here, where can I get it?
P.s. I am studying this tutorial here.

You cannot simply access mongodb from the browser console. Your browser is a client, and there isn't (as far as I know) a javascript client-side library. Any javascript library you will find will most likely be for Node.js (server).
The mongo console is its own type of compiled client. The native language is javascript, but those commands only pertain to the actual mongo command shell. connect is a command for the mongo command shell.
Enabling REST starts a port on your mongod that will accept REST http communication. You get a browser page here: http://localhost:28017/
This is a very basic page displaying data, but you can further run queries yourself. See http://www.mongodb.org/display/DOCS/Http+Interface#HttpInterface-SimpleRESTInterface
Note, the REST interface is READ ONLY. You cannot insert.
That being said, you need a proper driver for your language of choice.

Related

How would I control/access ubuntu server services via a node.js web admin panel?

I don't know where to start with this question!
Basically, I would like to build a control panel, web based, using node.js as the back end. This would run on my raspberry pi running Ubuntu Server.
The idea is that I can gain statistics (CPU, Temperature, Disk Space etc) and set up basic features like MongoDB database, hosting etc.
Now, this is obviously possible just like many web panels out there, however, is this possible with node.js.
I suppose the first question would be, can I start/stop services (reboot server, start MongoDB etc) via Node.Js, and secondly, can I get info from this to display in my web panel?
I tried Google but for the first time, I don't even know what question to ask :)
I found Node JS examples of running command line commands, however, when passing in simple commands, like "node -v" it crashed, so I am not sure this is the method used by other commercial server web panels.
Any advice would be great :)
Thanks
You should try this tutorial: https://stackabuse.com/executing-shell-commands-with-node-js/
From node doc for child_process:
https://nodejs.org/api/child_process.html
const { exec } = require('child_process');
exec('"/path/to/test file/test.sh" arg1 arg2');
// Double quotes are used so that the space in the path is not interpreted as
// a delimiter of multiple arguments.
exec('echo "The \\$HOME variable is $HOME"');
// The $HOME variable is escaped in the first instance, but not in the second.

How to run Mongo JS Shell Script with DB Connection

I'm trying to write a mongo shell script that will delete some entries in an existing DB, and then reload them (depending on some conditions), and I'm having a lot of trouble.
One thing I can't understand/figure out is this:
To run the script file I'm making (let's call it script.js), I'm reading (from mongo docs and other places) that I need to use a command like this:
mongo mongodb://{{mycreds}}#{{address of existing mongo}}:27017/{{DB I want}}?authSource=admin script.js
However, since I'm connecting to that same DB in the script itself, I'm also seeing that I need to make that connection in the script.js:
db = connect(
"mongodb://{{mycreds}}#{{address where the existing mongo is}}:27017/{{DB I want}}?authSource=admin"
);
Why do I have to specify the DB connection when running the script? It's in the script?
Either way, I'm not able to make the connection in the script. I don't know if I'm running the script incorrectly, or making the connection to the DB in the script incorrectly. Or just something else.
In the docs, it says I can run the script in the mongo shell as well. But don't I have to have a mongo instance running at 27017 to start the script? And do I need a mongo running on my machine (since there's already one that exists that I need to connect to)?
I would really appreciate some clarity in this. All I'm trying to figure out is how to run a mongo script that connects to an existing DB, and I'm getting really tangled in all these docs.
Edit:
After receiving a comment (thanks Joe), I removed the connection in the script file and was able to make the connection. I guess having both was messing it up. I'd still like to be able to have the connection in the script, but not when I run the script.
I want to do this so that others can run the script without entering the long connection address.
If anyone knows a way to do this, I'd appreciate the help. Thanks.
you always need a credential for any DB you want to connect even if it's SQL. if you want to make it easier for you, you need to create a file.js in vs code and put a credential in a file when you need it you can just call it that's it, and use roboMongo to make your life easier with MongoDB.

how to predict online tensorflow model using JS

We build a NN model in tensorflow similar to
https://github.com/GoogleCloudPlatform/cloudml-samples/tree/master/census/tf-keras
Then we exported it to GCP using the following gcloud ai-platform models create commands
and predict using gcloud ai-platform predict works fine
Now we want to do online prediction using javascript, seems to have several options:
using https://www.npmjs.com/package/#google-cloud/automl, but
Checking https://googleapis.dev/nodejs/automl/latest/v1beta1.PredictionServiceClient.html#predict
it seems that it can not be used for neural networks(?)
When we run the example code
https://www.npmjs.com/package/#google-cloud/automl
with our parameters we get 'Invalid resource ID' error
Using REST API, curl works fine but how do we set permanent authorization in the server ?
Any other suggestion ?
Amir
I solved the problem by building Python container that run on gcloud
(https://cloud.google.com/run/docs/quickstarts/build-and-deploy)
The JS call the container using REST API, the python container do the prediction and reply to results the JS

Determine what Meteor methods are defined by the server

I'm trying to automate a Meteor app available online (namely, Meteor.com's account management - to add a collaborator to all 170+ organizations I'm a member of).
I've researched reverse engineering Meteor apps but haven't been able to figure out correctly from DDP messages what methods are available on the server.
Meteor methods can be seen among the WebSocket frames by looking for "msg":"method". For example, if you log into https://meteor.com, go to Organizations, and add a username to an organization in your Meteor account, you can see this in the WebSocket frame:
{
"msg":"method",
"method":"addOrganizationMember",
"params":["jspdf", "splendido"],
"id": "2"
}
(If the output looks uglier than that, vote for the Chrome team to implement this feature request for prettifying WebSocket frame dumps.) That method name, however, failed when running Meteor.call('addOrganizationMember', 'jspdf', 'splendido') in the console, with an error that the method wasn't found (404).
So you just want to check if the method exists first & then if it does, make the call?
I think you'd want something like:
if (Meteor.server.method_handlers.addOrganizationMember) {
Meteor.call('addOrganizationMember', 'jspdf', 'splendido')
})
For client methods, it'd be Meteor.connection._methodHandlers
Hopefully I understood, set me straight if I didn't.

Server-side Javascript in production fails to open connection to a named instance of SQL2008

I've got a production site that has been working for years with a SQL Server 2000 default instance on server named MDWDATA. TCP port 1433 and Named Pipes are enabled there. My goal is to get this web app working with a copy of the database upgraded to SQL Server 2008. I've installed SQL2008 with SP1 on a server called DEVMOJITO and tested the new database using various VB6 desktop programs that exercise various stored procs in a client-server fashion and parts of the website itself work fine against the upgraded database residing on this named instance of SQL2008. So, while I am happy that the database upgrade seems fine there is a part of this website that fails with this Named Pipes Provider: Could not open a connection to SQL Server [1231]. I think this error is misleading. I disabled Named Pipes on the SQL2000 instance used by the production site, restarted SQL and all the ASP code still continued to work fine (plus we have a firewall between both database servers and these web virtual directories on a public facing webserver.
URL to my production virtual directory which demos the working page:
URL to my development v-directory which demos the failing page:
All the code is the same on both prod and dev sites except that on dev I'm trying to connect to the upgraded database.
I know there are dozens of things to check which I've been searching for but here are a few things I can offer to help you help me:
The code that is failing is server-side Javascript adapted from Brent Ashley's "Javascript Remote Scripting (JSRS)" code package years ago. It operates in an AJAX-like manner by posting requests back to different ASP pages and then handling a callback. I think the key thing to point out here is how I changed the connection to the database: (I cannot get Javascript to format right here!)
function setDBConnect(datasource)
{
var strConnect; //ADO connection string
//strConnect = "DRIVER=SQL Server;SERVER=MDWDATA;UID=uname;PASSWORD=x; DATABASE=StagingMDS;";
strConnect = "Provider=SQLNCLI10;Server=DEVMOJITO\MSSQLSERVER2008;Uid=uname;Pwd=x;DATABASE=StagingMDS;";
return strConnect;
}
function serializeSql( sql , datasource)
{
var conn = new ActiveXObject("ADODB.Connection");
var ConnectString = setDBConnect(datasource);
conn.Open( ConnectString );
var rs = conn.Execute( sql );
Please note how the connection string differs. I think that could be the problem but I don't know what to do. I am surprised the error returned says "named pipes" was involved because I really wanted to use TCP. The connection string syntax here is the same as used successfully on a different part of the site which uses VBScript which I'll paste here to show:
if DataBaseConnectionsAreNeeded(strScriptName) then
dim strWebDB
Set objConn = Server.CreateObject("ADODB.Connection")
if IsProductionWeb() Then
strWebDB = "DATABASE=MDS;SERVER=MDWDATA;DRIVER=SQL Server;UID=uname;PASSWORD=x;"
end if
if IsDevelopmentWeb() Then
strWebDB = "Provider=SQLNCLI10;Server=DEVMOJITO\MSSQLSERVER2008;Database=StagingMDS;UID=uname;PASSWORD=x;"
end if
objConn.ConnectionString = strWebDB
objConn.ConnectionTimeout = 30
objConn.Open
set oCmd = Server.CreateObject("ADODB.Command")
oCmd.ActiveConnection = objConn
This code works in both prod and dev virtual directories and other code in other parts of the web which use ASP.NET work against both databases correctly. Named pipes and TCP are both enabled on each server. I don't understand the string used by the Pipes but I am using the defaults always.
I wonder why the Javascript call above results in use of named pipes instead of TCP. Any ideas would be greatly appreciated.
Summary of what I did to get this working:
Add an extra slash to the connection string since this is server-side Javascript:
Server=tcp:DEVMOJITO\MSSQLSERVER2008,1219;
Explicitly code tcp: as a protocol prefix and port 1219. I learned that by default a named instance of SQL uses dynamic porting. I ended up turning that off and chose, somewhat arbitrarily, the port 1219, which dynamic had chosen before I turned it off. There are probably other ways to get this part working.
Finally, I discovered that SET NOCOUNT ON needed to be added to the stored procedure being called. Otherwise, the symptom is the message: "Operation is not allowed when the object is closed".

Categories

Resources