MongoDB provides an interesting "multikey" feature that can automatically index arrays of an object's values.
# 测试
> db.articles1.save( { name: "Warm Weather", author: "Digoal", tags: ['weather', 'hot', 'record', 'april'] } )
> db.articles1.find()
{ "_id" : ObjectId("4d23e43f9ae655253f80e95e"), "name" : "Warm Weather", "author" : "Digoal", "tags" : [ "weather", "hot", "record", "april" ] }
> db.articles1.find( { tags: 'april' } )
{ "_id" : ObjectId("4d23e43f9ae655253f80e95e"), "name" : "Warm Weather", "author" : "Digoal", "tags" : [ "weather", "hot", "record", "april" ] }
# 使用[]之后就是精确匹配了
> db.articles1.find( { tags: ['april'] } )
# 使用$all的话表示包含所有[]列出的values的array
> db.articles1.find( { tags: {$all : ['april']} } )
{ "_id" : ObjectId("4d23e43f9ae655253f80e95e"), "name" : "Warm Weather", "author" : "Digoal", "tags" : [ "weather", "hot", "record", "april" ] }
# 很明显现在没有索引
> db.articles1.find( { tags: {$all : ['april']} } ).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"indexBounds" : {
}
}
# array 上的索引
> db.articles1.ensureIndex( { tags : 1 } );
> db.articles1.find( { tags: {$all : ['april']} } ).explain()
{
"cursor" : "BtreeCursor tags_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"indexBounds" : {
"tags" : [
[
"april",
"april"
]
]
}
}
# array中内嵌文档
db.articles2.save( { name: "Warm Weather", author: "Digoal", tags: ['weather', 'hot', 'record', 'april'], comments : [{"nick" : "digoal" , "say" : "very good"},{"nick" : "francs" , "say" : "very good"}] } )
> db.articles2.find()
{ "_id" : ObjectId("4d23e5109ae655253f80e960"), "name" : "Warm Weather", "author" : "Digoal", "tags" : [ "weather", "hot", "record", "april" ], "comments" : [
{
"nick" : "digoal",
"say" : "very good"
},
{
"nick" : "francs",
"say" : "very good"
}
] }
# 并行array不允许建立索引,原因是产生笛卡尔积
> db.articles3.save( { name: "Warm Weather", author: "Digoal", tags: ['weather', 'hot', 'record', 'april'],"tags2" : ["ok"] } )
> db.articles3.ensureIndex( { tags : 1,tags2 :1} );
cannot index parallel arrays [tags2] [tags]
# 单一的array可以和非array key组成复合索引 :
> db.articles3.ensureIndex( { tags : 1,name :1} );
> db.articles3.getIndexes()
[
{
"name" : "_id_",
"ns" : "test.articles3",
"key" : {
"_id" : 1
}
},
{
"_id" : ObjectId("4d23e6659ae655253f80e963"),
"ns" : "test.articles3",
"key" : {
"tags" : 1,
"name" : 1
},
"name" : "tags_1_name_1"
}
]
# 测试
> db.articles1.save( { name: "Warm Weather", author: "Digoal", tags: ['weather', 'hot', 'record', 'april'] } )
> db.articles1.find()
{ "_id" : ObjectId("4d23e43f9ae655253f80e95e"), "name" : "Warm Weather", "author" : "Digoal", "tags" : [ "weather", "hot", "record", "april" ] }
> db.articles1.find( { tags: 'april' } )
{ "_id" : ObjectId("4d23e43f9ae655253f80e95e"), "name" : "Warm Weather", "author" : "Digoal", "tags" : [ "weather", "hot", "record", "april" ] }
# 使用[]之后就是精确匹配了
> db.articles1.find( { tags: ['april'] } )
# 使用$all的话表示包含所有[]列出的values的array
> db.articles1.find( { tags: {$all : ['april']} } )
{ "_id" : ObjectId("4d23e43f9ae655253f80e95e"), "name" : "Warm Weather", "author" : "Digoal", "tags" : [ "weather", "hot", "record", "april" ] }
# 很明显现在没有索引
> db.articles1.find( { tags: {$all : ['april']} } ).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"indexBounds" : {
}
}
# array 上的索引
> db.articles1.ensureIndex( { tags : 1 } );
> db.articles1.find( { tags: {$all : ['april']} } ).explain()
{
"cursor" : "BtreeCursor tags_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"indexBounds" : {
"tags" : [
[
"april",
"april"
]
]
}
}
# array中内嵌文档
db.articles2.save( { name: "Warm Weather", author: "Digoal", tags: ['weather', 'hot', 'record', 'april'], comments : [{"nick" : "digoal" , "say" : "very good"},{"nick" : "francs" , "say" : "very good"}] } )
> db.articles2.find()
{ "_id" : ObjectId("4d23e5109ae655253f80e960"), "name" : "Warm Weather", "author" : "Digoal", "tags" : [ "weather", "hot", "record", "april" ], "comments" : [
{
"nick" : "digoal",
"say" : "very good"
},
{
"nick" : "francs",
"say" : "very good"
}
] }
# 并行array不允许建立索引,原因是产生笛卡尔积
> db.articles3.save( { name: "Warm Weather", author: "Digoal", tags: ['weather', 'hot', 'record', 'april'],"tags2" : ["ok"] } )
> db.articles3.ensureIndex( { tags : 1,tags2 :1} );
cannot index parallel arrays [tags2] [tags]
# 单一的array可以和非array key组成复合索引 :
> db.articles3.ensureIndex( { tags : 1,name :1} );
> db.articles3.getIndexes()
[
{
"name" : "_id_",
"ns" : "test.articles3",
"key" : {
"_id" : 1
}
},
{
"_id" : ObjectId("4d23e6659ae655253f80e963"),
"ns" : "test.articles3",
"key" : {
"tags" : 1,
"name" : 1
},
"name" : "tags_1_name_1"
}
]