随笔 - 136, 文章 - 0, 评论 - 14, 阅读 - 13万
  首页  :: 新随笔  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

MongoDB snippet之analyze-schema

Posted on   高&玉  阅读(133)  评论(0编辑  收藏  举报

介绍

snippet功能是MongoDB实验性的功能,MongoDB对snippet不提供技术支持。snippet功能可能会随时更改或者删除。

只能使用mongosh对snippet进行管理及执行。

 

snippet有三个重要组件:

  • snippet是可以与mongosh一起使用的代码。
  • Packages是绑定了元数据的脚本,以便更容易的管理。
  • Registries是可以共享的包的集合。

使用

搜索存储库查看可用的snippet

[root]# mongosh admin
Enterprise replica02 [direct: primary] admin> snippet search
┌─────────┬───────────────────┬─────────┬────────────────────┐
│ (index) │       name        │ version │                          description                           │
├─────────┼───────────────────┼─────────┼────────────────────┤
│    0'mongocompat''1.0.7''mongo compatibility script for mongosh'            │
│    1'spawn-mongod''1.0.1''Spin up a local mongod process'                │
│    2'analyze-schema''1.0.5''schema(db.coll)'                        │
│    3'mock-collection''1.0.2''mockCollection([{ a: 1 }, { a: 2 }]).find({ a: { $gt: 2 } })' │
│    4'resumetoken''1.0.2''Resume token decoder script'                  │
└─────────┴───────────────────┴─────────┴────────────────────┘

 

获取存储库信息

Enterprise replica02 [direct: primary] db01> snippet info
Snippet repository URL:      https://compass.mongodb.com/mongosh/snippets-index.bson.br
  -->  Homepage:             https://github.com/mongodb-labs/mongosh-snippets

 

安装analyze-schema

Enterprise replica02 [direct: primary] admin> snippet install analyze-schema
Running install...
Installed new snippets analyze-schema. Do you want to load them now? [Y/n]: y
Finished installing snippets: analyze-schema

 

查看本地已安装的analyze-schema

Enterprise replica02 [direct: primary] admin> snippet ls
snippets@ /root/.mongodb/mongosh/snippets
├── mongosh:analyze-schema@1.0.5
└── npm@8.5.3

 

查看analyze-schema帮助手册

Enterprise replica02 [direct: primary] admin> snippet help analyze-schema
# analyze-schema

Analyze the schema of a collection or a cursor.

```js
> schema(db.coll);
┌─────────┬───────┬───────────┬────────────┐
│ (index) │   012      │
├─────────┼───────┼───────────┼────────────┤
│    0'_id''100.0 %''ObjectID' │
│    1'a  ''50.0 %''Number'  │
│    2'a  ''50.0 %''String'  │
└─────────┴───────┴───────────┴────────────┘
> schema(db.coll.find({ }));
┌─────────┬───────┬───────────┬────────────┐
│ (index) │   012      │
├─────────┼───────┼───────────┼────────────┤
│    0'_id''100.0 %''ObjectID' │
│    1'a  ''100.0 %''Number'  │
└─────────┴───────┴───────────┴────────────┘
> schema(db.test.aggregate([{ $group: { _id: null, count: { $sum: 1 } } }]));
┌─────────┬─────────┬───────────┬──────────┐
│ (index) │    012     │
├─────────┼─────────┼───────────┼──────────┤
│    0'_id  ''100.0 %''Null'  │
│    1'count''100.0 %''Number' │
└─────────┴─────────┴───────────┴──────────┘
> schema(db.coll, { verbose: true });
{
  fields: [
    {
      name: '_id',
      // [ ... ]
    },
    {
      path: 'a',
      count: 2,
      types: [
        {
          name: 'Number',
          path: 'a',
          probability: 0.5,
          unique: 1,
          // [ ... ]
        },
        {
          name: 'String',
          bsonType: 'String',
          // [ ... ]
        }
      ],
      total_count: 2,
      type: [ 'Number', 'String' ],
      probability: 1
    }
  ],
  count: 2
}

 

创建测试集合

> use db01
> db.reservations.insertMany( [
   {"_id": 1001, "roomNum": 1, "reserved": true },
   {"_id": 1002, "roomNum": 2, "reserved": true },
   {"_id": 1003, "roomNum": 3, "reserved": "false" },
   {"_id": 1004, "roomNum": 4, "reserved": true },
] )

 

分析集合

注释:

  • _id 100%是number。
  • reserved 75%是Boolean,25%是String。
  • roomNum 100%是Number。

 

卸载analyze-schema

Enterprise replica02 [direct: primary] db01> snippet uninstall analyze-schema
Running uninstall...
Done!

 

参考:https://docs.mongodb.com/mongodb-shell/snippets/

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示