0

my question is very simple, I have:

  • Mongodb server running on a Linux server
  • Windows machine with powershell

how can I connect my powershell to insert/add/update documents from my Windows (powershell) server to mongodb database instance running on a remote linux server?

I tried using C++ drivers but does not connect.

this is my code:

$mongoDbDriverPath = "C:\scripts\CSharpDriver-1.9-rc0\";

Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.dll"

$dbName = "mydb"
$collectionName = "backupData"
$db = [MongoDB.Driver.MongoDatabase]::Create("mongodb://<username>:<passsword>@<IP>:27017/$($dbName)")
$collection = $db[$collectionName]

$document = new-object MongoDB.Bson.BsonDocument
$document.Add("PreName",[MongoDB.Bson.BsonValue]::Create("Daniel"))
$document.Add("LastName",[MongoDB.Bson.BsonValue]::Create("Weber"))
$collection.save($document)

This is what I get

Exception calling "Save" with "1" argument(s): "Unable to connect to server <IP>:27017: A connection attempt failed because the connected party did not properly respond after a period of time, or established 
connection failed because connected host has failed to respond <IP>:27017."
At C:\scripts\mongo_test.ps1:14 char:17
+ $collection.save <<<< ($document)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Any suggestion?

thank you very much

1
  • mongoimport is not a solution for me because it does not parses dates... Commented Mar 1, 2014 at 3:05

2 Answers 2

4

SOLUTION:

NOTE: my linux distribution is Ubuntu 11.04

  1. edit your config file /etc/mongodb.conf file with:

    bind_ip = 0.0.0.0

  2. restart yout mongodb service:

    /etc/init.d/mondogb restart

  3. Add following rules to your IP tables:

    iptables -A INPUT -s -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -d -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

  4. make ip tables changes persistent

    iptables-save > /etc/iptables.conf

Hope it helpds!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for share your solution !! Sometimes users forget to return in order to post their results.
-1

If you only want make queries against your MongoDB server, you can download MongoDB for Windows and use MongoDB Shell (mongo.exe).

mongo.exe server/port

Then you will be able to make queries, insert documents, etc.

If you want use MongoDB directly from PowerShell, you can use C# driver. Here is an example.

1 Comment

tnanks rubenfa, I already tried that, my problem is connecting from Windows (powershell) to a mongodb server in a remote machine in linux

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.