本文介绍了在Mono中使用C#驱动程序比较mongo集合的两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是Mongodb和C#驱动程序的全新产品.

Am completely new to Mongodb and C# driver.

开发工作已在Ubuntu 14.04上使用Monodevelop完成,而Mongodb的版本为3.2.10:

Development is being done using Monodevelop on Ubuntu 14.04 and Mongodb's version is 3.2.10 :

当前我的代码具有如下POCO:

Currently my code has a POCO as below:

public class User
{
    public String Name { get; set;}
    public DateTime LastModifiedAt { get; set;}
    public DateTime LastSyncedAt { get; set;}

     public User ()
    {

    }
}

已经能够创建收藏集并添加用户.

Have been able to create a collection and also to add users.

如何查找LastModifiedAt时间戳大于LastSyncedAt时间戳的用户?进行了一些搜索,但是找不到答案.

How do I find users, whose LastModifiedAt timestamp is greater than LastSyncedAt timestamp ? Did some searching, but haven't been able to find the answer.

任何建议都会有很大帮助

Any suggestions would be of immense help

谢谢

推荐答案

实际上,这不是很简单.使用诸如:

Actually, it is not very simple. This should be possible with querysuch as :

var users = collection.Find(user => user.LastModifiedAt > user.LastSyncedAt).ToList();

但是不幸的是,MongoDriver无法翻译该表达式.您可以查询所有用户并在客户端进行过滤:

But unfortunetly MongoDriver could not translate this expression. You could either query all Users and filter on the client side:

var users = collection.Find(Builders<User>.Filter.Empty)
                      .ToEnumerable()
                      .Where(user => user.LastModifiedAt > user.LastSyncedAt)
                      .ToList();

或者发送json查询,因为MongoDb本身可以做到:

Or send json query, because MongoDb itself is able to do it:

var jsonFliter = "{\"$where\" : \"this.LastModifiedAt>this.LastSyncedAt\"}";
var users = collection.Find(new JsonFilterDefinition<User>(jsonFliter))
                      .ToList();

是的,您需要为您的模型类提供一个Id-属性,我没有首先提到它,因为我认为您确实有一个,只是没有在问题中发帖.

And, yes, you need an Id - Property for your model class, i haven't mentioned it first, because i thought you do have one, just not posted in the question.

这篇关于在Mono中使用C#驱动程序比较mongo集合的两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:29