在MongoDB中正确使用

在MongoDB中正确使用

本文介绍了在MongoDB中正确使用/处理DateTimeOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class ScheduledEvent : Event
{
    public DateTimeOffset StartDateTime { get; set; }
}

StartDateTime = 5/27/2013 2:09:00 AM +00:00表示太平洋标准时间2013年5月26日下午7:09

StartDateTime = 5/27/2013 2:09:00 AM +00:00 representing 05/26/2013 07:09 PM PST

MongoDB中记录的内容

What's recorded in MongoDB:

> db.ScheduledEvent.find().toArray()
[
        {
                "_id" : BinData(3,"ZE2p31dh00qb6kglsgHgAA=="),
                "Title" : "Today 26th at 7:09pm",
                "Length" : "00:00:00",
                "MoreInformation" : "http://1.com",
                "Speakers" : [
                        {
                                "_id" : BinData(3,"ndzESsQGukmYGmMgKK0EqQ=="),
                                "Name" : "Mathias Brandewinder"
                        }
                ],
                "Location" : {
                        "_id" : BinData(3,"AAAAAAAAAAAAAAAAAAAAAA=="),
                        "Name" : "Somwhere "
                },
                "Organizers" : [
                        {
                                "_id" : BinData(3,"AAAAAAAAAAAAAAAAAAAAAA=="),
                                "Name" : null
                        }
                ],
                "CreatedOn" : [
                        NumberLong("635052144104050898"),
                        0
                ],
                "StartDateTime" : [
                        NumberLong("635052173400000000"),
                        0
                ]
        }
]

我意识到StartDateTime是作为Ticks存储在MongoDB中的.

I realize that StartDateTime is stored as Ticks in MongoDB.

var dateMarker = DateTimeOffset.UtcNow;
var nextDay = dateMarker.AddDays(1);

此查询无效:

var today = EventRepoistory.All().Where(z => z.StartDateTime >= dateMarker && z.StartDateTime < nextDay).OrderByDescending(z => z.StartDateTime).ToList();

我已将查询显示添加到Mongo C#驱动程序,其中显示以下查询:

I have added a query display to the Mongo C# driver which shows the following query:

{ "$query" : { "StartDateTime" : { "$gte" : [NumberLong("635052168609734070"), 0], "$lt" : [NumberLong("635053032609734070"), 0] } }, "$orderby" : { "StartDateTime" : -1 } }

服务器= 6350521 73400000000

Server = 6350521 73400000000

上限= 6350530 32609734070

Upper bound = 6350530 32609734070

问题:为什么MongoDB查询不返回任何内容?

已研究:

MongoDB和DateTimeOffset类型,但它表明LINQ提供程序正在做什么?

MongoDB and DateTimeOffset type but it seams that LINQ provider is doing what it's supposed to?

尝试:

db.ScheduledEvent.find({ "StartDateTime" : { "$gte" : [NumberLong("1"), 0]  } }  )

没有结果.

推荐答案

在此处找到类似的答案:MongoDB和DateTimeOffset类型(如您在问题中所述)

A similar answer is found here: MongoDB and DateTimeOffset type (as you note in your question)

我通过执行以下操作来使用C#驱动程序:

I got this working with the C# Driver by doing the following:

var query = Query.GT("StartDateTime.0", startDate.Ticks);
var json = query.ToJson();

产生此JSON:

{ "StartDateTime.0" : { "$gt" : NumberLong("635251617859913739") } }

上面的JSON有效.根据链接的答案,原因是DateTimeOffset是一个数组.

The above JSON works. Per the linked answer the reason is that DateTimeOffset is an array.

当我使用LINQ时(如您所述),JSON得到了不同的结果.

When I use LINQ I get (as you noted) a different result for the JSON.

var query = from r in col.AsQueryable<MyObjectType>()
    where r.StartDateTime>= startDate && r.StartDateTime< endDate
    select r;

上面的LINQ查询产生以下JSON:

The LINQ query above produces the following JSON:

{ "StartDateTime" : { "$gte" : [NumberLong("635251617859913739"), 0], "$lt" : [NumberLong("635251635859913739"), 0] } }

我不确定是否需要修复C#驱动程序中的LINQ提供程序来处理DateTimeOffset,但是使用查询构建器指定DateTimeOffset数组的第一个元素(StartDateTime.0)是我可以完成此工作的唯一方法.

I am not sure if the LINQ provider in the C# driver needs to be fixed to handle DateTimeOffset but using the Query builder to specify the first element of the DateTimeOffset array (StartDateTime.0) was the only way I got this working.

这篇关于在MongoDB中正确使用/处理DateTimeOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:00