0

I need to remove a sub field from all the elements in a collection using mongoose, I created a JS script for this few weeks ago and as far as I can recall it worked back then, but now seems like is not working anymore. What I was basically doing is:

Collection.find({'supField.myfield': {$exists: true}})
  .then(function(dataList){
      var dataId = dataList[0]._id;
      // some other stuff here
      return Collection.update({_id: dataId}, {'$unset' : {'supField.myfield':1}})
  })
  .then(function(updated){
      console.log(updated) // dumps 0
  })

If requiring mongoose i set the debug flag (require('mongoose').set('debug', true)) it dumps no update query.

This behavior is strange and I'm afraid it's been caused by some update, but it can also be some mistake on my side.

[EDIT] Apparently is a problem connected with the fact that I'm trying to update a subfield and not a normal field. But still I got no evidence of this being a problem

bncc
  • 478
  • 7
  • 18
  • Have you tried unsetting with `Collection.update({"_id": dataId}, {"$unset": {"supField.myfield": ""}})`? – chridam Jun 05 '15 at 14:24
  • Just tried, not much changes. Query syntax looks quite correct to me, I took it from [this post](http://stackoverflow.com/a/6938733/2689638) – bncc Jun 05 '15 at 14:28
  • Thanks @JohnnyHK, apparently that's exactly the reason! – bncc Jun 09 '15 at 14:19

0 Answers0