我想根据标题,开始日期和开始时间查找mongodb中是否存在事件。这是存储在mongo db中的一个事件:
{
"_id" : ObjectId("55bb834330b16c0bdadc6cd8"),
"event_id" : 26757,
"title" : "Corporate Training using cinema",
"title_slug" : "the-spiritual-companys-corporate-training-using-cinema",
"subtitle" : "",
"description" : "The spiritual company has a session where in it intends to provide corporate training via the medium of cinema.\r\n\r\nHow will they impart this quirk? Well be there to find out mate.\r\n\r\nFees - INR 1500/-",
"location" : ObjectId("55bb834330b16c0bdadc6cd6"),
"start_time" : ISODate("1900-01-01T10:00:00Z"),
"end_time" : ISODate("1900-01-01T01:00:00Z"),
"start_date" : ISODate("2015-08-01T00:00:00Z"),
"end_date" : ISODate("2015-08-01T00:00:00Z"),
"post_date" : ISODate("2015-07-31T15:12:46Z"),
"last_modified" : ISODate("2015-07-31T15:12:46Z"),
"all_day" : false,
"image_url" : "",
"thumbnail_url" : "",
"publish_status" : 4,
"moderation_status" : 4,
"categories" : [
ObjectId("55bb7d9b30b16c0bdadc5c8c")
],
"tags" : [ ],
"organizer" : ObjectId("55bb834330b16c0bdadc6cd7"),
"editor" : ObjectId("55bb44da30b16c7b1a39157b"),
"repeat_bool" : false,
"group_id" : "26757"
}
我通过搜索标题和开始日期获得了成功的结果但是当我包含时间部分时,我得到一个空列表。 这是我在python 2.7 +中的mongoengine中的查询:
from mongoengine import *
from model import *
from datetime import date
db = connect('xyz')
#The query starts here
#The model contains the Event document
b = date(2015,8,1).isoformat()
a = Event.objects(title = "Corporate Training using cinema", start_date = b, start_time = "1900-01-01T10:00:00Z")
当我执行上面的查询时,我得到的结果是一个空列表,即它没有在db中找到该事件。
>>>a
>>>[]
如果我没有在查询中包含start_time,那么它的执行会得到一个成功的结果,但我必须在查询中包含start_time。 如何比较mongoengine中的start_time或时间部分?
答案 0 :(得分:2)
您将字符串与ISO日期对象匹配首先将输入转换为ISO日期对象。我不知道Python的语法,但在php中我使用
$match=new MongoDate(timestamp)
在语法中包含上述类