Question about deleteOne

According to the node documentation:

deleteOne(filter, options, callback){Promise}
filter object The Filter used to select the document to remove|

In the original code in UserDao line 137, in the deleteUser(email) function:
await users.deleteOne({ email })

In user-management.test.js line 19, the email was passed as:
await UsersDAO.deleteUser(testUser.email)
which is a string, not an object.

Why is the above delete operation working?
How does the database know where to look without a field been specified?

Hey @danchann,

That syntax is actually new to ES6 and is just a shorthand for initializing an object. So in the first example you have, you are essentially saying create an object with a key of whatever I put between { } and then assign its value to whatever the value of that was.

So with await UsersDAO.deleteUser(testUser.email) you are just passing a string to a function UsersDAO.deleteUser()that doesn’t actually have any type restrictions. Then, in that function, it attempts the query await users.deleteOne({ email }) which due to the { } is actually just a shortcut for {email: "value of email"}. You can read a bit more detailed explanation on the Mozilla site, here.

Thanks for the question! There is a lot of syntactic sugar that comes with newer Ecmascript versions and keeping track of it can be tough, to say the least!

Thanks,
Chris

1 Like

Hey Greg,

I am not sure that I agree with what you have there but I am open to discussion, I have no problem admitting error. However, your article is for destructuring an object, while this situation is actually for initializing an object from a string.
With destructuring you take an object and assign its value to a variable, such as this example from your article:

var o = {p: 42, q: true};
var {p, q} = o;

Here o has two fields p: 42 and q: true and the second line assigns the value of p and q to their own variables with the same name. This would be the equivalent of var p = o.p and var q = o.q.

Where as with this question was regarding initializing an object, such as this from my article:

var a = 'foo', 
    b = 42, 
    c = {};

// Shorthand property names (ES2015)
var o = {a, b, c};

This actually results in the creation of an object with the fields and values of a,b,c. You could kind of look at it like saying var o = {},o.a = a, o.b = b, o.c = c which would result in o = { "a" : "foo", "b" : 42, "c" : { }}.

So with this specific situation because we pass the variable email which just contains a string(not an obj) into { } it initializes an object with key email and a value of its value. resulting in {email: 'value of email'} which is the required format needed for the deleteOne filter document.

I do believe my original answer was correct but, If I am still wrong on this, please let me know, I have certainly been confused before!

Thanks,
Chris

I’m sorry, you are correct, I was thinking of something else.
The deleteOne function accepts an object.In this case shorthand property names are used to initialize an object.

{ email }
is the same as
{ email: email }

I’ve removed my post above as not to confuse anyone.