mongodb 如何在 mongo shell 中查看文档字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5900792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to view document fields in mongo shell?
提问by Rob
Is there a way to figure out the fields/keys in a document while in mongo's shell? As an example, let's say we have a document like (pseudocode):
有没有办法在 mongo 的 shell 中找出文档中的字段/键?举个例子,假设我们有一个像(伪代码)这样的文档:
{
"message": "Hello, world",
"from": "hal",
"field": 123
}
I'd like to run a command in the shell that returns the list of fields/keys in that document. For instance, something like this:
我想在 shell 中运行一个命令,该命令返回该文档中的字段/键列表。例如,这样的事情:
> var message = db.messages.findOne()
> message.keys()
... prints out "message, from, field"
Thanks!
谢谢!
回答by Will Fitzgerald
Even easier:
更简单:
Object.keys(db.messages.findOne())
回答by Shane Andrade
A for ... in
loop should do the trick:
一for ... in
环应该做的伎俩:
> var message = db.messages.findOne();
> for (var key in message) {
... print(key);
... }
回答by Manohar Reddy Poreddy
Other answers are correct.
其他答案都是正确的。
However, as I am completely new, I didn't understand where & how the above commands need to be executed.
但是,由于我是全新的,我不明白需要在何处以及如何执行上述命令。
Below helped, from my github.
On Windows: Run this code in a command prompt (cmd).
On Mac or Linux: Run this code in a terminal window.
下面有帮助,来自我的 github。
在 Windows 上:在命令提示符 (cmd) 中运行此代码。
在 Mac 或 Linux 上:在终端窗口中运行此代码。
// ------------
// start mongo client
mongo
// ------------
// list all databases
show dbs
// NOTE: assume one of the databases is myNewDatabase
// use the 'myNewDatabase' database
use myNewDatabase
// ------------
// show all collections of 'myNewDatabase' database
show collections
// NOTE: assume one of the collections is 'myCollection'
// show all documents of 'myCollection' collection
db.myCollection.find()
// ------------
// field keys
Object.keys(db.myCollection.findOne());
// values
db.myCollection.find().forEach(function(doc) {
for (field in doc) {
print(doc[field]);
}
});
// ------------
回答by zanther
To get a list of all fields used in a collection in MongoDB, this is the way I found most straightforward (your mileage may vary :) ):
要获取 MongoDB 中集合中使用的所有字段的列表,这是我发现的最直接的方式(您的里程可能会有所不同 :)):
Create a .js file with the content:
创建一个包含以下内容的 .js 文件:
use yourdbname
mr = db.runCommand({
"mapreduce" : "collectionName",
"map" : function() {
for (var key in this) { emit(key, null); }
},
"reduce" : function(key, stuff) { return null; },
"out": "collectionName" + "_keys"
})
db[mr.result].distinct("_id")
I found out how to do this here (GeoffTech blog)
我在这里找到了如何做到这一点(GeoffTech 博客)
I ran it from the shell to print the output in the console
我从 shell 运行它以在控制台中打印输出
mongo < nameOfYourFile.js
or dump the output in a text file:
或将输出转储到文本文件中:
mongo < nameOfYourFile.js > outputDir\nameOfYourOutputFile.txt
I'm totally new to MongoDb so I hope it does indeed get all fields regardless of use throughout the documents!
我对 MongoDb 完全陌生,所以我希望它确实可以获取所有字段,而不管整个文档中的使用情况如何!
(I'm using MongoDb on windows 10, so my console may differ from yours)
(我在 Windows 10 上使用 MongoDb,所以我的控制台可能与你的不同)
回答by u11145812
var task = db.task.find().next()
for (let key in task){print(key)}