Mongodb基础入门(3)排序和索引
今天继续Mongodb,简单的记录下其排序和索引的使用。 在Mongodb中使用sort()方法对数据进行排序。 命令格式:db.collectionName.find().sort({key:参数}) 参数说明: -1:表示降序 1:表示升序(默认) doc集合中数据如下: db.doc.find({},{_id:0,goods_id:1
今天继续Mongodb,简单的记录下其排序和索引的使用。
在Mongodb中使用sort()方法对数据进行排序。
命令格式:db.collectionName.find().sort({key:参数})
参数说明:
-1:表示降序
1:表示升序(默认)
doc集合中数据如下:
> db.doc.find({},{_id:0,goods_id:1})
{ "goods_id" : 1 }
{ "goods_id" : 4 }
{ "goods_id" : 3 }
{ "goods_id" : 5 }
{ "goods_id" : 6 }
{ "goods_id" : 7 }
{ "goods_id" : 8 }
{ "goods_id" : 9 }
{ "goods_id" : 10 }
{ "goods_id" : 11 }
{ "goods_id" : 12 }
> db.doc.find({},{_id:0,goods_id:1}).sort({goods_id:1})
索引
1、 简介
和mysql数据类似,为了提高查询效率,Mongodb也提供索引的支持。在Mongodb中,索引可以按照字段进行升序/降序来创建,以便于排序。当然,Mongodb默认采用B-tree方式来索引。
按索引作用类型可分为:
1、 单列索引:在单个键上创建索引。
2、 组合索引:在多个键上同时创建索引,也叫多列索引。
3、 文档索引:任何类型,包括文档(document)都可以作为索引。
索引的性质可以分:
1、 普通索引:普通方式创建的索引。注意:Mongodb存在默认的_id的键,相当于主键。集合在创建之后,系统会自动在_id创建索引,该索引为系统默认,无法删除。
2、 唯一索引:某列为唯一索引时,不能添加重复文档。注意,如果文档不存在指定字段时,会将该字段默认为null,而null也会被认为重复。
3、 稀疏索引:和稀疏矩阵类似,稀疏索引就是将含有某个字段的文档进行索引,不包含该字段的文档则进行索引。一般在小部分文档含有某列时常用。
4、 哈希索引:2.4版本新增的索引方式。相比于普通索引,其速度更快。但是无法对范围查询进行优化。多用于随机性比较强的散列当中。
2、 查看索引
db.collectionName.getIndexes()
3、 创建索引
A、 创建普通单列索引:默认是升序索引,采用B-tree方式
db.collectionName.ensureIndex({field:1/-1})//1:升序;-1:降序
B、 创建多列索引:
db.collectionName.ensureIndex({field1:1/-1,field2:1/-1})
C、 创建文档索引:
A)创建普通文档索引
db.collectionName.ensureIndex({filed:1/-1})
> db.users.insert({name:"god",info:{city:"NewYork",state:"happy"}})
WriteResult({"nInserted" : 1 })
>db.users.ensureIndex({info:1})//将整个info文档作为索引
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
>db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 1,
"key" : {
"info" : 1
},
"name" : "info_1",
"ns" : "test.users"
}
]
注意:在使用索引查询的时候需要按照事先文档字段的顺序。
> db.users.find({info:{city:"NewYork",state:"happy"}})//能够利用索引查到结果
{ "_id" :ObjectId("54a79a1bc289fc3b6fcc719a"), "name" :"god", "info" : { "city
" : "NewYork", "state" : "happy" } }
>db.users.find({info:{$gte:{city:"New York"}}})//能够利用索引查到结果
{ "_id" :ObjectId("54a79a1bc289fc3b6fcc719a"), "name" :"god", "info" : { "city
" : "NewYork", "state" : "happy" } }
>db.users.find({info:{state:"happy",city:"New York"}})//不能利用索引查到结果
B)创建子文档索引
db.collectionName.ensureIndex({filed.subfield:1/-1})
> db.users.ensureIndex({"info.city":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
>db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.users"
},
{
"v" : 1,
"key" : {
"info.city" : 1
},
"name" : "info.city_1",
"ns" : "test.users"
}
]
D、创建唯一索引:可以针对多列创建唯一索引
db.collectinName.ensureIndex({filed.subfield:1/-1},{unique:true})
E、创建稀疏索引:
db.collectionName.ensureIndex({filed:1/-1},{sparse:true})
F、 创建哈希索引:可以对单个字段或字文档建立hash索引,不能针对多个列。
db.collectionName.ensureIndex({field:”hash”})
4、 删除索引
A、删除单个索引:
db.collectionName.dropIndex({filed:1/-1})
B、删除所有索引:_id列的索引不会删除。
db.collectionName.dropIndexes()
注意:在关系数据库中,表被删除后,索引随之删除。
而在Monodb中删除集合,索引仍然存在,因此需要手动删除索引。
5、 重建索引
一个集合在经过多次修改之后,将会导致集合的文件产生碎片。同样索引文件也会如此。因此可以通过索引的重建来减少索引文件碎片,提高索引效率。和mysql中的optimize table类似。命令:db.collectionName.reIndex().
索引的管理
1、查询所有索引:
system.indexes集合中包含了每个索引的详细信息,因此可以通过该命令:
db.system.indexes.find()查询已经存在的索引.
{"v" : 1, "key" : { "_id" : 1 }, "name": "_id_", "ns" : "test.doc" }
{"v" : 1, "key" : { "_id" : 1 }, "name": "_id_", "ns" : "test.category" }
{"v" : 1, "key" : { "_id" : 1 }, "name": "_id_", "ns" : "test.tea" }
{"v" : 1, "key" : { "email" : 1 },"name" : "sparse:1", "ns" : "test.tea"}
{"v" : 1, "key" : { "_id" : 1 }, "name": "_id_", "ns" : "test.users" }
{"v" : 1, "key" : { "info.city" : 1 },"name" : "info.city_1", "ns" :"test.users" }
2、查看查询计划:
为了分析查询性能及索引,一边获得更多查询方面有用的信息,可以使用如下命令:
db.collectionName.find(查询表达式).explain()
"cursor" :"BasicCursor" ——>表示索引没有发挥作用
"nscanned":1 ——>表示查询了多少个文档。
"n",:1 ——>表示返回的文档数量。
"millis":0 ——>表示整个查询的耗时。
"nscannedObjects" : 11, ——>理论上需要扫描多少行
3、后台创建索引
为已有数据的文档创建索引时,为了不阻塞其他操作,同时可以在后台创建索引,可以使用命令:db.test.ensureIndex({filed:1/-1},{"background":true})
相比阻塞创建索引而言,后台创建索引效率较低。
注意
1、如果数据集合比较小(一般来说是4m一下),此时如果使用sort()进行排序就不需要使用索引。
2、在使用组合索引查询时,查询字段的顺序必须和事先创建索引时的顺序保持一致。否则会出现上文提到的出现查不到的情况。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

We know that LLM is trained on large-scale computer clusters using massive data. This site has introduced many methods and technologies used to assist and improve the LLM training process. Today, what we want to share is an article that goes deep into the underlying technology and introduces how to turn a bunch of "bare metals" without even an operating system into a computer cluster for training LLM. This article comes from Imbue, an AI startup that strives to achieve general intelligence by understanding how machines think. Of course, turning a bunch of "bare metal" without an operating system into a computer cluster for training LLM is not an easy process, full of exploration and trial and error, but Imbue finally successfully trained an LLM with 70 billion parameters. and in the process accumulate

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

Retrieval-augmented generation (RAG) is a technique that uses retrieval to boost language models. Specifically, before a language model generates an answer, it retrieves relevant information from an extensive document database and then uses this information to guide the generation process. This technology can greatly improve the accuracy and relevance of content, effectively alleviate the problem of hallucinations, increase the speed of knowledge update, and enhance the traceability of content generation. RAG is undoubtedly one of the most exciting areas of artificial intelligence research. For more details about RAG, please refer to the column article on this site "What are the new developments in RAG, which specializes in making up for the shortcomings of large models?" This review explains it clearly." But RAG is not perfect, and users often encounter some "pain points" when using it. Recently, NVIDIA’s advanced generative AI solution

On July 24, Kuaishou video generation large model Keling AI announced that the basic model has been upgraded again and is fully open for internal testing. Kuaishou said that in order to allow more users to use Keling AI and better meet the different levels of usage needs of creators, from now on, on the basis of fully open internal testing, it will also officially launch a membership system for different categories of members. Provide corresponding exclusive functional services. At the same time, the basic model of Keling AI has also been upgraded again to further enhance the user experience. The basic model effect has been upgraded to further improve the user experience. Since its release more than a month ago, Keling AI has been upgraded and iterated many times. With the launch of this membership system, the basic model effect of Keling AI has once again undergone transformation. The first is that the picture quality has been significantly improved. The visual quality generated through the upgraded basic model

On Linux/macOS: Create the data directory and start the "mongod" service. On Windows: Create the data directory and start the MongoDB service from Service Manager. In Docker: Run the "docker run" command. On other platforms: Please consult the MongoDB documentation. Verification method: Run the "mongo" command to connect and view the server version.
