Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use MongoDB from my C# console application

Tags:

c#

mongodb

Unable to connect to server localhost:27017: Command 'ping' failed: no > such cmd (response: { "errmsg" : "no such cmd", "ok" : 0.0 }).


This might be a basic stuff which I'm missing out here... Please help me out

The above is the exception which I'm getting...
Below is the code which I'm using (It's the sample demo given in the site) Note: My database is running. I'm able to create and edit the database from command line.

using System;
using System.Collections.Generic;

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace MongoDBTest
{
    public class Entity
    {
        public ObjectId Id { get; set; }
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var connectionString = "mongodb://localhost/?safe=true";
            var server = MongoServer.Create(connectionString);
            var database = server.GetDatabase("test");
            var collection = database.GetCollection<Entity>("entities");

            var entity = new Entity { Name = "Tom" };
            collection.Insert(entity);
            var id = entity.Id;

            var query = Query.EQ("_id", id);
            entity = collection.FindOne(query);

            entity.Name = "Dick";
            collection.Save(entity);

            var update = Update.Set("Name", "Harry");
            collection.Update(query, update);

            collection.Remove(query);
        }
    }
}
like image 307
mohanaravind Avatar asked Sep 07 '12 11:09

mohanaravind


1 Answers

From the mongo shell can you run these commands:

> db.version()
2.2.0
> db.runCommand("ping")
{ "ok" : 1 }
>

This is to verify that you aren't using a version of the server so old that it doesn't have the ping command.

like image 104
Robert Stam Avatar answered Nov 14 '22 23:11

Robert Stam