Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:32:57+00:00 2026-06-18T14:32:57+00:00

Consider I have a Mongoose schema defined var Schema_MySchema = new mongoose.Schema({ Field1: String,

  • 0

Consider I have a Mongoose schema defined

var Schema_MySchema = new mongoose.Schema({
Field1: String, Field2: String
});

I have added virtual attribute & setting options in the following way:

Schema_MySchema.virtual('USERNAME').get(function () {
    return this.Field1;
});

Schema_MySchema.set('toJSON', { virtuals: true });

Using the schema object to retrieve the data from MongoDB as follows:

var Mod_Obj = mongoose.model('SchemaName', Schema_MySchema);
var Model_Instance = new Mod_Obj();
Model_Instance.find({}, function (err, docs) {
   /* 
      Here I get a object docs with following structure:
      [{ _id: ObjectId("511XXXdff9c4c419000008"), 
          Field1: 'SOMEvalueFromMongoDB_1', 
          Field2: 'SOMEvalueFromMongoDB_2',
          USERNAME: 'SOMEvalueFromMongoDB_1'
      }]
  */
});

Now I want to remove actual attribute returned from MongoDB, say I want to remove Field1 from docs

I tried following ways to remove:

1. delete docs.Field1;

2. var Json_Obj = docs.toJSON(); delete Json_Obj.Field1;

3. var Json_Obj = docs.toObject({ virtuals: true }); delete Json_Obj.Field1;

4. delete docs[0].Field1;

5. delete docs[0]['Field1'];

All the ways didn’t work. 🙁

If execute for testing on simple JSON it worked:

var a = { 'A' : 1, 'B': 2 };
delete a.A;
console.log(a); //prints only object with B attribute only. i.e. { B: 2 }

Could anyone tell me whats wrong here?

Thanks in Advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:32:58+00:00Added an answer on June 18, 2026 at 2:32 pm

    I believe you are missing the index there as docs is an array.

    if your docs has the following structure

    [{ _id: ObjectId("511XXXdff9c4c419000008"), 
          Field1: 'SOMEvalueFromMongoDB_1', 
          Field2: 'SOMEvalueFromMongoDB_2',
          USERNAME: 'SOMEvalueFromMongoDB_1'
      }]
    

    the deletion code should be

    delete docs[index]['Field1']
    

    ie. to delete Field1 of 1st element

    delete docs[0]['Field1']
    

    UPDATED TO COMMENTS:

    please refer to this answer on SO.

    Here you will need to do like

    var k = docs[index].toObject();
    delete k['Field1'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider, Mongoose schema: var schema_obj = new Schema({ field1: String, field2: String, ......... });
Consider we have a simple cycling Javascript process as: function test() { el=document.getElementById("test"); var
Consider I have a some Factory class: class Factory { public: Factory(); virtual ~Factory();
Consider I have a JSON string of the following format: How do I parse
Consider you have the following code: public abstract class MenuItem { protected string m_Title;
Consider you have 2 classes: Student and Course and a Course can be added
Consider I have a file new.txt like, asdfg qwerty zcx poi Now i need
Consider I have the following classes: /// File classes.d class C { string type()
Consider I have a variable class_name . class_name can hold string values like blog
Consider I have a Singleton class defined as follows. public class MySingleton implements Serializable{

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.