The Doctor Moon was an artificial moon which was built by Felman Lux, Strackman Lux's grandfather, to protect CAL, his youngest daughter who became the Library's computer and was given an imaginary world in which to play while in control of the Library. He was built as a virus checker designed to monitor CAL's systems and her spiritual wellbeing. He continued to perform these functions even during the Vashta Nerada crisis. His presence is enough to stop the Doctor's sonic screwdriver working, which he did as nightfall arrived and his physical body came in range.
In Charlotte's imaginary world Doctor Moon was in human form and was treating her. He protected her and gave her advice to save the others in the Library. That was until she was in an overload and temporarily switched him off. When the Doctor saved River Song into CAL's imaginary world, the Doctor Moon was back online once more, protecting CAL, River and the rest of the team, except for Strackman Lux who survived the events. (Template:DW 2)
Doctor Moon's function in the virtual world was apparently to keep both Charlotte and others downloaded into it from realizing the unreality of the world they were in, possibly to help them keep sane. The virtual world was modeled after early 21st Century Earth (or something similar to it). Though he did inform Charlotte “she had forgotten again” and that the “world of her nightmares” (reality) was quite real perhaps in preparation of her uploading the survey team knowing they too would fall before the flesh hungry shadows.
In the virtual world he can control people's memories, including CAL's, using the simple sentence “And then …” followed by whatever command he wished from forgetting something to remembering something. Unlike CAL who could be considered to have gone mad thanks to the Vashta Nerada incident Doctor Moon was still quite sane though very logic bound.