MongoDB:可以只为一个属性处理自定义反序列化器吗?

时间:2016-03-25 10:50:58

标签: c# mongodb

我已经进行了一些谷歌搜索,但我仍然找不到任何东西..设置自定义序列化器非常容易,但如何处理类的一个属性的自定义反序列化?

1 个答案:

答案 0 :(得分:2)

您可以使用投影实现此目的,这是一个Slazure示例:

首先,安装Slazure NuGet包:

PM> Install-Package Slazure.MongoDB

然后在Visual Studio中运行以下C#代码示例:

using System;
using System.Linq;
using MongoDB.Bson;
using SysSurge.Slazure.Core.Linq.QueryParser;
using SysSurge.Slazure.MongoDB;
using SysSurge.Slazure.MongoDB.Linq;

namespace ProjectionJsonExample
{
    class Program
    {
        static void CreateDocument()
        {
            // Create a MongoDB document.
            dynamic storage = new DynStorage("mongodb://localhost/ConsoleExample");

            // Get reference to the Employees document collection - it's created if it doesn't already exist
            dynamic employeesCollection = storage.EmployeesColl;

            // Create a document in the Employees collection for John with his email as the document id - the document is created if it doesn't already exist
            var employee = employeesCollection.Document("j.doe@example.org");
            employee.Name = "John Doe";
            employee.Salary = 50000; // John earns $50k/year
            employee.Birthdate = new DateTime(1995, 8, 18); // John was born 08/18/1995

            // Save the document to the MongoDB database
            employee.Save();
        }

        static DynDocument QueryDocument()
        {
            // Build a document query that return employees that has a salary greater than $40k/year using a dynamic LINQ query filter.
            dynamic storage = new QueryableStorage<DynDocument>("mongodb://localhost/ConsoleExample");

            QueryableCollection<DynDocument> employeesCollection = storage.EmployeesColl;
            var employeeQuery = employeesCollection
                // Query for salary greater than $40k and born later than early '95.
                .Where("Salary > 40000 and Birthdate > DateTime(1995, 1, 1)")
                // Projection makes sure that we only return Birthdate and no other properties.
                .Select("new(Birthdate)");

            // Execute the query and return the first document
            return employeeQuery.Cast<DynDocument>().First();
        }

        static void DeleteCollection()
        {
            dynamic storage = new DynStorage("mongodb://localhost/ConsoleExample");

            // Delete EmployeesColl collection if it exists to make sure we start with fresh data
            storage.Delete("EmployeesColl");
        }

        static void Main(string[] args)
        {
            // Delete EmployeesColl collection if it exists to make sure we start with fresh data
            DeleteCollection();

            // Add a employee document to the MongoDB database
            CreateDocument();

            // Get employee
            var employee = QueryDocument();
            var json = employee.GetBsonDocument().ToJson();
            Console.WriteLine(json);

            Console.WriteLine("Press a key to continue...");
            Console.ReadKey();
        }
    }
}

运行这个小例子的结果如下:

{ "_id" : "79868b41-17d5-4737-b99d-c92e492bb502", "Birthdate" : ISODate("1995-08-17T22.00.00Z") }
Press a key to continue...

正如您在上面所看到的,即使我们创建的文档具有 Name Salary 属性,由于我们指定的投影,这些属性也未从MongoDB服务器检索到在我们的代码中使用Select("new(Birthdate)")