如何为“日期”创建索引?
CentOS7、MongoDB服务器版本:3.4.2
db.animals.createIndex('date')
{
"ok" : 0,
"errmsg" : "The field 'key' must be an object, but got string",
"code" : 14,
"codeName" : "TypeMismatch"
}
db.animals.find({}, {date: 1}).limit(1)
{ "_id" : 3477, "date" : ISODate("2016-12-22T09:38:59Z") }
最佳答案
您可以使用以下方法在键 date
上创建简单索引:
Ascending order: db.animals.createIndex({'date':1})
Descending order: db.animals.createIndex({'date':-1})
您可能需要看一下 indexing添加索引前的文档
关于mongodb - 如何为 "date"创建索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42887627/