如何通过MongoDB查询获取garages、system集合的全部列与信息
Hey there! Let's walk through how to pull data and details from the garages and system collections you're targeting. Since you already confirmed these collections exist (from your earlier count of 3 total), here are practical, straightforward steps to get the info you need:
1. 查看集合的基础信息(字段结构、统计数据)
快速了解字段/列结构
If you want to see what columns (fields) exist in the collection, start by fetching a single document—it gives you a clear snapshot of the schema:
// 查看garages集合的字段结构 db.garages.findOne().pretty() // 查看system集合的字段结构 db.system.findOne().pretty()
If you need a complete list of all unique fields across every document (useful if some docs have optional fields), use this aggregation pipeline:
// 提取garages集合的所有唯一字段 db.garages.aggregate([ {$project: {fields: {$objectToArray: "$$ROOT"}}}, {$unwind: "$fields"}, {$group: {_id: null, allFields: {$addToSet: "$fields.k"}}}, {$project: {_id: 0}} ]) // 替换集合名为"system"即可获取对应集合的所有字段
获取集合统计元数据
To get details like document count, collection size, index configuration, run the stats() method:
// 获取garages集合的统计信息 db.garages.stats() // 获取system集合的统计信息 db.system.stats()
2. 获取集合的全部文档数据
查看格式化后的全量数据
To fetch every document in the collection (formatted for readability), use:
// 查看garages集合的所有文档 db.garages.find().pretty() // 查看system集合的所有文档 db.system.find().pretty()
If the collection is large, add .limit(n) to return only the first n documents (e.g., db.garages.find().limit(10).pretty()) to avoid overwhelming your shell.
导出数据为数组(适合后续处理)
If you want to retrieve all documents as a JSON array (for scripting or local processing), use:
// 将garages集合所有文档转为数组 db.garages.find().toArray() // 将system集合所有文档转为数组 db.system.find().toArray()
Note: This works best for small-to-medium collections—large datasets may cause memory issues in the MongoDB shell.
关于"system"集合的小提醒
Keep in mind that MongoDB has built-in system collections (like system.users, system.indexes) that store internal database data. If your system collection is user-defined, all the above methods work as-is. If it's a built-in system collection, you may need elevated permissions to access it.
内容的提问来源于stack exchange,提问作者John

