开发者社区> 问答> 正文

mongodb,update方法无效

我的数据里有
{ "_id" : ObjectId("509b645910ade1be6c000012"), "name" : "aaa", "age" : "21" }
{ "_id" : ObjectId("509b645910ade1be6c000013"), "name" : "bbb", "age" : "20" }
{ "_id" : ObjectId("509b645910ade1be6c000014"), "name" : "ccc", "age" : "25" }
{ "_id" : ObjectId("509b645d10ade1be6c000015"), "name" : "aaa", "age" : "21" }
{ "_id" : ObjectId("509b645d10ade1be6c000016"), "name" : "bbb", "age" : "20" }
{ "_id" : ObjectId("509b645d10ade1be6c000017"), "name" : "ccc", "age" : "25" }
{ "_id" : ObjectId("509b645e10ade1be6c000018"), "name" : "aaa", "age" : "21" }
{ "_id" : ObjectId("509b645e10ade1be6c000019"), "name" : "bbb", "age" : "20" }
{ "_id" : ObjectId("509b645e10ade1be6c00001a"), "name" : "ccc", "age" : "25" }
然后使用PHP修改数据
`$conn = new Mongo("mongodb://root:111111@127.0.0.1/local");
$db = $conn->local; //选择test数据库
$collection = $db->phptest; //若此表不存在则会自行创建
$newdata = array("$set" => array("age" => "888811111"));
$collection->update(array('name' => 'aaa'), $newdata,false,true);`
执行PHP之后数据没有改变,这是什么原因呢?

展开
收起
落地花开啦 2016-02-19 14:00:29 4493 0
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

    首先是你update函数用得似乎不对。文档上:
    public bool|array MongoCollection::update ( array $criteria , array $new_object [, array $options = array() ] )
    其次,你这里涉及到多条数据update,所以$options=array('multiple'=>1)如果设定为安全操作,最好还要加上 'safe'=>1

    2019-07-17 18:44:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Data as a Service - 数据即服务 -- MongoDB⾼级应⽤模式 立即下载
阿里云MongoDB云服务构建 立即下载
饿了么高级架构师陈东明:MongoDB是如何逐步提高可靠性的 立即下载