如何为 mongodb 中的特定文档创建复合索引
How to create a compound index for speicifc documents in mongodb
我是运行 MongoDB 4.2,请看下面我的文件长什么样:
{
"_id" : ObjectId("61e8e5b72e74b7fc3e16b632"),
"1" : 2,
"age_upon_outcome" : "1 year",
"animal_id" : "A725717",
"animal_type" : "Cat",
"breed" : "Domestic Shorthair Mix",
"color" : "Silver Tabby",
"date_of_birth" : "2015-05-02",
"datetime" : "2016-05-06 10:49:00",
"monthyear" : "2016-05-06T10:49:00",
"name" : "",
"outcome_subtype" : "SCRP",
"outcome_type" : "Transfer",
"sex_upon_outcome" : "Spayed Female",
"location_lat" : 30.6525984560228,
"location_long" : -97.7419963476444,
"age_upon_outcome_in_weeks" : 52.9215277777778
}
最重要的是,我需要创建一个复合索引,以提高查找“outcome_type”为“Transfer”的品种的查询性能,但我不确定如何创建将下面的命令缩小到仅包含 outcome_type=Transfer
的文档
db.collection.createIndex( { breed: 1 } )
会是这个:
db.collection.createIndex(
{ breed: 1 },
{ partialFilterExpression: { outcome_type: 'Transfer' } }
)
我是运行 MongoDB 4.2,请看下面我的文件长什么样:
{
"_id" : ObjectId("61e8e5b72e74b7fc3e16b632"),
"1" : 2,
"age_upon_outcome" : "1 year",
"animal_id" : "A725717",
"animal_type" : "Cat",
"breed" : "Domestic Shorthair Mix",
"color" : "Silver Tabby",
"date_of_birth" : "2015-05-02",
"datetime" : "2016-05-06 10:49:00",
"monthyear" : "2016-05-06T10:49:00",
"name" : "",
"outcome_subtype" : "SCRP",
"outcome_type" : "Transfer",
"sex_upon_outcome" : "Spayed Female",
"location_lat" : 30.6525984560228,
"location_long" : -97.7419963476444,
"age_upon_outcome_in_weeks" : 52.9215277777778
}
最重要的是,我需要创建一个复合索引,以提高查找“outcome_type”为“Transfer”的品种的查询性能,但我不确定如何创建将下面的命令缩小到仅包含 outcome_type=Transfer
的文档db.collection.createIndex( { breed: 1 } )
会是这个:
db.collection.createIndex(
{ breed: 1 },
{ partialFilterExpression: { outcome_type: 'Transfer' } }
)