Youssef Boudaya
Youssef Boudaya

Reputation: 997

Run mongo query in shell

I have a little problem when trying this in shell script ( works fine directly on mongo database shell):

mongo jenkinsdb<< EOF


ids_to_exclude = db.history.find({projectId: $object_id._id.str}, 
{_id:1}).sort({"_id":-1}).limit(2).toArray()
var array = new Array()
ids_to_exclude.forEach(function(myDoc){array.push(myDoc._id);})
array
db.getCollection('history').updateMany({ _id:{$nin:array}},{$set:{backupPath:undefined}})

exit
EOF

I get “uncaught exception: SyntaxError: expected property name, got ‘:’ : @(shell):1:46” i think the problem is in “db.getCollection …” command.

Upvotes: 0

Views: 52

Answers (1)

D. SM
D. SM

Reputation: 14480

You need to escape all of the dollar signs in the heredoc or use the single-quoted heredoc syntax:

cat <<'EOF'


ids_to_exclude = db.history.find({projectId: $object_id._id.str}, 
{_id:1}).sort({"_id":-1}).limit(2).toArray()
var array = new Array()
ids_to_exclude.forEach(function(myDoc){array.push(myDoc._id);})
array
db.getCollection('history').updateMany({ _id:{$nin:array}},{$set:{backupPath:undefined}})

exit
EOF

Upvotes: 1

Related Questions