0

I am trying to make a bash script that finds the maximum value in the following collection.

{ "_id" : ObjectId("5eaf83d1f803806404796611"), "livres" : "poeme", "ts" : ISODate("2020-05-04T02:54:09.668Z"), "val" : { "pages" : 30 } }
{ "_id" : ObjectId("5eaf83eff803806404796612"), "livres" : "poeme", "ts" : ISODate("2020-05-04T02:54:39.670Z"), "val" : { "pages" : 50 } }
{ "_id" : ObjectId("5eaf840df803806404796613"), "livres" : "histoire", "ts" : ISODate("2020-05-04T02:55:09.673Z"), "val" : { "pages" : 70 } }
{ "_id" : ObjectId("5eaf842bf803806404796614"), "livres" : "poeme", "ts" : ISODate("2020-05-04T02:55:39.675Z"), "val" : { "pages" : 0 } }
{ "_id" : ObjectId("5eaf8449f803806404796615"), "livres" : "histoire", "ts" : ISODate("2020-05-04T02:56:09.673Z"), "val" : { "pages" : 10 } }
{ "_id" : ObjectId("5eaf8467f803806404796616"), "livres" : "poeme", "ts" : ISODate("2020-05-04T02:56:39.675Z"), "val" : { "pages" : 50 } }
{ "_id" : ObjectId("5eaf8485f803806404796617"), "livres" : "poeme", "ts" : ISODate("2020-05-04T02:57:09.675Z"), "val" : { "pages" : 20 } }

The following query works fine in mongo cli:

db.livres_data.aggregate([{"$match": {"livres": "poeme"}},{"$group": {"_id": null,"MA": {"$max": "$val"}}}])

it returns:

  { "_id" : null, "MA" : { "pages" : 50 } }

When I ported this query to a bash script as below:

\#!/bin/bash

mongo myDB_name --eval "printjson(db.livres_data.aggregate([{'$match': {'livres': '$1'}},{'$group': {'_id': null,'MA': {'$max': 'val'}}}]))"

and execute it as: script_name.sh poeme

I got the following error:

MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/myDB_name?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d7fa1320-6f51-4836-829d-50b823f5f007") }
MongoDB server version: 4.2.0
2020-05-04T08:38:50.399+0000 E  QUERY    [js] uncaught exception: Error: command failed: {
    "ok" : 0,
    "errmsg" : **"Unrecognized pipeline stage name: ''",**
    "code" : 40324,
    "codeName" : "Location40324"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:583:17
assert.commandWorked@src/mongo/shell/assert.js:673:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell /collection.js:1012:12
@(shell eval):1:11
2020-05-04T08:38:50.399+0000 E  -        [main] exiting with code -4

Any help is appreciated. Thank you very much.

6
  • 1
    there's a typo in your script, you should have $val instead of val in your $group stage Commented May 4, 2020 at 9:12
  • you need to be careful with which $variables you want expanded in your script and which should be kept as literals Commented May 4, 2020 at 11:55
  • Thank you for your help. I changed as you advised: ` mongo myDB_name --eval "printjson(db.livres_data.aggregate([{'$match': {'livres': '$1'}},{'$group': {'_id': null,'MA': {'$max': '$val'}}}]))" ` but still the same error :( Commented May 4, 2020 at 13:18
  • @SamCarli : Why do you've {'$match': {'livres': '$1'}} in bash script ? Commented May 4, 2020 at 16:31
  • @whoami: in fact I am trying to execute something like this sql query: ** select max(val) from livres_data where livres="poeme". I suppose that **"where livres="poeme" in the sql query is ported to mongodb as {'$match': {'livres': '$1'}} Commented May 5, 2020 at 2:01

1 Answer 1

1

The issue was solved by changing the " by ' in the query as below:

mongo myDB_name --eval 'printjson(db.livres_data.aggregate([{"$match": {"livres": "'$1'"}},{"$group": {"_id": null,"MA": {"$max": "val"}}}]))'

Also '$1' was changed to "'$1'"

Sign up to request clarification or add additional context in comments.

Comments

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.