如何将我的数据插入到 mongoDB 循环中的集合中?

发布于 2025-01-13 01:55:11 字数 727 浏览 0 评论 0原文

我想将数据填充到 mongo db 中的集合中。如果我对插入查询进行硬编码,那就好像:

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 中?

谢谢,

杰克

I want to populate data into a collection in mongo db. If I to hard-code the insert query, it would be as if:

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" }
]);

As we can see, the last char of "_id" value is in the range of (0~9,A~Z). So is there a loop way I can have such data populated inside MongoDB Shell ?

Thanks,

Jack

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2025-01-20 01:55:11

瞧:

 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> 

解释:

  1. 定义变量“字符”,其中包含字符串 _id 中最后一个字符所需的字符
  2. 定义另一个包含字符串长度的变量
  3. 在 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));

Voila:

 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> 

Explained:

  1. Define variable "characters" that contain the characters you need for the last character in your string _id
  2. Define another variable that will contain the lenght of the characters string
  3. Add the insert() request inside the for loop and generate the _id based on incremental loop variable converting it into character based on string character location.

Afcourse you can modify and create bigger batches with multiple entries inside insertMany

here is the insertMany option:

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文