为 MongoDB 数据库播种的文档的自动随机测试数据生成
Automatic Random Test Data Generation of Documents for Seeding a MongoDB Database
我正在使用 JSON 生成器 https://next.json-generator.com 为我的 MongoDB 数据库播种。我发现无法从预定义组中生成一组随机颜色(从一到六)。
我已成功生成除颜色之外的所有其他 属性 值。我需要 availableColors 键从预定义的六种颜色中生成随机可变数量的颜色:"blue"、"brown"、"green"、"white"、"yellow", "gray".
这里是 URL 在线生成器,可以实时编辑它:https://next.json-generator.com/E1g60a1pL
代码如下:
[
{
'repeat(5, 10)': {
id: '{{objectId()}}',
name: '{{firstName()}}',
price: '{{floating(5, 4000, 2, "[=11=],0.00")}}',
availableColors: (colors) => {
var condition = '{{integer(1, 6)}}';
var color = [];
for (j = 0; j < condition+1; j++)
{color [j] = '{{random("blue", "brown", "green", "white", "yellow", "gray")}}';}
let unique_array = [];
for(let i = 0;i < color.length; i++) {
if(unique_array.indexOf(color[i]) == -1){
unique_array.push(color[i]);
}
}
return unique_array;
},
weight: '{{floating(1, 4000, 2, "0.00")}}',
inStock: '{{integer(0, 2000)}}'
}
}
]
这是我得到的结果:
[
{
"id": "5ce82b1302c9777aac5fd681",
"name": "Blake",
"price": "9.53",
"availableColors": [],
"weight": "3753.22",
"inStock": 449
},
{
"id": "5ce82b137ab9fe24eda22714",
"name": "Felicia",
"price": ",190.01",
"availableColors": [],
"weight": "3797.51",
"inStock": 1288
},
{
"id": "5ce82b135414eb7550aee368",
"name": "Bettye",
"price": "7.41",
"availableColors": [],
"weight": "2182.52",
"inStock": 1288
},
{
"id": "5ce82b13f751e63a8506fbf2",
"name": "Mullen",
"price": ",306.81",
"availableColors": [],
"weight": "694.51",
"inStock": 821
},
{
"id": "5ce82b130544c7c08086a6bc",
"name": "Angelita",
"price": "4.90",
"availableColors": [],
"weight": "3.44",
"inStock": 226
},
{
"id": "5ce82b130d9e2fc4c2a21e22",
"name": "Mcknight",
"price": ",582.76",
"availableColors": [],
"weight": "1183.82",
"inStock": 1917
},
{
"id": "5ce82b13fb509ee9c384a096",
"name": "Nannie",
"price": ",479.29",
"availableColors": [],
"weight": "754.85",
"inStock": 716
},
{
"id": "5ce82b13881cb29ec7a1772b",
"name": "Sutton",
"price": ",726.83",
"availableColors": [],
"weight": "1382.76",
"inStock": 1911
},
{
"id": "5ce82b1386ad13bffcf0923b",
"name": "Maria",
"price": ",679.58",
"availableColors": [],
"weight": "1106.28",
"inStock": 5
},
{
"id": "5ce82b13fccd87dbe6451971",
"name": "Noble",
"price": "9.25",
"availableColors": [],
"weight": "1657.83",
"inStock": 235
}
]
我希望任何文档的 "availableColors" 都是一到六种预定义颜色的数组。有什么想法吗?
您需要使用 Math.random 制作一个纯 javascript 循环
我更新了 link:
https://next.json-generator.com/EJXO4xfaU
在深入研究了 JSON Generator 主页中提供的示例后,我发现了如何使用其关键字来获得相同的结果。顺便说一句,按照我的文档架构,我添加了随机提供或不提供所有不需要的属性的值的方法。
这里是给在线编辑的URL:https://next.json-generator.com/4k8Wd87pU
代码如下:
[{
'repeat(5, 10)': {
id: '{{objectId()}}',
name: '{{firstName()}}',
price(tags) {
const nRequired = tags.integer(0, 5);
if (nRequired) {
return tags.floating(5, 4000, 2, "[=10=],0.00");
}
},
availableColors(tags) {
const nRequired = tags.integer(0, 3);
if (!nRequired) return;
const Colors = ['blue', 'brown', 'green', 'white', 'yellow', 'gray'];
const nColors = tags.integer(0, Colors.length - 1); // generate a random integer from 0 to 5 will be used to select the total number of colors to add as values
const aColors = [];
for (j = 0; j <= nColors && !aColors[j]; j++) {
let sColor = tags.integer(0, Colors.length - 1); // generate a random integer from 0 to 5 that will be used as index to select a random color from the Colors array
if (aColors.indexOf(Colors[sColor]) == -1) {
aColors.push(Colors[sColor]);
}
}
return aColors;
},
weight(tags) {
const nRequired = tags.integer(0, 5);
if (nRequired) {
return tags.floating(1, 900, 2, "0.00");
}
},
inStock(tags) {
const nRequired = tags.integer(0, 2);
if (nRequired) {
return tags.integer(0, 2000);
}
}
}
}]
我正在使用 JSON 生成器 https://next.json-generator.com 为我的 MongoDB 数据库播种。我发现无法从预定义组中生成一组随机颜色(从一到六)。
我已成功生成除颜色之外的所有其他 属性 值。我需要 availableColors 键从预定义的六种颜色中生成随机可变数量的颜色:"blue"、"brown"、"green"、"white"、"yellow", "gray".
这里是 URL 在线生成器,可以实时编辑它:https://next.json-generator.com/E1g60a1pL
代码如下:
[
{
'repeat(5, 10)': {
id: '{{objectId()}}',
name: '{{firstName()}}',
price: '{{floating(5, 4000, 2, "[=11=],0.00")}}',
availableColors: (colors) => {
var condition = '{{integer(1, 6)}}';
var color = [];
for (j = 0; j < condition+1; j++)
{color [j] = '{{random("blue", "brown", "green", "white", "yellow", "gray")}}';}
let unique_array = [];
for(let i = 0;i < color.length; i++) {
if(unique_array.indexOf(color[i]) == -1){
unique_array.push(color[i]);
}
}
return unique_array;
},
weight: '{{floating(1, 4000, 2, "0.00")}}',
inStock: '{{integer(0, 2000)}}'
}
}
]
这是我得到的结果:
[
{
"id": "5ce82b1302c9777aac5fd681",
"name": "Blake",
"price": "9.53",
"availableColors": [],
"weight": "3753.22",
"inStock": 449
},
{
"id": "5ce82b137ab9fe24eda22714",
"name": "Felicia",
"price": ",190.01",
"availableColors": [],
"weight": "3797.51",
"inStock": 1288
},
{
"id": "5ce82b135414eb7550aee368",
"name": "Bettye",
"price": "7.41",
"availableColors": [],
"weight": "2182.52",
"inStock": 1288
},
{
"id": "5ce82b13f751e63a8506fbf2",
"name": "Mullen",
"price": ",306.81",
"availableColors": [],
"weight": "694.51",
"inStock": 821
},
{
"id": "5ce82b130544c7c08086a6bc",
"name": "Angelita",
"price": "4.90",
"availableColors": [],
"weight": "3.44",
"inStock": 226
},
{
"id": "5ce82b130d9e2fc4c2a21e22",
"name": "Mcknight",
"price": ",582.76",
"availableColors": [],
"weight": "1183.82",
"inStock": 1917
},
{
"id": "5ce82b13fb509ee9c384a096",
"name": "Nannie",
"price": ",479.29",
"availableColors": [],
"weight": "754.85",
"inStock": 716
},
{
"id": "5ce82b13881cb29ec7a1772b",
"name": "Sutton",
"price": ",726.83",
"availableColors": [],
"weight": "1382.76",
"inStock": 1911
},
{
"id": "5ce82b1386ad13bffcf0923b",
"name": "Maria",
"price": ",679.58",
"availableColors": [],
"weight": "1106.28",
"inStock": 5
},
{
"id": "5ce82b13fccd87dbe6451971",
"name": "Noble",
"price": "9.25",
"availableColors": [],
"weight": "1657.83",
"inStock": 235
}
]
我希望任何文档的 "availableColors" 都是一到六种预定义颜色的数组。有什么想法吗?
您需要使用 Math.random 制作一个纯 javascript 循环 我更新了 link: https://next.json-generator.com/EJXO4xfaU
在深入研究了 JSON Generator 主页中提供的示例后,我发现了如何使用其关键字来获得相同的结果。顺便说一句,按照我的文档架构,我添加了随机提供或不提供所有不需要的属性的值的方法。
这里是给在线编辑的URL:https://next.json-generator.com/4k8Wd87pU
代码如下:
[{
'repeat(5, 10)': {
id: '{{objectId()}}',
name: '{{firstName()}}',
price(tags) {
const nRequired = tags.integer(0, 5);
if (nRequired) {
return tags.floating(5, 4000, 2, "[=10=],0.00");
}
},
availableColors(tags) {
const nRequired = tags.integer(0, 3);
if (!nRequired) return;
const Colors = ['blue', 'brown', 'green', 'white', 'yellow', 'gray'];
const nColors = tags.integer(0, Colors.length - 1); // generate a random integer from 0 to 5 will be used to select the total number of colors to add as values
const aColors = [];
for (j = 0; j <= nColors && !aColors[j]; j++) {
let sColor = tags.integer(0, Colors.length - 1); // generate a random integer from 0 to 5 that will be used as index to select a random color from the Colors array
if (aColors.indexOf(Colors[sColor]) == -1) {
aColors.push(Colors[sColor]);
}
}
return aColors;
},
weight(tags) {
const nRequired = tags.integer(0, 5);
if (nRequired) {
return tags.floating(1, 900, 2, "0.00");
}
},
inStock(tags) {
const nRequired = tags.integer(0, 2);
if (nRequired) {
return tags.integer(0, 2000);
}
}
}
}]