JAVA全系列 教程
3762个小节阅读:7088.9k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
复合索引是指单个索引结构指向多个字段,下图展示了在两个字段上建立索引
使用下面的数据来创建一个复合索引
xxxxxxxxxx
{
"item": "Banana",
"category": ["food", "produce", "grocery"],
"location": "4th Street Store",
"stock": 4,
"type": "cases"
}
在item和stock字段上建立升序索引
xxxxxxxxxx
db.products.createIndex( { "item": 1, "stock": 1 } )
多键索引用于为数组中的元素创建索引
先创建集合inventory,使用下面的数据来创建多键索引
xxxxxxxxxx
[{ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8, 9 ] },
{ _id: 6, type: "food", item: "bbb", ratings: [ 5, 9 ] },
{ _id: 7, type: "food", item: "ccc", ratings: [ 9, 5, 8 ] },
{ _id: 8, type: "food", item: "ddd", ratings: [ 9, 5 ] },
{ _id: 9, type: "food", item: "eee", ratings: [ 5, 9, 5 ] }]
xxxxxxxxxx
db.inventory.createIndex( { ratings: 1 } )
MongoDB使用多键索引查找在“ratings”数组中有“5”的文档,然后,MongoDB检索这些文档并筛选“ratings”数组等于查询数组“[5,9]”的文档。
xxxxxxxxxx
db.inventory.find( { ratings: [ 5, 9 ] } )