如何为" date"?
创建索引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") }
答案 0 :(得分:20)
您可以使用以下方法在密钥date
上创建简单索引:
Ascending order: db.animals.createIndex({'date':1})
Descending order: db.animals.createIndex({'date':-1})
在添加索引
之前,您可能需要查看indexing doc答案 1 :(得分:1)
对于单个字段,您无需反转索引,因为mongodb可以在任一方向上使用索引。仅当您具有复合索引并且排序方向朝不同方向时,这才有意义。