title: Not able to persist array of objects in mongo using mongoose
body: <p>I'm trying to persist an array of objects in a document using mongoose. I have tried multiple times but it's not persisting array in document. It places an empty array in document.</p>
  
  <p>Following is my Schema:</p>
  
  <pre><code>var ProfileSchema = new Schema({
  
    name: String,
    PagesData: [{
      pageAccessToken: {type: String, get: decryptText, set: encryptText},
      category: String,
      name: String,
      id: String,
      perms: [String]
    }]
  
  });
  
  module.exports = mongoose.model('Profile', ProfileSchema);
  </code></pre>
  
  <p>I'm trying to save a document with an array of objects using following query:</p>
  
  <pre><code>var newProfile = new Profile();
  newProfile.name = "someName";
  newProfile.PagesData = [ { pageAccessToken: 'someToken',
      category: 'Bags/Luggage',
      name: 'someBrandName',
      id: '12345',
      perms: 
       [ 'ADMINISTER',
         'EDIT_PROFILE',
         'CREATE_CONTENT' ] } ];
  
  newProfile.save(function(err, result, numAffected){
      if(err) {
          console.log(err);
          res.send(500, "Error");
      }
      console.log(result);
      res.send(200, "Success");
  });
  </code></pre>
  
  <p>I tried debugging the mongo commands using </p>
  
  <blockquote>
    <p>require('mongoose').set('debug', true)</p>
  </blockquote>
  
  <p>On Debug logs it shows, empty array during insert command execution.</p>
  
  <p>Can anyone please tell me how can I store this array of object in my schema ?</p>
  
  <p>Thanks,</p>
  
  <p><strong>Update:</strong></p>
  
  <p>It's been too long and I'm still not able to figure out the root cause of the problem. There is a long thread going on github for this. 
  <a href="https://github.com/Automattic/mongoose/issues/3249">https://github.com/Automattic/mongoose/issues/3249</a>
  I would like other experts to please take a look and suggest me some way by which I can solve the issue. I'm really stuck at this.</p>
  
  <p>Update 2:</p>
  
  <p>None of the solution worked for me so far, so I decided to modify the schema only to meet my requirements. This resulted in a different problem:</p>
  
  <p>I want to create a map with a objectId as key and an array of string values as its value. The closest that I can get is:</p>
  
  <pre><code>var schema = new Schema({
     map: [{myId: {type:mongoose.Schema.Types.ObjectId, ref: 'MyOtherCollection'}, values: [String]}]
  });
  </code></pre>
  
  <p>But somehow this is not working for me. When I perform an update with {upsert: true}, it is not correctly populating the key: value in the map. In fact, I'm not even sure if I have declared the schema correctly.</p>
  
  <p>Can anyone tell me if the schema is correct ? Also, How can I perform an update with {upsert: true} for this schema?</p>
  
  <p>Also, if above is not correct and can;t be achieved then how can I model my requirement by some other way. My use case is I want to keep a list of values for a given objectId. I don't want any duplicates entries with same key, that's why picked map.</p>
  
  <p>Please suggest if the approach is correct or should this be modelled some other way?</p>
  
  <p>Thanks</p>