Assigning the results of a find operation to a variable is not a direct reference to that document?

Hey all,
So in the video, I noticed that the instructor wanted to assign a new property of box office to the “The Martian” document… He assigned the document to a variable, removed the id property, added the boxOffice property, and then inserted this second version into the collection.
So normally in JavaScript, assigning a variable to equal an object means the variable has a reference to that object - any changes made to that object via the variable will also affect the original object.
let a = {key: “value”};
let b = a; = 4; //a will also have id = 4;
So does MongoDB’s findOne return basically a deep copy of the object? And not a reference to it?

Looking up the findOne operation on the MongoDB docs, I noted this line: " Although similar to the find() method, the findOne() method returns a document rather than a cursor."
So the document returned is not a direct reference to the document? Am I understanding this correctly? :smiley:


The same document also goes on to say:

Basically, with findOne(), you won’t be able to use any of these cursor methods to traverse the documents. It’s no longer a pointer to the database iterable object because it returns just one document so there’s no point creating an iterable cursor object, just return a deep copy of the document.

1 Like

Awesome, thanks!