If you do not want to serve the script from a file (I try not to distribute external files as much as possible) or not to use the --eval parameter, which can be hard to read if you have many entries, you can use bash heredoc
You can enter the terminal:
-bash-4.1$ mongo mongodb://myServerAddress/myDbName <<EOF > db.myCollectionName.insert({ > name: "doc name", > title: "doc title" > }) > EOF
Result:
MongoDB shell version v3.4.1 connecting to: mongodb://myServerAddress/myDbName MongoDB server version: 3.0.7 WARNING: shell and server versions do not match WriteResult({ "nInserted" : 1 }) bye -bash-4.1$
If you want to save it in a script, just remove the > , which really requests a multiline command.
To use a script, it must be as follows:
#!/usr/bin/env bash mongo mongodb://myServerAddress/myDbName <<EOF db.myCollectionName.insert({ name: "doc name", title: "doc title" }) EOF
source share