MongoDB: min(), max() doesn't support embedded document

我怕爱的太早我们不能终老 提交于 2020-01-06 15:01:51

问题


I have applied index on embedded document field which is of type date. Due to unpredictable behavior of MongoDB query with $lt & $gt operators I am trying to use min() max() functions of Cursor which force both lower & upper bounds on the index. But when I used it, it gave me error:

planner returned error: unable to find relevant index for max/min query"

Query looks like:

db.user.find().min({'record.date':ISODate("2014-12-01")}).max({'record.date':ISODate("2014-12-01")}).explain()

I have indexed the field 'record.date'. I even tried to force it with hint() but of no use. It showed me this error:

planner returned error: hint provided does not work with min query"

Somewhere on the Internet I saw on one of the forums that we can query with embedded fields in min(), max() functions. Field 'record' is an array of sub-documents. I have another field of type date in main document for which min(), max() worked fine (this field is indexed too). Can anyone guess why is this happening?

sample document: user:

{name:'ad',dob: ISODate('yyyy-mm-dd'),createdAt: ISODate(),...,record:[

{date:ISODate:(),... },...]}

Index was created as:

db.user.ensureIndex({'record.date':1})

Thanks.

来源:https://stackoverflow.com/questions/28345738/mongodb-min-max-doesnt-support-embedded-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!