MongoDB更新

文章目录
[隐藏]

1.MongoDB更新

1.1.Update语法格式

db.collection.update(
   <query>, (update的查询条件,类似数据库的where后)
   <update>, (要更新的数据或者对象,类似数据库set后)
   {
     upsert: <boolean>, (如果查询不到数据是否要插入该数据。可选,默认:false)
     multi: <boolean>, (true 更新所有查询到的数据,false 只更新第一条数据。可选,默认:false)
     writeConcern: <document> (抛出异常的等级。可选)
   }
)

备注:
WriteConcern.NONE:没有异常抛出
WriteConcern.NORMAL:仅抛出网络错误异常,没有服务器错误异常
WriteConcern.SAFE:抛出网络错误异常、服务器错误异常;并等待服务器完成写操作。
WriteConcern.MAJORITY: 抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作。
WriteConcern.FSYNC_SAFE: 抛出网络错误异常、服务器错误异常;写操作等待服务器将数据刷新到磁盘。
WriteConcern.JOURNAL_SAFE:抛出网络错误异常、服务器错误异常;写操作等待服务器提交到磁盘的日志文件。
WriteConcern.REPLICAS_SAFE:抛出网络错误异常、服务器错误异常;等待至少2台服务器完成写操作。

1.2.Update()实例

查看数据
> db.col.find().pretty()
{
	"_id" : ObjectId("5cd086464afffe4d17793044"),
	"title" : "test",
	"desc" : "this is test"
}
{
	"_id" : ObjectId("5cd087224afffe4d17793045"),
	"title" : "test",
	"desc" : "this is test"
}
{
	"_id" : ObjectId("5cd087394afffe4d17793046"),
	"title" : "test2",
	"desc" : "this is test2"
}

更新数据
> db.col.update({'title':'test'},{$set:{'title':'test_new'}},{multi:true});
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })

查看数据
> db.col.find().pretty();
{
	"_id" : ObjectId("5cd086464afffe4d17793044"),
	"title" : "test_new",
	"desc" : "this is test"
}
{
	"_id" : ObjectId("5cd087224afffe4d17793045"),
	"title" : "test_new",
	"desc" : "this is test"
}
{
	"_id" : ObjectId("5cd087394afffe4d17793046"),
	"title" : "test2",
	"desc" : "this is test2"
}

1.3.Save语法格式

db.collection.save(
   <document>, (文档数据)
   {
     writeConcern: <document> (异常等级,可选)
   }
)

1.4.Save()实例

> db.col.save({ "_id" : ObjectId("5cd086464afffe4d17793044"), "title" : "test_new_save", "desc" : "this is save" });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.col.find().pretty();
{
	"_id" : ObjectId("5cd086464afffe4d17793044"),
	"title" : "test_new_save",
	"desc" : "this is save"
}
{
	"_id" : ObjectId("5cd087224afffe4d17793045"),
	"title" : "test_new",
	"desc" : "this is test"
}
{
	"_id" : ObjectId("5cd087394afffe4d17793046"),
	"title" : "test2",
	"desc" : "this is test2"
}

2.PHP MongoDB更新数据

1. 更新test.test_col集合中title为test_name 设置为test_new
<?php
$m = new MongoClient();    // 连接到mongodb
$db = $m->test;            // 选择一个数据库
$collection = $db->test_col; // 选择集合
// 更新文档
$collection->update(array("title"=>"test_name"), array('$set'=>array("title"=>"test_new")));
// 显示更新后的文档
$cursor = $collection->find();
// 循环显示文档标题
foreach ($cursor as $document) {
    echo $document["title"] . "\n";
}
?>

输出结果:
test_new

3.PHP7 MongoDB更新数据

1. 更新test.test_col中title为:test_name的第一条数据,更新为:test_new
<?php
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
    ['title' => 'test_name'],
    ['$set' => ['title' => 'test_new']],
    ['multi' => false, 'upsert' => false]
);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");  
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.test_col', $bulk, $writeConcern);
?>
189 人浏览过

发表评论

邮箱地址不会被公开。 必填项已用*标注