如何将我的数据插入 mongoDB 循环中的集合?
How to insert my data into collection in a loop in mongoDB?
我想将数据填充到 mongo 数据库中的集合中。如果我硬编码插入查询,就好像:
db.devices.insertMany([
{"_id" : "FX200FTQ2109BZ00", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" },
{"_id" : "FX200FTQ2109BZ01", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" },
{"_id" : "FX200FTQ2109BZ02", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" },
...
{"_id" : "FX200FTQ2109BZ0A", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" },
...
{"_id" : "FX200FTQ2109BZ0Z", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" }
]);
我们可以看到,“_id”值的最后一个字符在(0~9,A~Z)范围内。那么有没有一种循环方式可以在 MongoDB Shell 中填充此类数据?
谢谢,
杰克
瞧:
mongos> var characters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
mongos>var length=characters.length;
mongos>for(var i = 0; i < length; i++ ) {
db.devices.insert({
"_id":"FX200FTQ2109BZ0"+characters.charAt(Math.floor(i)) ,
"cloud_blueprint_id": 105969 ,
"cloud_blueprint_name":"AWS-BP"
}
)}
WriteResult({ "nInserted" : 1 })
mongos> db.devices.count()
62
mongos> db.devices.findOne()
{
"_id" : "FX200FTQ2109BZ0A",
"cloud_blueprint_id" : 105969,
"cloud_blueprint_name" : "AWS-BP"
}
mongos>
解释:
- 定义变量“字符”,其中包含字符串 _id 中最后一个字符所需的字符
- 定义另一个包含字符串长度的变量
- 在for循环中添加insert()请求,并根据增量循环变量生成_id,根据字符串字符位置将其转换为字符。
当然,您可以在 insertMany
中修改和创建具有多个条目的更大的批次
这里是 insertMany 选项:
mongos> var characters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';var length=characters.length;var query=[],many="";for(var i = 0; i < length; i++ ) { query[i]='{"_id":"FX200FTQ2109BZ0'+characters.charAt(Math.floor(i))+'" ,"cloud_blueprint_id": 105969 ,"cloud_blueprint_name":"AWS-BP" }';};many="["+query.join(",")+"]";print(many);db.x.insertMany(eval(many));
我想将数据填充到 mongo 数据库中的集合中。如果我硬编码插入查询,就好像:
db.devices.insertMany([
{"_id" : "FX200FTQ2109BZ00", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" },
{"_id" : "FX200FTQ2109BZ01", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" },
{"_id" : "FX200FTQ2109BZ02", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" },
...
{"_id" : "FX200FTQ2109BZ0A", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" },
...
{"_id" : "FX200FTQ2109BZ0Z", "cloud_blueprint_id" : 105969, "cloud_blueprint_name" : "AWS-BP" }
]);
我们可以看到,“_id”值的最后一个字符在(0~9,A~Z)范围内。那么有没有一种循环方式可以在 MongoDB Shell 中填充此类数据?
谢谢,
杰克
瞧:
mongos> var characters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
mongos>var length=characters.length;
mongos>for(var i = 0; i < length; i++ ) {
db.devices.insert({
"_id":"FX200FTQ2109BZ0"+characters.charAt(Math.floor(i)) ,
"cloud_blueprint_id": 105969 ,
"cloud_blueprint_name":"AWS-BP"
}
)}
WriteResult({ "nInserted" : 1 })
mongos> db.devices.count()
62
mongos> db.devices.findOne()
{
"_id" : "FX200FTQ2109BZ0A",
"cloud_blueprint_id" : 105969,
"cloud_blueprint_name" : "AWS-BP"
}
mongos>
解释:
- 定义变量“字符”,其中包含字符串 _id 中最后一个字符所需的字符
- 定义另一个包含字符串长度的变量
- 在for循环中添加insert()请求,并根据增量循环变量生成_id,根据字符串字符位置将其转换为字符。
当然,您可以在 insertMany
中修改和创建具有多个条目的更大的批次这里是 insertMany 选项:
mongos> var characters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';var length=characters.length;var query=[],many="";for(var i = 0; i < length; i++ ) { query[i]='{"_id":"FX200FTQ2109BZ0'+characters.charAt(Math.floor(i))+'" ,"cloud_blueprint_id": 105969 ,"cloud_blueprint_name":"AWS-BP" }';};many="["+query.join(",")+"]";print(many);db.x.insertMany(eval(many));