Question: Hi I am beginner in node js mongodb. I want to insert data in mongodb. First I check user exist in users collection then data insert and after complete process then callback. Please give suggestion

```
   exports.contactSync = function contactSync(data, userId, callback) {

    var Promise = require('promise');
    MongoClient.connect(Mongo_url, function (err, db) {
    assert.equal(null, err);
    db.collection('contacts').deleteMany({user_id: userId});
       var bulk = db.collection('contacts').initializeOrderedBulkOp(),
            counter = 0;

    data.forEach(function (doc) {
        let promises = [];
        db.collection('users').findOne({email: doc.trim()}, {user_id: 1}).then(function (userData) { 
            promises.push(new Promise(resolve => {
                bulk.insert({user_id: userId, contact_id: userData.user_id});
            }));
        });

        counter++;

        if (data.length == counter) { 
             return Promise.all(promises);
            bulk.execute(function (err, r) {
                // do something with the result
                     console.log("succes");
            });
        }
    }); 

    if (counter > 0) {
        bulk.execute(function (err, result) {
            console.log("succes"); 
            callback({'error': 0, 'message': 'Successfull synchronization', 'data': null, 'status': 200});
            // do something with the result here
        });
    }
    }

```

Answer: 
In fact, this is not about headers, as they are just pieces of code, that are copy-pasted into files using the preprocessor. The root of your question is why we need a full definition of an external value, in order to compile a unit. For example, in C, you can freely use any external function, given that you provided the prototype, and in older versions of C you don't even need the prototype. 

The reason why it works is because a compilation unit, produced by a C compiler, will contain unresolved symbols (e.g., functions or data values defined somewhere else), and it is the job of a linker to connect those symbols to their respective implementations. 

OCaml, underneath the hood, uses a C-like format for compilation units and is actually relying on a system C linker to produce an executable. Thus there are no practical or theoretical limitations so far. 

So what is the problem? As long, as you can provide a type of an external symbol, you can easily use it. And here is the main problem -- if there is a mutual dependency (on a type level) between two compilation units (i.e., files), the OCaml typechecker can't infer the type. This limitation is mostly technical, as the typechecker works on a compilation unit level. The reason for this limitation is mostly historical, as at the time when OCaml compiler was written the separate compilation was trendy, and thus a decision was made to apply the typechecker on a per file basis. 

To summarize, if you're able to provide types that OCaml type checker will accept, then you can easily induce a mutual recursion between your compilation units. To move the typechecker from your way, you can use different mechanisms, e.g., the Obj module, or `external` definitions. Of course, in that case, you're taking the whole responsibility on the soundness of your type definitions. Basically, the same as in C language. 

Since you can always produce a whole-program compilation system from a compiler, that supports the separate compilation, by just concatenating all modules in one big module, the alternative solution would be to use some sort of a preprocessor that will collect all the definitions, and copy them into a single file (i.e., one compilation unit), and then apply the compiler to it.
Ocaml has module interface files `.mli` which play a role equivalent of header files `.h` in C and C++. Read the [§2.5 Modules and Separate Compilation](https://caml.inria.fr/pub/docs/manual-ocaml/moduleexamples.html#sec22) chapter of the Ocaml documentation. Actually what is important is the `.cmi` compiled form of these module interface files. (In C with GCC precompiled headers are [very limited](https://stackoverflow.com/a/12438040/841108)).

BTW, C++ header files are a nightmare. Most standard C++ headers (e.g. `<map>`) are actually pulling many thousands of lines. On my GCC6 Linux system, a simple file containing just `#include <vector>` is expanded to more than ten thousand lines (and this is one of the reason why compiling C++ code can be quite slow).

The C++ standardization team has discussed the incorporation of modules in the standard, but so far this has not been accepted. BTW, this suggests that modules are useful and cleaner than header files. Read [Clang's Modules](https://clang.llvm.org/docs/Modules.html) documentation.

Most recent programming language have some kind of compiled modules or packages (Ada, Go, D, ...)

C header files are a bit less messy (but C has no genericity) and practically smaller than C++ ones.

An Ocaml implementation file can (and often has) quite long `let rec` top-level definitions like

```
let rec 
 foo x y = 
 (* many lines *)
 .....
and 
 bar z t = 
 (* many lines *)
 ....
and 
 freezbee u v w = 
 (* many lines *)
 ....

(**** the above let rec definition can span many thousand lines *)

```

If you need to define mutually recursive functions across *several* Ocaml modules, you can use recursive modules as [answered by kne](https://stackoverflow.com/a/44040686/841108) or you could define references initialized to stub functions raising an exception:

```
let refoo = ref (fun (x : int) -> failwith "refun unitialized");;

```

in other parts of the same module, you would call `!refoo y` (instead of e.g. calling a `foo y`....) and you'll need some code which would fill `refoo` with a meaningful function. IIRC this trick is used a few times in the Ocaml compiler itself.