简体   繁体   English

在mono中使用c#驱动比较mongo集合的两个字段

[英]Comparing two fields of mongo collection using c# driver in mono

Am completely new to Mongodb and C# driver.对 Mongodb 和 C# 驱动程序完全陌生。

Development is being done using Monodevelop on Ubuntu 14.04 and Mongodb's version is 3.2.10 :在 Ubuntu 14.04 上使用 Monodevelop 进行开发,Mongodb 的版本是 3.2.10 :

Currently my code has a POCO as below:目前我的代码有一个 POCO 如下:

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.已经能够创建集合并添加用户。

How do I find users, whose LastModifiedAt timestamp is greater than LastSyncedAt timestamp ?如何找到 LastModifiedAt 时间戳大于 LastSyncedAt 时间戳的用户? Did some searching, but haven't been able to find the answer.进行了一些搜索,但一直无法找到答案。

Any suggestions would be of immense help任何建议都会有很大帮助

Thanks谢谢

Actually, it is not very simple.实际上,这不是很简单。 This should be possible with querysuch as :这应该可以通过查询实现,例如:

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

But unfortunetly MongoDriver could not translate this expression.但不幸的是 MongoDriver 无法翻译这个表达式。 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();

Or send json query, because MongoDb itself is able to do it:或者发送json查询,因为MongoDb本身就可以做到:

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

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.而且,是的,您的模型类需要一个 Id - 属性,我没有首先提到它,因为我认为您确实有一个,只是没有发布在问题中。

There is another way to do it.还有另一种方法可以做到。 First lets declare collection:首先让我们声明集合:

var collection = Database.GetCollection<BsonDocument>("CollectionName");

Now lets add our project:现在让我们添加我们的项目:

var pro = new BsonDocument {
                {"gt1", new BsonDocument {
                    { "$gt", new BsonArray(){ "$LastModifiedAt", "$LastSyncedAt" }
                    }
                } },
                {"Name", true },
                {"LastModifiedAt", true },
                {"LastSyncedAt", true }
                };

Now lets add our filter:现在让我们添加我们的过滤器:

var filter = Builders<BsonDocument>.Filter.Eq("gt1", true);

We'll aggregate our query:我们将汇总我们的查询:

var aggregate = collection.Aggregate(new AggregateOptions { AllowDiskUse = true })
                .Project(pro)
                .Match(filter)

Now our query is ready.现在我们的查询准备好了。 We can check our query as follow:我们可以检查我们的查询如下:

var query=aggregate.ToString();

Lets run our query as follow:让我们按如下方式运行我们的查询:

var query=aggregate.ToList();

This with return the required data in list of bson documents.这将返回 bson 文档列表中所需的数据。

This solution will work mongo c# driver 3.6 or above.此解决方案适用于 mongo c# 驱动程序 3.6 或更高版本。 Please comment in case of any confusion.如有任何混淆,请发表评论。 Hopefully i'll able to explain this.希望我能够解释这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM