Python pymongo排序返回的记录不';我没有分类键

Python pymongo排序返回的记录不';我没有分类键,python,mongodb,sorting,pymongo,Python,Mongodb,Sorting,Pymongo,我正在对一个集合的“日期”列进行排序,以查找该集合中的最短日期。但是,它会返回一条缺少日期键的记录。下面是片段。这是虫子吗 date_records = usercollection.find({'customer_id':'abc'}).sort('join_date',1).limit(1) for record in date_records: print record # prints a record that doesn't have the join_date key

我正在对一个集合的“日期”列进行排序,以查找该集合中的最短日期。但是,它会返回一条缺少日期键的记录。下面是片段。这是虫子吗

date_records = usercollection.find({'customer_id':'abc'}).sort('join_date',1).limit(1)
for record in date_records:
    print record # prints a record that doesn't have the join_date key
    print record['join_date']
输出:

{ "_id" : ObjectId("94dbe4c6ea890e28113d7664"), "region" : "Virginia", "country_code" : "US", "customer_id" : "abc"}

KeyError: u'join_date'
这不是一个bug,应该如何工作:

比较将不存在的字段视为空BSON 对象因此,对文档{}和{a:null中的a字段进行排序 }将按排序顺序将文件视为同等文件

既然你注意到了:

我想从所有具有最早日期的记录中检索最早的日期 加入日期字段

使用以下方法检查是否存在加入日期:


这不是一个bug。快速问题:是否只对包含“加入日期”字段的记录进行排序?是的,我想从所有包含“加入日期”字段的记录中检索最早的日期。
usercollection.find({
    'customer_id': 'abc',
    'join_date': {'$exists': True}
}).sort('join_date', 1)