Failed to save array of objects in mongo using mongoose

I am trying to save an array of objects in a document using mongoose. I tried several times but did not save the array in the document. It puts an empty array into the document.

Below is my diagram:

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); 

I am trying to save a document with an array of objects using the following query:

 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"); }); 

I tried debugging mongo commands with

require ('mongoose'). set ('debug', true)

Debug logs display an empty array during the execution of an insert command.

Can anyone tell me how I can save this array of object in my schema?

Thanks,

Update:

It has been too long, and I still cannot understand the root cause of the problem. There is a long thread for github for this. https://github.com/Automattic/mongoose/issues/3249 I would like other experts to catch the eye and offer me some way with which I can solve this problem. I am really stuck with this.

Update 2:

None of the solutions have worked for me so far, so I decided to change the scheme only to meet my requirements. This led to another problem:

I want to create a map with the objectId key and an array of string values ​​as the value. The closest I can get is:

 var schema = new Schema({ map: [{myId: {type:mongoose.Schema.Types.ObjectId, ref: 'MyOtherCollection'}, values: [String]}] }); 

But for some reason this does not work for me. When I update using {upsert: true}, it fails to populate the key value: on the map. In fact, I'm not even sure if I declared the schema correctly.

Can someone tell me if the circuit is correct? Also, how can I upgrade using {upsert: true} for this schema?

In addition, if the above is incorrect and can be achieved, then how can I simulate my requirement in some other way. My use case I want to keep a list of values ​​for this object. I do not want duplicate records with a single key, so I chose a map.

Please propose whether this approach is correct or should it be modeled in some other way?

thanks

+6
source share
4 answers

I tried the exact code you provided here and it works for me. I am not sure what causes the problem. Until we get the same problem, it’s very difficult to fix it. Here are some tips you can try:

  • Create a simple diagram and try to save the object, so you can figure out if you need to do something with this diagram.
  • You can try your circuit in an example application to see if dependency is causing the problem.

Once you know exactly where the problem is, you can also find a solution. Hope this helps.

+8
source

I tested this and the insert works for me using below: (I had to remove get: decryptText, set: encryptText)

 var n = { name: "Testing for mongoose", PagesData : [{ pageAccessToken: 'someToken', category: 'Bags/Luggage', name: 'someBrandName', id: '12345', perms: [ 'ADMINISTER', 'EDIT_PROFILE', 'CREATE_CONTENT' ] } ] } Profile.create(n, function (err) { if (!err) { return 'records saved successfully'; } else { return error on save:' + err; } }); 

Screenshot from DB result:

+5
source

To create multiple pageDatas, you can use it as an inline collection instead of using arrays.

The scheme will look as follows:

 var PagesDataSchema = new Scheme({ pageAccessToken: {type: String, get: decryptText, set: encryptText}, category: String, name: String, id: String, perms: [String] }) var ProfileSchema = new Schema({ name: String, PagesData: [PagesDataSchema] }); module.exports = mongoose.model('Profile', ProfileSchema); 

Link : http://mongoosejs.com/docs/subdocs.html

To save a document that you can use.

 exports.save = function(req,res){ var test = new ProfileSchema; // new object for ProfileSchema domain. test.name= req.body.name; if(req.body.PagesData){ req.body.PagesData.forEach(function(page){ // For every element of pageData from client. test.PagesData.push(page) // This pushes each and every pagedata given from the client into PagesData. }) } test.save(function (saveErr, saved) { // Saves the new document into db. if (saveErr) { console.log(saveErr) return; } res.status(HttpStatus.OK).json(saved); }); }; 

Hope this helps.

+3
source

You tried

 Profile.create({ name: "someName", PagesData: [ { pageAccessToken: 'someToken', category: 'Bags/Luggage', name: 'someBrandName', id: '12345', perms: [ 'ADMINISTER', 'EDIT_PROFILE', 'CREATE_CONTENT' ] } ] }, function(err, profile) { // do your stuff }) 

?

0
source

All Articles