mongoDB中多字段对象的索引

mongoDB中多字段对象的索引,mongodb,Mongodb,我有一个map reduce调用生成的集合,密钥由两个字段组成,所以我的id设置如下: { "_id": { "ts": ISODate("2014-04-22T13: 46: 00.0Z"), "own": "LP2" } //... my fields } 索引是在_id上自动构造的: { "v": NumberInt(1), "key": { "_id": NumberInt(1) }, "ns": "DB.COLLE

我有一个map reduce调用生成的集合,密钥由两个字段组成,所以我的id设置如下:

{
   "_id": {
     "ts": ISODate("2014-04-22T13: 46: 00.0Z"),
     "own": "LP2" 
  }
  //... my fields
}
索引是在_id上自动构造的:

{
   "v": NumberInt(1),
   "key": {
     "_id": NumberInt(1) 
  },
   "ns": "DB.COLLECTION_NAME",
   "name": "_id_" 
}
但当我进行查询时,我通常希望在ts字段上排序

我知道我可以在它上面构造一个索引,但是我想知道_id上的索引是否已经支持它了,比如复合索引。这样可以节省插入的时间


提前感谢您的回答。

我认为不会。测试这一点的方法是插入一些测试数据并尝试一些测试查询。当我运行db.collectionname.find{}.sort{{u id.ts:-1}.explain时,很明显它没有使用索引进行排序:

{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 13,
    "nscannedObjects" : 13,
    "nscanned" : 13,
    "nscannedObjectsAllPlans" : 13,
    "nscannedAllPlans" : 13,
    "scanAndOrder" : true,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "server" : "local:27017",
    "filterSet" : false
}
请注意,Scanander是正确的

在添加了一个新索引之后,我看到了更好的结果:

db.collectionname.ensureIndex({"_id.ts":1})
db.collectionname.find({}).sort({"_id.ts":-1}).explain()
{
    "cursor" : "BtreeCursor _id.ts_1 reverse",
    "isMultiKey" : false,
    "n" : 13,
    "nscannedObjects" : 13,
    "nscanned" : 13,
    "nscannedObjectsAllPlans" : 13,
    "nscannedAllPlans" : 13,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "_id.ts" : [
            [
                {
                    "$maxElement" : 1
                },
                {
                    "$minElement" : 1
                }
            ]
        ]
    },
    "server" : "local:27017",
    "filterSet" : false
}
请注意,Scanander现在为false