• mongodb update 字符 操作


    mongodb update 字符 操作

    张映 发表于 2014-07-23

    分类目录: nosql

    标签:

    下面常用的update操作,用mongodb2.6.3版本测试的,官方发布的稳定版本2.4,建议用稳定版。

    一,upsert表示如果有数据就不插入,没数据就插入

    1,命令行下

    1. > db.peoples.update(        //查找name等于tank的用户  
    2. ... { name: "tank" },  
    3. ... {  
    4. ... "_id":1,  
    5. ... name: "Andy",  
    6. ... rating: 10,  
    7. ... score: 10  
    8. ... },  
    9. ... { upsert: true }      //如果没有就插入  
    10. ... );  
    11. WriteResult({ "nMatched" : 0, "nUpserted" : 1, "_id" : 1 })  
    12. > db.peoples.find();  
    13. "_id" : 1, "name" : "Andy", "rating" : 10, "score" : 10 }  
    14. > db.peoples.update(  
    15. ... { _id: 1 },  
    16. ... {  
    17. ... "_id":1,  
    18. ... name: "Andy",  
    19. ... rating: 10,  
    20. ... score: 10  
    21. ... },  
    22. ... { upsert: true }  
    23. ... );  
    24. WriteResult({ "nMatched" : 1, "nUpserted" : 0 })   //有匹配数据就不做插入操作  
    25. > db.peoples.find();  
    26. "_id" : 1, "name" : "Andy", "rating" : 10, "score" : 10 }  

    2,php upsert操作

    1. $collection->update(  
    2.  array("name" => "zhang"),  
    3.  array("_id"=>2,"name"=>"tank","rating"=>10,"score"=>10),  
    4.  array("upsert" => true)  
    5. );  
    6. print_r($collection->findOne());  

    二,$set 替换值

    1,命令行下操作

    1. > db.peoples.find();  
    2. "_id" : 1, "name" : "Andy", "rating" : 10, "score" : 10 }  
    3. > db.peoples.update(  
    4. ... { _id: 1 },  
    5. ... {  
    6. ... $set: { rating: 18 }  
    7. ... }  
    8. ... );  
    9. WriteResult({ "nMatched" : 1, "nUpserted" : 0 })  
    10. > db.peoples.find();  
    11. "_id" : 1, "name" : "Andy", "rating" : 18, "score" : 10 }  

    2,php $set操作

    1. $where = array("_id"=>1);  
    2. $param = array('$set'=>array("score"=>"100")); //注意此处的set必须为 单引号  
    3. $collection->update($where,$param);  
    4. print_r($collection->findOne());  

    三,$inc如果没有对应对段就直接赋值,如果有在原来的值上加上该值

    1,命令行下操作

    1. > db.peoples.find();  
    2. "_id" : 1, "name" : "Andy", "rating" : 28, "score" : 10 }  
    3. > db.peoples.update(  
    4. ... { _id: 1 },  
    5. ... {  
    6. ... $inc: { age: 30 }  
    7. ... }  
    8. ... );  
    9. WriteResult({ "nMatched" : 1, "nUpserted" : 0 })  
    10. > db.peoples.find();  
    11. "_id" : 1, "age" : 30, "name" : "Andy", "rating" : 28, "score" : 10 }    //第一次,加了一个字段  
    12. > db.peoples.update(  
    13. ... { _id: 1 },  
    14. ... {  
    15. ... $inc: { age: 30 }  
    16. ... }  
    17. ... );  
    18. WriteResult({ "nMatched" : 1, "nUpserted" : 0 })  
    19. > db.peoples.find();  
    20. "_id" : 1, "age" : 60, "name" : "Andy", "rating" : 28, "score" : 10 }   //第二次,发现有这个字段就把值加上去了  

    2,php $inc操作

    1. $where = array("_id"=>1);  
    2. $param = array('$inc'=>array("age"=>30));  
    3. $collection->update($where,$param);  
    4. print_r($collection->findOne());  

    四,$unset删除字段

    1,命令行下操作

    1. > db.peoples.find();  
    2. "_id" : 1, "age" : 120, "name" : "Andy", "rating" : 28, "score" : 10 }  
    3. > db.peoples.update(  
    4. ... { _id: 1 },  
    5. ... {  
    6. ... $unset: { age: ""}    //删除age字段  
    7. ... }  
    8. ... );  
    9. WriteResult({ "nMatched" : 1, "nUpserted" : 0 })  
    10. > db.peoples.find();  
    11. "_id" : 1, "name" : "Andy", "rating" : 28, "score" : 10 }  
    12. >  

    2,php $unset操作

    1. $where = array("_id"=>1);  
    2. $param = array('$unset'=>array("score"=>""));  
    3. $collection->update($where,$param);  
    4. print_r($collection->findOne());  

    五,$rename修改字段名称

    1,命令行下操作

    1. > db.peoples.find();  
    2. "_id" : 1, "name" : "Andy", "rating" : 28 }  
    3. > db.peoples.update(  
    4. ... { _id: 1 },  
    5. ... { $rename: { "name": "firstname" } }   //将name改成firstname  
    6. ... );  
    7. WriteResult({ "nMatched" : 1, "nUpserted" : 0 })  
    8. > db.peoples.find();  
    9. "_id" : 1, "firstname" : "Andy", "rating" : 28 }  

    2,php $rename 操作

    1. $where = array("_id"=>1);  
    2. $param = array('$rename'=>array("firstname"=>"name"));   //将firstname改成name  
    3. $collection->update($where,$param);  
    4. print_r($collection->findOne());  

    六,multi更新多条数据

    1,命令行下操作

    1. > db.peoples.find();  
    2. "_id" : 1, "name" : "Andy", "rating" : 28 }  
    3. "_id" : 2, "name" : "zhang", "rating" : 3, "score" : 5 }  
    4. "_id" : 3, "name" : "hao", "rating" : 30, "score" : 5 }  
    5. > db.peoples.update(  
    6. ... { rating: { $gt: 15 } },  
    7. ... { $inc: { score: 10 } },  
    8. ... { multi: true }  
    9. ... );  
    10. WriteResult({ "nMatched" : 2, "nUpserted" : 0 })  
    11. > db.peoples.find();  
    12. "_id" : 1, "name" : "Andy", "rating" : 28, "score" : 10 }      //这条数据更新了  
    13. "_id" : 2, "name" : "zhang", "rating" : 3, "score" : 5 }  
    14. "_id" : 3, "name" : "hao", "rating" : 30, "score" : 15 }     //这条数据更新了  

    2,php multi操作

    1. $where = array("rating"=>array('$gt'=>10));  
    2. $param = array('$set'=>array("name"=>"tank1","rating"=>50));  
    3. $ismore = array("multiple" => true);  
    4. $collection->update($where,$param,$ismore);  

    版本不一样,功能多少会不一样,例如,我用的版本是2.6.3,$min和$max就用不了,会报"errmsg" : "Invalid modifier specified $min",希望mongodb出一个稳定高版本。

    转载请注明
    作者:海底苍鹰
    地址:http://blog.51yip.com/nosql/1638.html
  • 相关阅读:
    返回三级联动的JSON数据
    返回三级联动的JSON数据
    python3访问map
    第十八讲、中介者模式
    第十七讲、命令模式
    第十六讲、模板方法模式
    第十五讲、组合模式
    第十四讲、享元模式
    第十三讲、装饰器模式
    第十二讲、桥接模式
  • 原文地址:https://www.cnblogs.com/cynleely/p/7842848.html
Copyright © 2020-2023  润新知