my question is very simple, I have:
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
SOLUTION:
NOTE : my linux distribution is Ubuntu 11.04
edit your config file /etc/mongodb.conf file with:
bind_ip = 0.0.0.0
restart yout mongodb service:
/etc/init.d/mondogb restart
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
make ip tables changes persistent
iptables-save > /etc/iptables.conf
Hope it helpds!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.