2016-08-03 83 views
1

我試圖做一個'找到所有'的資源在一個mongodb集合。mongo find()產生沒有輸出與eval

我可以得到數確定:

mongo ip:port/database1 -u correctusername -p correctpassword 
--authenticationDatabase admin --eval "db.getCollection('collection_123').count()" 

但是當我嘗試調整它,

mongo ip:port/database1 -u correctusername -p correctpassword 
--authenticationDatabase admin --eval "db.getCollection('collection_123').find()" 

它產生

MongoDB shell version: 2.4.0 
connecting to: ip:port/database1 
DBQuery: database1.collection_123 -> { } 

我也試過find({})

該重新似乎不是身份驗證錯誤。第一個查詢工作併產生一個計數。我還可以

mongo ip:port/database1 -u correctusername -p correctpassword 
--authenticationDatabase admin 

進入蒙戈外殼和

db.getCollection('collection_123').find() 

而且看到正確的JSON輸出。

+0

關懷給集合名稱?這真的是table123? – Oct

+0

@Oct更新到集合,它只是一個測試集合 – Michael

回答

1

我發現的唯一辦法就是

mongo ip:port/database1 -u correctusername -p correctpassword 
--authenticationDatabase admin --eval "db.collection.find().forEach(printjson)" 

但輸出是不一樣的外殼,進一步命令將無法正常工作,如排序或限制。

更新

更好的解決方案是

mongo ip:port/database1 -u correctusername -p correctpassword 
--authenticationDatabase admin --eval "printjson(db.collection.find().toArray())"