我正在尝试填写并生成测试集合(100个文档),这些是我的属性:
_id:“从1到100的串行整数”,组件:“string”,规格:[“最多3个字的数组”],描述:“string”,日期:“date”,类型:“2可能值type1或type2“,status:”Boolean“
我开始扭动脚本,请你检查一下并帮我完成它(类型,状态和日期),谢谢
for(var i=0; i < 100; i++)
{
var x = i;
var varName = "component" + i;
db.collectionName.insert(
{
_id: x, component: varName, specifications: [""], description: "",
date: "", type: "", status:""
});
for(var j=0; j < 3; j++)
{
db.collectionName.update({"component": varName },
{$push: {"specifications":"spec"+Math.floor(Math.random()*50)}},
false, true)
}
db.collectionName.update({"component": varName},
{$set: {"description": "test description"+Math.floor(Math.random()*10)}})
}
答案 0 :(得分:0)
您不需要进行更新
for(var i=0; i < 100; i++){
var varName = "component" + i;
db.collectionName.insert(
{
_id: i,
component: varName,
specifications: ["spec"+Math.floor(Math.random()*50),"spec"+Math.floor(Math.random()*50),"spec"+Math.floor(Math.random()*50)],
description: "test description"+Math.floor(Math.random()*10),
date: new Date(),
type: i % 2 ===0 ? "type1" : "type2",
status: i % 2 ===0
});
}