I got a problem with shellscript here.
I need to read a json file and pass this as a string using another command. Basically what I'm doing is reading a json file that has a schema validator and passing this schema validator do Mongo while creating a new collection.
If I pass the command like this from my .sh file:
mongo $DATABASE -u $MY_USER -p $PASS --eval "db.createCollection('$MY_COLLECTION', { validator: { \$jsonSchema: { \"bsonType\":\"object\", \"additionalProperties\":false, \"required\":[ \"mongo-modified\", \"mongo-revision\" ], \"properties\":{ \"_id\":{}, \"Description\":{\"bsonType\":\"string\"},\"mongo-modified\":{\"bsonType\":\"date\"},\"mongo-revision\":{\"bsonType\":\"string\"},\"Summary\":{\"bsonType\":\"string\"}}} } });"
Then it works fine. As you can see I had to escape all the double quotes to make it work. The problem is that instead of having it hardcoded there I'll be reading from a json file like this:
VALIDATOR=`cat /tmp/schema-validator.json`
And then I'll call mongo like this:
mongo $DATABASE -u $MY_USER -p $PASS --eval "db.createCollection('$MY_COLLECTION', { validator: { $VALIDATOR } });"
It's not working this way and I believe the reason is the quotes I have inside my $VALIDATOR.
The Json file looks like this:
{
"bsonType": "object",
"additionalProperties": false,
"required": [
"mongo-modified",
"mongo-revision"
],
"properties": {
"_id": {
},
"Description": {
"bsonType": "string"
},
"mongo-modified": {
"bsonType": "date"
},
"mongo-revision": {
"bsonType": "string"
},
"Summary": {
"bsonType": "string"
}
}
Another possible reason would be having multiple lines. Either way I'm kind of stuck with that.
If someone could give me a direction on that it would be great.
Thanks!