Mongo 查询在 mongo shell 中有效,但在 bash mongo --eval 中无效? [英] Mongo query works in mongo shell but not in a bash mongo --eval?

查看:65
本文介绍了Mongo 查询在 mongo shell 中有效,但在 bash mongo --eval 中无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个示例查询:

db.readings.find( {"_id.s" : ISODate("2012-11-01T00:05:00Z") } ).count()

Query 在 mongo shell 中工作.但是,在 bash 脚本中或直接在 Ubuntu shell 中

Query works in the mongo shell. However, in a bash script or directly in the Ubuntu shell

mongo Fivemin --eval "printjson(db.readings.find( {"_id.s" : ISODate("2012-11-01T00:05:00Z") } ).count())"

返回一个 SyntaxError: missing : after property id (shell eval):1

我似乎找不到查询的问题.我恢复到 { "_id" : {"s" : ...} },它仍然出现同样的问题.find().count() 但是有效.

I can't seem to find a problem with the query. I reverted to { "_id" : {"s" : ...} }, and it still gives the same problem. find().count() works however.

推荐答案

在 bash shell 中遇到了同样的问题.

Had the same issue in bash shell.

这会因为双引号而失败:

This will fail because of double quotes:

mongo fivemin --eval "printjson(db.readings.find( {"_id.s" : ISODate("2012-11-01T00:05:00Z") } ).count())"

但是在单引号中使用 eval 字符串是可行的:

But using the eval string in single quote it works:

mongo fivemin --eval 'printjson(db.readings.find( {"_id.s" : ISODate("2012-11-01T00:05:00Z") } ).count())'

如果你使用像 $regex 这样的 $ 符号,你需要对它进行转义:

And if you use a $ sign like $regex you need to escape it:

mongo fivemin --eval 'printjson(db.readings.find( {"_id.s" : {"\$regex":"2012-11-01T*"} } ).count())'

这篇关于Mongo 查询在 mongo shell 中有效,但在 bash mongo --eval 中无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆