mongodb - mongo find() 使用 eval 不产生输出

标签 mongodb

我正在尝试在 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/

相关文章:

Mongodb 更新深度嵌套的子文档

node.js - 不能在模块外使用 import 语句

java - MongoDB 和 Morphia - 传统 id(长)而不是 ObjectId

mongodb - 如何更改 mongoshell 提示符?

javascript - Jade block 扩展数据库中的每个项目?

java - Spring 和 MongoDB : SAXParseException while reading Bean Definitions

node.js - 查找对象是否在预保存钩子(Hook) Mongoose 中更改

node.js - MongoDB:在集合上设置 TTL 索引时出错: session

mongodb - 删除 mongodb $lookup 聚合的结果

node.js - 有没有办法用 mongoskin 列出集合?