Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use "delete" operator on mongoose query results

I'm trying to delete a key before sending data back to the browser. For some reason, maybe because it is a mongoose object, this isn't working:

delete myObject.property

If I do console.log(delete myObject.property) I get true which I understand means the property was not deleted. How can I get rid of this key?

(More background: I know I could leave it off by using a mongoose select query, but I do need to select the key initially. I can also set it to null, which works fine, but I'd much rather get rid of the key completely)

like image 321
Glenn Avatar asked Jan 10 '18 22:01

Glenn


People also ask

How do you delete an item from MongoDB using Mongoose?

There is currently no method called deleteById() in mongoose. However, there is the deleteOne() method with takes a parameter, filter , which indicates which document to delete. Simply pass the _id as the filter and the document will be deleted.

What is the Mongoose command used to delete an item?

Mongoose | deleteOne() Function The deleteOne() function is used to delete the first document that matches the conditions from the collection. It behaves like the remove() function but deletes at most one document regardless of the single option.

Why we use lean in Mongoose?

The lean option tells Mongoose to skip hydrating the result documents. This makes queries faster and less memory intensive, but the result documents are plain old JavaScript objects (POJOs), not Mongoose documents.

Can we overwrite Mongoose default ID with own id?

You can also overwrite Mongoose's default _id with your own _id . Just be careful: Mongoose will refuse to save a document that doesn't have an _id , so you're responsible for setting _id if you define your own _id path.


2 Answers

As MikaS states, you need to convert to convert the Mongoose document object into an ordinary object first:

const newObj = myObject.toObject();
delete newObj.property;

This should be fine if you're doing this once. However, if you have to repeat this logic everywhere you return this this type of document with certain keys omitted or other transformations, you should define transformations on your schema:

// specify the transform schema option
if (!schema.options.toObject) schema.options.toObject = {};
schema.options.toObject.transform = function (doc, ret, options) {
  // any kind of simple/complicated transformation logic here
  // remove the _id of every document before returning the result
  delete ret._id;
  return ret;
}

// without the transformation in the schema
doc.toObject(); // { _id: 'anId', name: 'Wreck-it Ralph' }

// with the transformation
doc.toObject(); // { name: 'Wreck-it Ralph' }

Read about Document#toObject and transformations in the docs

like image 192
Govind Rai Avatar answered Oct 23 '22 03:10

Govind Rai


Convert the Mongoose document object to an ordinary Javascript object first:

const newObj = myObject.toObject();
delete newObj.property;
like image 25
Mika Sundland Avatar answered Oct 23 '22 03:10

Mika Sundland