【问题标题】:Using custom container class in MongoDB在 MongoDB 中使用自定义容器类
【发布时间】:2015-07-01 09:00:50
【问题描述】:

我想使用 2.0 驱动程序将我自己的自定义容器插入到 MongoDB 中的集合中。但是当我尝试这样做时,我看不到任何事情发生。

我有以下几点:

namespace ConsoleApplication2
{
    class Program
    {
        private static IMongoDatabase _database;

        static void Main(string[] args)
        {
            var dbClient = new MongoClient("mongodb://localhost");
            _database = dbClient.GetDatabase("Test");

            var demo = new DemoClass() { Adderss = "add", Name = "Test friend" };
            var collection = _database.GetCollection<DemoClass>("DemoClass");
            collection.InsertOneAsync(demo);
        }
    }

    [BsonDiscriminator(RootClass = true)]
    public class DemoClass
    {
        [BsonId]
        public object Id { get; set; }
        public string Adderss { get; set; }
        public string Name { get; set; }
    }
}

集合未创建,但mongod 告诉我连接已建立。

2015-07-01T10:58:09.998+0200 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50317 #11 (4 connections now open)
2015-07-01T10:58:10.178+0200 I NETWORK  [conn11] end connection 127.0.0.1:50317 (3 connections now open)

对我如何让它发挥作用有什么建议吗? 欢迎提供完整示例。

【问题讨论】:

    标签: c# .net mongodb


    【解决方案1】:

    你必须注册你想使用的类。 看这个例子:

    using System;
    using System.Collections.Generic;
    using MongoDB.Bson.Serialization;
    using MongoDB.Driver;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            private static IMongoDatabase _database;
    
            static void Main(string[] args)
            {
                var dbClient = new MongoClient("mongodb://localhost");
                _database = dbClient.GetDatabase("Test");
    
                BsonClassMap.RegisterClassMap<DemoClass>(); //This class can be used
                MongoInsert();
                MongoGet();
            }
    
            static private void MongoInsert()
            {
                //Create an entity
                var person = new DemoClass
                {
                    FirstName = "Jane",
                    Age = 12,
                    PetNames = new List<dynamic> { "Sherlock", "Watson" }
                };
    
                //Insert into db, and wait for it (wanted for this example)
                _database.GetCollection<dynamic>("people").InsertOneAsync(person).Wait();
            }
    
            static private void MongoGet()
            {
                var names = _database.GetCollection<DemoClass>("people")
                    .Find(x => x.FirstName == "Jane")
                    .SortBy(x => x.Age)
                    .Project(x => x.FirstName + " " + x.LastName) //This is important for the foreach-loop
                    .ToListAsync();
    
                //Write all found entities
                foreach (var name in names.Result)
                {
                    Console.WriteLine(name);
                }
            }
        }
    
        public class DemoClass
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Age { get; set; }
            public List<dynamic> PetNames { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多