2014年8月15日 星期五

mongo 筆記

http://docs.mongodb.org/manual/reference/operator/update/rename/
rename:

{
"occupation":"Doctor",
"name": {
   "first":"Jimmy",
   "additional":"Smith"
}

db.foo.update({}, {$rename:{"name.additional":"name.last"}}, false, true);
remap = function (x) {
  if (x.additional){
    db.foo.update({_id:x._id}, {$set:{"name.last":x.name.additional}, $unset:{"name.additional":1}});
  }
}

db.foo.find().forEach(remap);

db.students.update( { _id: 1 }, { $rename: { "nmae": "name" } } )
#rename all field
db.user.update({}, {$rename: {"session.play_data.Attack":"session.play_data.Power"}}, false, true)
#add all field
db.user.update({}, set{"session.play_data.Denfense":0}}, true, true)
[remove]
db.expTable.remove({'_id':ObjectId("52b29b8f5d89c90330cc23ad")})

#remove all documents in collection
db.expTable.remove()
#remove collections
db.expTable.drop()

[backup]
在command line下
mongodump --host 192.168.132.62 --port 27017 --out /backup/mongo-2013-01-17
#會存在 C:/backup/mongo-2013-01-17/
================[pymongo]================
from bson.objectid import ObjectId
#mike is a cursour
mike = testDB['posts'].find({'_id':ObjectId('52b144545d87c91b605dfca9')})

#modify a item
item = testDB['posts'].find({'_id':ObjectId('52b144545d87c91b605dfca9')})
item = item[0]
item['text'] = "haha"
testDB['posts'].save(item)

or

item = testDB['posts'].find_one({'_id':ObjectId('52b144545d87c91b605dfca9')})
testDB['posts'].update({'_id':ObjectId('52b144545d87c91b605dfca9')}, {"$set":{"text":"UCCU"}})

沒有留言 :

張貼留言