我已經在某些集合上定義了一些簡單的遊標作爲find()。如何在cursor.ForEach中打印文檔?
cursor = db.students.find()
學生有以下模式:
"_id" : 19,
"name" : "Gisela Levin",
"scores" : [
{
"type" : "exam",
"score" : 44.51211101958831
},
{
"type" : "quiz",
"score" : 0.6578497966368002
},
{
"type" : "homework",
"score" : 93.36341655949683
},
{
"type" : "homework",
"score" : 49.43132782777443
}
]
我想要遍歷光標和打印文檔。我嘗試這樣的:
cursor.forEach(function(o){print(o)})
它只打印此:
[目標對象]
如果我修改一個循環來選擇一些簡單的屬性它的工作原理:
cursor.forEach(function(o){print(o._id)})
雖然如果我將其更改爲打印分數,它會打印此:
[object Object],[object Object],[object Object],[object Object]
是否有任何函數在mongodb shell中打印json文檔?
你也可以使用函數'pretty':'db.collection.find({},{ 「算賬」:1, 「_id」:0}) .pretty()'。 – Jaco
儘管這篇文章有點老了:你也可以用下面的表示法 'db.collection.find()。forEach(function(record){printjson(record.field)})' – Tobias
這隻適用於你想要的在文檔中打印出單個字段。 @Tobias – styvane