Finding unreferenced documents
There is a better way, but this assume that you have an index on vid field (for example, {"vid": 1} ). If you don't, you can create one using db.data.ensureIndex({"vid": 1}). This can take several minutes to end. 

The fastest solution (option 2) use $group after a $project  Here the $project stage only add overhead and does not speed up the $group stage, so we can get rid of it.

Currently, the $group operator can't take advantage of index, so it can be quite slow for such big dataset. A better solution would be to use distinct() instead


  Index Use
  
  When possible, db.collection.distinct() operations can use indexes.


so we can fetch withData array simply using this: 

let withData = db.data.distinct("vid")


Then, the datagroup collection is filtered using map() and filter(). On really big array, this can be really expensive as javascript is quite slow. Instead, we can let mongodb do this operation with a simple query using $nin:

let withoutData = db.datagroup.distinct("_id", {"_id": {$nin: withData}})


As distinct() returns an array, we get directly the result in proper format, so we can avoid js mapping and filtering. 

so the final script is:

let withData = db.data.distinct("vid")
let withoutData = db.datagroup.distinct("_id", {"_id": {$nin: withData}})




Some results

on my machine (ubuntu16.04) , with a similar dataset ( 100 000 000 docs in data, 20000 in datagroup)  

New script runs in less than 5s :

time mongo test &lt; newOption.js
0,06s user 0,00s system 1% cpu 3,534 total