I am new to substrate and I am working on a project (in substrate) and as per the requirement I have to store the map key and value on a external database and to do so I chosen mongodb but I don't know how to implement it in runtime. or there are any other ways to use external code written in rust and use it inside the pallet...
The runtime should be considered a sandboxed environment and cannot easily talk to the outer client.
We do define a set of host functions which specifically describe the interface between the runtime and the client, but this is probably the wrong way to approach your problem.
Instead you probably want to run a second service which listens to your chain and forms the DB with the information you want.
Take a look at: https://github.com/paritytech/substrate-archive
And other such indexing solutions.