如何解决一个年/月/日日期到一个更具体的日期,在MongoDB的时间数据?如何解决、具体、日期、时间

2023-09-05 23:08:56 作者:旧巷听风

我的工作转换我的博客上使用 /年/月/日键入网址。我虽然碰到了pretty的不好的问题的理念。我想只有到时/分分辨率,但当然我所有的作品都保存有具体越好一个完整的时间(我并不想改变这种状况)。

究竟如何我去解决一个具体日期来一个不太具体日期与MongoDB的?我使用的是10gen公司的官方MongoDB的映射为.NET。

我的数据模型是这样的:

 公共类BlogEntryData
{
    [BsonId]
    公众的ObjectId ID;
    公共字符串名称;
    公共字符串文本;
    公开日期时间发布;
    公开日期时间编辑;
    公开名单<字符串>标签;
    [BsonDefaultValue(真)
    公共BOOL发布;
}
 

和我开始使用我的博客条目

 变种C = Config.GetDB()GetCollection< BlogEntryData>(项目);
BlogEntryData进入;
尝试
{
    进入= c.FindOneByIdAs< BlogEntryData>(新的ObjectId(RouteParams [ID]));
}
抓住
{
    抛出新HttpException(404,博客条目没有找到);
}

如果(入口== NULL)
{
    抛出新HttpException(404,博客条目没有找到);
}
 

我知道如何去改变它,而不使用ID类似数字搜索字段或什么,但我从来没有质疑反对的日期。

另外,顺便说一句,会是更好的存储非规范化作为一个字符串在我的入门机型的URL日期?我只是想这样做,如果索引没有约会什么的正常工作

解决方案 与补贴相比,充电行业更需要运营

您可以DateTime对象进行重新psented在蒙戈以下语法$ P $;

  ISODate(2012-10-11T17:33:51.994Z)
 

这是规范的,可排序的格式。因此,它具有年启动,那么月,日,然后小时等,所以它已经至少到最具体的顺序。有几件事情,你可以用这个做的。

您可以做字符串解析了它,只用字符串的第一部分。但是,这不是太聪明了。 您可以简单复制的年份和月份作为单独的领域,在除了应用于ISODate()。这里的好处是,你可以直接引用这些字段,你可以索引它们具体。 最后,您可以搜索使用范围,可以用这样的简写;   

db.so.find({发布时间:{$ GT:ISODate(2012年10月11日),   $其中:ISODate(2012年10月12日)}})

我不建议1所有,但2和3会很好地工作。

和顺便说一句,这两个机制将很好地转化为C#的驱动程序。因此,通过更全面的回答方式;

  VAR dayOfInterest =新的日期时间(2012,10,11);
        变种rangeOfInterest = dayOfInterest.AddDays(1);

// ...

         VAR项= c.FindAs< BlogEntryData>(Query.And(
                                    Query.GT(发布,dayOfInterest)
                                    Query.LT(发布,rangeOfInterest)
                         ));
 

I'm working on converting my blog over to use /year/month/day type URLs. I've ran into a pretty bad problem with the concept though. I want to only have down to hour/minute resolution, but of course all of my entries are stored with a full time specific as possible(and I don't want to change that).

How exactly do I go about resolving a specific date to a less specific date with MongoDB? I'm using 10gen's official MongoDB mapper for .Net.

My data-model looks like:

public class BlogEntryData
{
    [BsonId]
    public ObjectId ID;
    public string Title;
    public string Text;
    public DateTime Posted;
    public DateTime Edited;
    public List<string> Tags;
    [BsonDefaultValue(true)]
    public bool Publish;
}

and I get an entry for my blog using

var c=Config.GetDB().GetCollection<BlogEntryData>("entries");
BlogEntryData entry;
try
{
    entry=c.FindOneByIdAs<BlogEntryData>(new ObjectId(RouteParams["id"]));
}
catch
{
    throw new HttpException(404,"Blog entry not found");
}

if(entry==null)
{
    throw new HttpException(404,"Blog entry not found");
}

I'm know how to change it to use instead of ID something like a numerical search field or whatever, but I've never queried against dates.

Also, as an aside, would it be better to store the "URL date" denormalized as a string in my entry model? I'd only want to do this if indexes didn't work properly on dates or something

解决方案

your DateTimes can be represented in Mongo with the following syntax;

ISODate("2012-10-11T17:33:51.994Z")

Which is the canonical, sortable format. So it starts with year, then month, then day, then hour etc, so it's already least to most specific order. There's a couple of things you can do with this.

You can do string parsing over it, just using the first part of the string. But that's not too smart. You can simply duplicate the year and month as separate fields, in addition to the ISODate(). The advantage here is that you can reference those fields directly and you can index them specifically. Finally, you can search using a range, the shorthand for which can be used thus;

db.so.find({Posted:{$gt: ISODate("2012-10-11"), $lt:ISODate("2012-10-12")}})

I wouldn't recommend 1 at all, but 2 and 3 would work very well.

And incidentally, both those mechanisms would translate well into the C# driver. So by way of a more complete answer;

        var dayOfInterest = new DateTime(2012, 10, 11);
        var rangeOfInterest = dayOfInterest.AddDays(1);

// ...

         var entries = c.FindAs<BlogEntryData>(Query.And(
                                    Query.GT("Posted", dayOfInterest),
                                    Query.LT("Posted", rangeOfInterest)
                         ));