Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MongoDB中使用自定义容器类_C#_.net_Mongodb - Fatal编程技术网

C# 在MongoDB中使用自定义容器类

C# 在MongoDB中使用自定义容器类,c#,.net,mongodb,C#,.net,Mongodb,我想使用2.0驱动程序将自己的自定义容器插入MongoDB中的集合。但当我尝试这样做时,我看不到任何事情发生 我有以下资料: namespace ConsoleApplication2 { class Program { private static IMongoDatabase _database; static void Main(string[] args) { var dbClient = new

我想使用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; }
    }
}
有什么建议可以帮助我实现这个目标吗?
欢迎提供完整示例。

您必须注册要使用的类。 请参见此示例:

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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用MongoDB.Bson.Serialization;
使用MongoDB.Driver;
命名空间控制台应用程序2
{
班级计划
{
私有静态IMongoDatabase\u数据库;
静态void Main(字符串[]参数)
{
var dbClient=新的MongoClient(“mongodb://localhost");
_database=dbClient.GetDatabase(“测试”);
BsonClassMap.RegisterClassMap();//可以使用这个类
MongoInsert();
MongoGet();
}
静态私有void MongoInsert()
{
//创建实体
var person=new DemoClass
{
FirstName=“Jane”,
年龄=12岁,
PetNames=新列表{“夏洛克”,“沃森”}
};
//插入数据库,并等待它(本例需要)
_database.GetCollection(“people”).InsertOneAsync(person.Wait();
}
静态私有void MongoGet()
{
var names=\u database.GetCollection(“人”)
.Find(x=>x.FirstName==“Jane”)
.SortBy(x=>x.Age)
.Project(x=>x.FirstName+“”+x.LastName)//这对于foreach循环很重要
.ToListAsync();
//写入所有找到的实体
foreach(names.Result中的变量名称)
{
Console.WriteLine(名称);
}
}
}
公开课
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共整数{get;set;}
公共列表名称{get;set;}
}
}
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; }
    }
}