1

Just trying to figure out some questions.

context: randomFunction is a function that takes 2 arguments(id & metaData). It is a called inside a controller . The code is given below:

await randomFunction(id, doc);

doc contains a object (basically a document in mongodb). suppose doc contains the following:

{
    _id: "123456789012345678901234",
    age: 30,
    name: 'Lorem Ipsum',
    gender: 'male'
}

Now , in the operations file where the randomFunction is initialized.

const randomFunction = async (id, metaData) => {
   console.log(metaData) // prints metaData
   delete metaData._id
   console.log(metaData) // still prints metaData without deleting _id property
   //code below
}

context: I wanted to delete the _id property of MetaData to pass it to the findOneAndUpdate() function in mongooose. But I was not able to succeed in deleting _id property. So I created the data object and passed it.

const randomFunction = async (id, metaData) => {
   const data = {
     name: metaData.name,
     age: metaData.age,
     gender: metaData.gender
   }
   //code below
}

It worked.

I then tested and console logged a few things.

const randomFunction = async (id, metaData) => {
    console.log(metaData) // prints metaData
    delete metaData._id
    console.log(metaData) // still prints metaData without deleting _id property
    const data = {
     _id: metaData._id,
     name: metaData.name,
     age: metaData.age,
     gender: metaData.gender
   }
    console.log(data) // prints data object
    delete data._id
    console.log(data) // prints data with deleting _id property
   //code below
}

So , What is the reason I was not able to delete _id in MetaData but able to able _id in Data.

When I was trying to reproduce the same thing in javascript console(chrome), I was not able to reproduce. (it is deleting the properties from arguments also.).

Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
  • You haven't asked any questions. – Scott Hunter Feb 18 '21 at 18:43
  • Can't reproduce, both are logging as expected for me ('gender' property deleted) – pilchard Feb 18 '21 at 18:43
  • Yes in console both are logging as expected so even I cannot reproduce the same. Will reframe my question. – Siddhesh Swami Feb 18 '21 at 19:20
  • I can't reproduce the problem. How are you setting the initial value for metaData? – A Haworth Feb 18 '21 at 21:43
  • I have reframed the question & added node js tag. I too am unable to reproduce it in console. MetaData is a Document of mongodb that is got by findById(). Thank you all for your responses. – Siddhesh Swami Feb 20 '21 at 06:41
  • @SiddheshSwami It does look like you can't delete `_id` from a Mongoose `Document` object. I did a bit of experimenting to see if `doc.set('_id', undefined)` and `doc._id = undefined` delete the `_id` field as mentioned [here](https://github.com/Automattic/mongoose/issues/4922) but they regenerate a new `_id` value instead. Someone more experienced with Mongoose might be able to explain the reasoning behind the behavior. – Arun Kumar Mohan Feb 20 '21 at 23:32
  • hey arun. it is not only about _id. all properties cannot be deleted. I tried deleting other properties also . like i had a object inside that doc. I tried deleting that too. but it does not get deleted. – Siddhesh Swami Feb 22 '21 at 12:35
  • @ArunKumarMohan I found out the solution. Mongoose documents are immutable. – Siddhesh Swami Mar 01 '21 at 04:58

1 Answers1

1

Maybe someone will find it useful.

The answer is the mongoose documents are immutable.

We need to convert the mongoose object to the javascript object.

The reason is as follows:

Link to appropriate question and answer:

https://stackoverflow.com/a/13350500/14619863