mongoDB's Multikeys feature

简介:
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"
        }
]
目录
相关文章
|
NoSQL
MongoDB · Feature · In-place update in MongoDB
There is a great new feature in the release note of MongoDB 3.5.12. Faster In-place Updates in WiredTiger This work brings improvements to in-p...
1773 0
|
NoSQL MongoDB 数据库
数据库数据恢复—MongoDB数据库数据恢复案例
MongoDB数据库数据恢复环境: 一台操作系统为Windows Server的虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 工作人员在MongoDB服务仍然开启的情况下将MongoDB数据库文件拷贝到其他分区,数据复制完成后将MongoDB数据库原先所在的分区进行了格式化操作。 结果发现拷贝过去的数据无法使用。管理员又将数据拷贝回原始分区,MongoDB服务仍然无法使用,报错“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
缓存 NoSQL Linux
在CentOS 7系统中彻底移除MongoDB数据库的步骤
以上步骤完成后,MongoDB应该会从您的CentOS 7系统中被彻底移除。在执行上述操作前,请确保已经备份好所有重要数据以防丢失。这些步骤操作需要一些基本的Linux系统管理知识,若您对某一步骤不是非常清楚,请先进行必要的学习或咨询专业人士。在执行系统级操作时,推荐在实施前创建系统快照或备份,以便在出现问题时能够恢复到原先的状态。
1525 79
|
存储 NoSQL MongoDB
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
586 8
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
|
运维 NoSQL 容灾
告别运维噩梦:手把手教你将自建 MongoDB 平滑迁移至云数据库
程序员为何逃离自建MongoDB?扩容困难、运维复杂、高可用性差成痛点。阿里云MongoDB提供分钟级扩容、自动诊断与高可用保障,助力企业高效运维、降本增效,实现数据库“无感运维”。
|
NoSQL MongoDB 数据库
数据库数据恢复——MongoDB数据库服务无法启动的数据恢复案例
MongoDB数据库数据恢复环境: 一台Windows Server操作系统虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 管理员在未关闭MongoDB服务的情况下拷贝数据库文件。将MongoDB数据库文件拷贝到其他分区后,对MongoDB数据库所在原分区进行了格式化操作。格式化完成后将数据库文件拷回原分区,并重新启动MongoDB服务。发现服务无法启动并报错。
|
存储 NoSQL MongoDB
数据库数据恢复—MongoDB数据库迁移过程中丢失文件的数据恢复案例
某单位一台MongoDB数据库由于业务需求进行了数据迁移,数据库迁移后提示:“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
存储 JSON NoSQL
学习 MongoDB:打开强大的数据库技术大门
MongoDB 是一个基于分布式文件存储的文档数据库,由 C++ 编写,旨在为 Web 应用提供可扩展的高性能数据存储解决方案。它与 MySQL 类似,但使用文档结构而非表结构。核心概念包括:数据库(Database)、集合(Collection)、文档(Document)和字段(Field)。MongoDB 使用 BSON 格式存储数据,支持多种数据类型,如字符串、整数、数组等,并通过二进制编码实现高效存储和传输。BSON 文档结构类似 JSON,但更紧凑,适合网络传输。
764 15
|
存储 NoSQL MongoDB
微服务——MongoDB常用命令1——数据库操作
本节介绍了 MongoDB 中数据库的选择、创建与删除操作。使用 `use 数据库名称` 可选择或创建数据库,若数据库不存在则自动创建。通过 `show dbs` 或 `show databases` 查看所有可访问的数据库,用 `db` 命令查看当前数据库。注意,集合仅在插入数据后才会真正创建。数据库命名需遵循 UTF-8 格式,避免特殊字符,长度不超过 64 字节,且部分名称如 `admin`、`local` 和 `config` 为系统保留。删除数据库可通过 `db.dropDatabase()` 实现,主要用于移除已持久化的数据库。
1019 0

推荐镜像

更多