Cannot use MongoDB from my c # console application

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


This may be the main material that I am missing here ... Please help me.

The above is the exception that I get ... Below is the code I use (this is an example of a demo on the site) Note. My database is running. I can create and edit the database from the 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);
        }
    }
}
+5
source share
1 answer

From the mongo shell, you can run the following commands:

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

This means that you are not using a server version so old that it does not have a ping command.

+4
source

All Articles