0

I'm trying to launch a search from a bash shell like that.

root@mongo01:~# mongo mydb --quiet --shell --eval "printjson(db.userContests.find({linkId: {$ne: null}}, {linkId: 1}).pretty())"
type "help" for help
2015-02-23T14:45:09.256+0100 SyntaxError: Unexpected token :

As you can see, I get an error.

However, launching the same from mongo shell works.

root@axn-mongo01:~# mongo mydb
MongoDB shell version: 2.6.7
connecting to: mydb
> db.userContests.find({linkId: {$ne: null}}, {linkId: 1}).pretty()
{
    "linkId" : "_guid_F5BDEJFk2hvxyamKIVePEw==",
    "_id" : ObjectId("546dc5e47d479b1d0d8b45aa")
}
{
    "linkId" : "_guid_F5BDEJFk2hvxyamKIVePEw==",
    "_id" : ObjectId("546debdf7d479b686f8b458d")
}
{
    "linkId" : "_guid_EcWY0_RmDs8wA2T9LuVAGtiDfUHf8i-jLxTrhDXjzo8=",
    "_id" : ObjectId("546df6237d479b62698b45be")

Someone knows what's happening? Thanks.

1 Answer 1

1

Dollar sign $ is getting interpolated by your shell, you can see what's going on using echo:

echo mongo mydb --quiet --shell --eval "printjson(db.userContests.find({linkId: {$ne: null}}, {linkId: 1}).pretty())"
mongo mydb --quiet --shell --eval printjson(db.userContests.find({linkId: {: null}}, {linkId: 1}).pretty())

As you can see your shell is parsing $ne, which is unset therefore generating invalid syntax.

To fix it use single quotes instead:

mongo mydb --quiet --shell --eval 'printjson(db.userContests.find({linkId: {$ne: null}}, {linkId: 1}).pretty())'
Sign up to request clarification or add additional context in comments.

4 Comments

You are right in part. but isn't enough. With single quotes ( or escaping dollar sign ) doesn't work.
Not exactly. It's difficult to explain. You can get the same with,,, mongo admin --quiet --shell --eval 'printjson(db.admin.find())'
try this: mongo mydb --quiet <<< 'db.userContests.find({linkId: {$ne: null}}, {linkId: 1})'
I think the problem is --shell and --eval can't be sure. I am glad it helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.