导出模式
在分析模式后,您可以导出模式。这对于共享模式和比较集合之间的模式非常有用。
如果您还没有这样做,请分析您的模式
选择您想要的集合,然后点击模式标签页。
点击 分析模式。
注意
当Compass分析您的模式时,它将从您的集合中随机采样文档子集。有关采样的更多信息,请参阅采样.
一旦分析完您的模式,请导出模式
在上面的菜单栏中,点击 集合。
从下拉菜单中,点击 将模式作为 JSON 分享。

您的模式将作为以下内容复制到剪贴板JSON 对象。
Schema 对象属性
Schema 对象具有count
和 fields
属性
count
是一个整数,表示用于生成模式的集合中抽取的文档数量。fields
是一个元数据对象数组,对应于采样过程中分析的文档中的每个字段。每个fields
数组元素包含以下字段
属性 | 数据类型 | 描述 |
---|---|---|
name | String | 对应字段的名称,例如 _id 。 |
path | String | 文档中对应字段的路径。 |
count | Integer | 包含对应字段的文档数量。 |
types | Array | 表示对应字段中出现的每个数据类型的元数据对象数组。 |
types[n].name | String | 此数据类型的名称。 |
types[n].bsonType | String | BSON 类型。 |
types[n].path | String | 文档中对应字段的路径。 |
types[n].count | Integer | 此数据类型在对应字段中出现的次数。 |
types[n].values | Array | 实际采样值数组,这些值出现在对应字段中并匹配此数据类型。 |
types[n].total_count | Integer | 如果对应字段是数组,则该数组中的元素数量。 |
types[n].probability | Number | 随机文档中对应字段值为此数据类型的概率。 |
types[n].unique | Integer | 此数据类型在对应字段中出现的唯一值数量。 |
types[n].has_duplicates | Boolean | true 如果此数据类型在对应字段中出现多次。否则 false 。 |
types[n].lengths | Array | 如果此数据类型是数组,则表示在对应字段中找到的数组长度的整数数组。对于其他数据类型不存在。 |
types[n].average_length | Number | 如果此数据类型是数组,则表示采样文档中对应字段中数组平均长度的数。 |
total_count | Integer | 从集合中抽取的文档数量。 |
type | String 或 Array | 表示对应字段可能类型的字符串或字符串数组。 |
has_duplicates | Boolean | true 如果对应字段中出现单个值多次。否则 false 。 |
probability | Number | 随机文档包含对应字段的概率。 |
示例模式
以下示例使用一组包含3个文档的集合,每个文档都有一个运动
字段以及该运动独特的相关信息
1 [ 2 { 3 "_id": { "$oid":"5e8359ba7782b98ba98c16fd" }, 4 "sport": "Baseball", 5 "equipment": [ "bat", "baseball", "glove", "helmet" ] 6 }, 7 { 8 "_id": { "$oid":"5e835a727782b98ba98c16fe" }, 9 "sport": "Football", 10 "variants": { 11 "us":"Soccer", 12 "eu":"Football" 13 } 14 }, 15 { 16 "_id": { "$oid":"5e835ade7782b98ba98c16ff" }, 17 "sport": "Cricket", 18 "origin": "England" 19 } 20 ]
您可以将上述示例导入到MongoDB Compass以实验模式输出。要将示例集合导入到MongoDB Compass:
复制上面的JSON文档。
在MongoDB Compass中,选择一个集合或创建一个新集合以导入复制的文档。显示“文档”标签页。
点击添加数据。
从下拉菜单中选择插入文档。
在对话框的JSON视图中粘贴复制的文档,然后点击插入。
上述示例输出以下模式
1 { 2 "fields": [ 3 { 4 "name": "_id", 5 "path": "_id", 6 "count": 3, 7 "types": [ 8 { 9 "name": "ObjectID", 10 "bsonType": "ObjectID", 11 "path": "_id", 12 "count": 3, 13 "values": [ 14 "5e8359ba7782b98ba98c16fd", 15 "5e835a727782b98ba98c16fe", 16 "5e835ade7782b98ba98c16ff" 17 ], 18 "total_count": 0, 19 "probability": 1, 20 "unique": 3, 21 "has_duplicates": false 22 } 23 ], 24 "total_count": 3, 25 "type": "ObjectID", 26 "has_duplicates": false, 27 "probability": 1 28 }, 29 { 30 "name": "equipment", 31 "path": "equipment", 32 "count": 1, 33 "types": [ 34 { 35 "name": "Undefined", 36 "type": "Undefined", 37 "path": "equipment", 38 "count": 2, 39 "total_count": 0, 40 "probability": 0.6666666666666666, 41 "unique": 1, 42 "has_duplicates": true 43 }, 44 { 45 "name": "Array", 46 "bsonType": "Array", 47 "path": "equipment", 48 "count": 1, 49 "types": [ 50 { 51 "name": "String", 52 "bsonType": "String", 53 "path": "equipment", 54 "count": 4, 55 "values": [ 56 "bat", 57 "baseball", 58 "glove", 59 "helmet" 60 ], 61 "total_count": 0, 62 "probability": 1, 63 "unique": 4, 64 "has_duplicates": false 65 } 66 ], 67 "lengths": [ 68 4 69 ], 70 "total_count": 4, 71 "probability": 0.3333333333333333, 72 "average_length": 4 73 } 74 ], 75 "total_count": 3, 76 "type": [ 77 "Undefined", 78 "Array" 79 ], 80 "has_duplicates": true, 81 "probability": 0.3333333333333333 82 }, 83 { 84 "name": "origin", 85 "path": "origin", 86 "count": 1, 87 "types": [ 88 { 89 "name": "Undefined", 90 "type": "Undefined", 91 "path": "origin", 92 "count": 2, 93 "total_count": 0, 94 "probability": 0.6666666666666666, 95 "unique": 1, 96 "has_duplicates": true 97 }, 98 { 99 "name": "String", 100 "bsonType": "String", 101 "path": "origin", 102 "count": 1, 103 "values": [ 104 "England" 105 ], 106 "total_count": 0, 107 "probability": 0.3333333333333333, 108 "unique": 1, 109 "has_duplicates": false 110 } 111 ], 112 "total_count": 3, 113 "type": [ 114 "Undefined", 115 "String" 116 ], 117 "has_duplicates": true, 118 "probability": 0.3333333333333333 119 }, 120 { 121 "name": "sport", 122 "path": "sport", 123 "count": 3, 124 "types": [ 125 { 126 "name": "String", 127 "bsonType": "String", 128 "path": "sport", 129 "count": 3, 130 "values": [ 131 "Baseball", 132 "Football", 133 "Cricket" 134 ], 135 "total_count": 0, 136 "probability": 1, 137 "unique": 3, 138 "has_duplicates": false 139 } 140 ], 141 "total_count": 3, 142 "type": "String", 143 "has_duplicates": false, 144 "probability": 1 145 }, 146 { 147 "name": "variants", 148 "path": "variants", 149 "count": 1, 150 "types": [ 151 { 152 "name": "Undefined", 153 "type": "Undefined", 154 "path": "variants", 155 "count": 2, 156 "total_count": 0, 157 "probability": 0.6666666666666666, 158 "unique": 1, 159 "has_duplicates": true 160 }, 161 { 162 "name": "Document", 163 "bsonType": "Document", 164 "path": "variants", 165 "count": 1, 166 "fields": [ 167 { 168 "name": "eu", 169 "path": "variants.eu", 170 "count": 1, 171 "types": [ 172 { 173 "name": "String", 174 "bsonType": "String", 175 "path": "variants.eu", 176 "count": 1, 177 "values": [ 178 "Football" 179 ], 180 "total_count": 0, 181 "probability": 1, 182 "unique": 1, 183 "has_duplicates": false 184 } 185 ], 186 "total_count": 1, 187 "type": "String", 188 "has_duplicates": false, 189 "probability": 1 190 }, 191 { 192 "name": "us", 193 "path": "variants.us", 194 "count": 1, 195 "types": [ 196 { 197 "name": "String", 198 "bsonType": "String", 199 "path": "variants.us", 200 "count": 1, 201 "values": [ 202 "Soccer" 203 ], 204 "total_count": 0, 205 "probability": 1, 206 "unique": 1, 207 "has_duplicates": false 208 } 209 ], 210 "total_count": 1, 211 "type": "String", 212 "has_duplicates": false, 213 "probability": 1 214 } 215 ], 216 "total_count": 0, 217 "probability": 0.3333333333333333 218 } 219 ], 220 "total_count": 3, 221 "type": [ 222 "Undefined", 223 "Document" 224 ], 225 "has_duplicates": true, 226 "probability": 0.3333333333333333 227 } 228 ], 229 "count": 3 230 }