我正在尝试在 mongodb 集合中“查找所有”资源。
我可以得到计数:
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
进入mongo shell并
db.getCollection('collection_123').find()
并查看正确的 json 输出。
最佳答案
我找到的唯一方法是
mongo ip:port/database1 -u correctusername -p correctpassword
--authenticationDatabase admin --eval "db.collection.find().forEach(printjson)"
但输出与 shell 中的输出不同,因为进一步的命令将不起作用,例如排序或限制。
更新
更好的解决办法是
mongo ip:port/database1 -u correctusername -p correctpassword
--authenticationDatabase admin --eval "printjson(db.collection.find().toArray())"
关于mongodb - mongo find() 使用 eval 不产生输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745235/