【问题标题】:How to get schema mongo如何获取模式 mongo
【发布时间】:2022-06-13 23:40:56
【问题描述】:

我试图从 mongo 库中获取架构,但我只能从单个记录中获取架构。如何获取所有记录的架构?

var schemaObj = db.getCollection("dado").find({})

function printSchema(obj, indent) {
  for (var key in obj) {
    if (typeof obj[key] != "function") {     //we don't want to print functions
        print(indent, key, typeof obj[key]);
        if (typeof obj[key] == "object") {             //if current property is of object type, print its sub properties too
            printSchema(obj[key], indent + "\t");
        }
    }
  }
};

printSchema(schemaObj, "");

结果与此类似,但这仅适用于一条记录,我想对所有记录都这样做。

_id object
   str string
   isObjectId boolean
name string
email string
adress object
   street string
   number number
   district string
   postal_code string
   complement string
   reference_point string
_id object
   str string
   isObjectId boolean
name string
email string
adress object
   street string
   number number
   district string
   postal_code string
   complement string
document string

我想要的结果是这样的,遍历所有现有字段

_id ObjectId
document String
email String
address Object
  district String
  postal_number String
  complement String
  number integer
  reference_point String
  street String
name String

【问题讨论】:

  • 你试过了吗?

标签: mongodb mongodb-query


【解决方案1】:

如果你有compass,你可以使用它从analysed sample获取schema

Reference

当 Compass 分析您的架构时,它会从您的集合中随机抽取一个文档子集。要了解有关抽样的更多信息,请参阅抽样。

【讨论】:

  • 我测试了它,但列带有数据,我想要一种无法可视化数据的方式
  • 你能详细说明尝试的步骤吗?
  • 我更新了问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 2017-06-24
  • 1970-01-01
  • 2012-06-10
相关资源
最近更新 更多