我已经进行了一些谷歌搜索,但仍然一无所获。设置自定义序列化程序非常容易,但如何处理类的一个属性的自定义反序列化?
最佳答案
你可以使用投影来实现,这里是一个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)")。
关于c# - MongoDB:是否可以只处理一个属性的自定义反序列化器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36218537/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的