The MQL commands I usually run in Robo 3T do not work in MongoDB for VS Code


I am currently trying to use MongoDB for VS Code to run CRUD commands that I would usually run in Robo 3T but they are not working - example script that works as intended in Robo 3T:

> var email = '<user email>';
> var user = db.getCollection('<collection name 1>').find({email: email});
> db.getCollection('<collection name 1>').deleteMany({platformUserId: user[0]._id})
> db.getCollection('<collection name 2>').deleteMany({email: email})
> db.getCollection('<collection name 3>').deleteMany({email: email})

When I try to run this in a playground I get the error ‘Cannot read property ‘_id’ of undefined’

Any ideas?


In the code you share the only place we see _id access is in

Since you use user[0] then it must be that user[0] really does not exist. You code is not safe because you use the result of db…find() without testing for a result or not. Many reasons might explain why you do not get user[0].

  1. You are not connected to the appropriate server
  2. You are not using the appropriate database
  3. You are not calling getCollection() with the appropriate collection name
  4. There is no user that matches { “email” : email }
  5. You might need to await when you initialize user with “db…find(…)”
1 Like