2016-03-25 2 views
2

私はいくつかのGoogle検索を行いましたが、まだ何も見つかりませんでした..カスタムシリアライザを設定するのはかなり簡単ですが、クラスの1つのプロパティのカスタムデシリアライズを処理する方法は簡単です。MongoDB:カスタムデシリアライザを1つのプロパティにしか扱うことはできませんか?

+0

http://stackoverflow.com/questions/19307752/deserializing-polymorphic-json-classes-without-type-in​​formation-using-json-net –

答えて

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("[email protected]"); 
      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... 

あなたは私たちが作成した文書が原因我々は我々のコードでSelect("new(Birthdate)")で指定された投影にこれらはMongoDBのサーバから取得されていなかった名前給与プロパティを持っていたにも関わらず、上記の見ることができるように。

関連する問題