首页  :: 新随笔  :: 管理

MongoDB snippet之analyze-schema

Posted on 2022-03-10 15:46  高&玉  阅读(126)  评论(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) │   0   │     1     │     2      │
├─────────┼───────┼───────────┼────────────┤
│    0    │ '_id' │ '100.0 %' │ 'ObjectID' │
│    1    │ 'a  ' │ '50.0 %'  │  'Number'  │
│    2    │ 'a  ' │ '50.0 %'  │  'String'  │
└─────────┴───────┴───────────┴────────────┘
> schema(db.coll.find({ }));
┌─────────┬───────┬───────────┬────────────┐
│ (index) │   0   │     1     │     2      │
├─────────┼───────┼───────────┼────────────┤
│    0    │ '_id' │ '100.0 %' │ 'ObjectID' │
│    1    │ 'a  ' │ '100.0 %' │  'Number'  │
└─────────┴───────┴───────────┴────────────┘
> schema(db.test.aggregate([{ $group: { _id: null, count: { $sum: 1 } } }]));
┌─────────┬─────────┬───────────┬──────────┐
│ (index) │    0    │     1     │    2     │
├─────────┼─────────┼───────────┼──────────┤
│    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/