{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3249","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3249/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3249/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3249/events","html_url":"https://github.com/Automattic/mongoose/issues/3249","id":100087313,"number":3249,"title":"Not able to persist array of objects in mongo using mongoose","user":{"login":"mukulgupta2507","id":4299536,"avatar_url":"https://avatars.githubusercontent.com/u/4299536?v=3","gravatar_id":"","url":"https://api.github.com/users/mukulgupta2507","html_url":"https://github.com/mukulgupta2507","followers_url":"https://api.github.com/users/mukulgupta2507/followers","following_url":"https://api.github.com/users/mukulgupta2507/following{/other_user}","gists_url":"https://api.github.com/users/mukulgupta2507/gists{/gist_id}","starred_url":"https://api.github.com/users/mukulgupta2507/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mukulgupta2507/subscriptions","organizations_url":"https://api.github.com/users/mukulgupta2507/orgs","repos_url":"https://api.github.com/users/mukulgupta2507/repos","events_url":"https://api.github.com/users/mukulgupta2507/events{/privacy}","received_events_url":"https://api.github.com/users/mukulgupta2507/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Automattic/mongoose/labels/can't%20reproduce","name":"can't reproduce","color":"bfdadc"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/52","html_url":"https://github.com/Automattic/mongoose/milestones/4.1.3","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/52/labels","id":1224161,"number":52,"title":"4.1.3","description":"","creator":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"open_issues":16,"closed_issues":5,"state":"open","created_at":"2015-07-24T15:00:20Z","updated_at":"2015-08-11T22:24:34Z","due_on":"2015-08-14T04:00:00Z","closed_at":null},"comments":8,"created_at":"2015-08-10T14:58:04Z","updated_at":"2015-08-12T18:58:59Z","closed_at":null,"body":"Hi,\r\n\r\nI'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.\r\n\r\nFollowing is my Schema:\r\n\r\n```\r\nvar ProfileSchema = new Schema({\r\n\r\n  name: String,\r\n  PagesData: [{\r\n    pageAccessToken: {type: String, get: decryptText, set: encryptText},\r\n    category: String,\r\n    name: String,\r\n    id: String,\r\n    perms: [String]\r\n  }]\r\n\r\n});\r\n\r\nmodule.exports = mongoose.model('Profile', ProfileSchema);\r\n```\r\nI'm trying to save a document with an array of objects using following query:\r\n```\r\nvar newProfile = new Profile();\r\nnewProfile.name = \"someName\";\r\nnewProfile.PagesData = [ { pageAccessToken: 'someToken',\r\n    category: 'Bags/Luggage',\r\n    name: 'someBrandName',\r\n    id: '12345',\r\n    perms: \r\n     [ 'ADMINISTER',\r\n       'EDIT_PROFILE',\r\n       'CREATE_CONTENT' ] } ];\r\n\r\nnewProfile.save(function(err, result, numAffected){\r\n    if(err) {\r\n        console.log(err);\r\n        res.send(500, \"Error\");\r\n    }\r\n    console.log(result);\r\n    res.send(200, \"Success\");\r\n});\r\n```\r\nI tried debugging the mongo commands using\r\n```\r\n    require('mongoose').set('debug', true);\r\n```\r\nOn Debug logs it shows, empty array during insert command execution.\r\n\r\nCan anyone please tell me how can I store this array of object in my schema ?\r\n\r\nThanks,\r\n"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/130411995","html_url":"https://github.com/Automattic/mongoose/issues/3249#issuecomment-130411995","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3249","id":130411995,"user":{"login":"mukulgupta2507","id":4299536,"avatar_url":"https://avatars.githubusercontent.com/u/4299536?v=3","gravatar_id":"","url":"https://api.github.com/users/mukulgupta2507","html_url":"https://github.com/mukulgupta2507","followers_url":"https://api.github.com/users/mukulgupta2507/followers","following_url":"https://api.github.com/users/mukulgupta2507/following{/other_user}","gists_url":"https://api.github.com/users/mukulgupta2507/gists{/gist_id}","starred_url":"https://api.github.com/users/mukulgupta2507/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mukulgupta2507/subscriptions","organizations_url":"https://api.github.com/users/mukulgupta2507/orgs","repos_url":"https://api.github.com/users/mukulgupta2507/repos","events_url":"https://api.github.com/users/mukulgupta2507/events{/privacy}","received_events_url":"https://api.github.com/users/mukulgupta2507/received_events","type":"User","site_admin":false},"created_at":"2015-08-12T18:58:59Z","updated_at":"2015-08-12T18:58:59Z","body":"I have updated your script but it is working fine in your case. I'm still stuck in my case and will be thankful to you if you can provide any help in this case.\r\n\r\n\r\n```\r\nvar assert = require('assert');\r\nvar mongoose = require('mongoose');\r\nvar crypto = require('crypto');\r\n\r\nmongoose.set('debug', true);\r\nmongoose.connect('mongodb://localhost/test');\r\n\r\nvar cryptKey = crypto.createHash('sha256').update('Nixnogen').digest();\r\nvar iv = \"someIv\";\r\n\r\nvar Schema = mongoose.Schema;\r\n\r\nvar ProfileSchema = new Schema({\r\n\r\n  name: String,\r\n  PagesData: [{\r\n    pageAccessToken: { type: String, get: decryptText, set: encryptText },\r\n    category: String,\r\n    name: String,\r\n    id: String,\r\n    perms: [String]\r\n  }],\r\n  type: String\r\n\r\n});\r\n\r\nfunction encryptText(text){\r\n\t\tvar cipher = crypto.createCipher('aes-256-cbc', cryptKey, iv);\r\n\t\tvar crypted = cipher.update(text,'utf8','binary');\r\n\t\tcrypted += cipher.final('binary');\r\n    crypted = new Buffer(crypted, 'binary').toString('base64');\r\n\t\treturn crypted;\r\n}\r\n\r\nfunction decryptText(text){\r\n\t\tif (text === null || typeof text === 'undefined' || text === '') {return text;};\r\n    text = new Buffer(text, 'base64').toString('binary');\r\n\t\tvar decipher = crypto.createDecipher('aes-256-cbc', cryptKey, iv);\r\n\t\tvar dec = decipher.update(text,'binary','utf8');\r\n\t\tdec += decipher.final('utf8');\r\n\t\treturn dec;\r\n}\r\n\r\nvar Profile = mongoose.model('Profile', ProfileSchema);\r\n\r\nvar newProfile = new Profile();\r\nnewProfile.name = \"someName\";\r\nvar data = [];\r\nvar dataObj = {pageAccessToken:'iqULIV8BssssAIFJbchZAla3MF90UFZA6Q', category: 'Bags/Luggage', name: 'someBrandName', id: '12345', perms:    [ 'ADMINISTER',\r\n     'EDIT_PROFILE',\r\n     'CREATE_CONTENT',\r\n     'MODERATE_CONTENT',\r\n     'CREATE_ADS',\r\n     'BASIC_ADMIN' ]};\r\ndata.push(dataObj);\r\nnewProfile.type = \"facebook\";\r\n\r\nnewProfile.PagesData = data;\r\nconsole.log(JSON.stringify(newProfile));\r\nvar obj = newProfile.toObject();\r\ndelete  obj._id;\r\n\r\nProfile.update({type: \"facebook\"}, obj, {upsert: true},function(err, result){\r\n\tif(err) {\r\n\t\tconsole.log(err);\r\n\t}\r\n\tconsole.log(result);\r\n});\r\n```\r\n\r\nIn my case, why newProfile.PagesData is not storing the correct value, please  help me.\r\n\r\nThanks"}}