mohanaravind
mohanaravind

Reputation: 133

Unable to use MongoDB from my C# console application

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);
        }
    }
}

Upvotes: 6

Views: 1759

Answers (1)

Robert Stam
Robert Stam

Reputation: 12187

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.

Upvotes: 4

Related Questions