2

I have this model Player:

"sport_id" : ObjectId("512db94e6ee1f54932000001"),
"team_id" : ObjectId("512dbaf36ee1f5523e00000a"),
"twitter" : false,
"twitter_account" : "@brianhartline",
"updated_at" : ISODate("2013-03-06T10:37:45.943Z"),
"version" : 7,

I have removed twitter from my model but database has field twitter. I dont want to drop the playerDB but I want to delete the twitter field in existing database.

what I have to do?

Community
  • 1
  • 1
God
  • 674
  • 2
  • 6
  • 31

3 Answers3

1

You can try to remove the attribute like this

player.attributes.without('twitter')
mart1nn
  • 211
  • 3
  • 6
1

This should work:

Player.each do |player|
  player.unset(:twitter)
end
rjurado01
  • 5,337
  • 3
  • 36
  • 45
  • drinor i want twitter column should be removed in the database if i used ur code it will be nil the database. – God Mar 06 '13 at 11:48
  • If you want remove twitter from database you should remove field from your Player model. – rjurado01 Mar 06 '13 at 11:53
  • ya i done it before, but if exciting database has field twitter, i dont want to drop the playerDB i want to delete only the twitter field in exciting databse. what i have to do? – God Mar 06 '13 at 12:00
  • see this question: http://stackoverflow.com/questions/2831059/how-to-drop-columns-using-rails-migration – rjurado01 Mar 06 '13 at 12:07
  • thanks, i drop the DB. link what u gave was is for ruby for me mongo is needed. – God Mar 06 '13 at 12:13
  • I don't check it but, see 'Remove a Field from a Document' in http://docs.mongodb.org/manual/applications/update/#Updating-%24unset. For the next time xD. – rjurado01 Mar 06 '13 at 12:34
0

To do this in the mongo shell:

db.players.update({}, {$unset: {twitter: 1}}, {multi: true})
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471