伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: MongoCollection::save

来源:网络转载 浏览:106次 时间:2023-05-08
MongoCollection::setReadPreference » « MongoCollection::remove PHP 手册 函数参考 数据库扩展 针对各数据库系统对应的扩展 Mongo 核心类 MongoCollection

MongoCollection::save

(PECL mongo >=0.9.0)

MongoCollection::save — 保存一个文档到集合

说明

public MongoCollection::save ( array|object $document [, array $options = array() ] ) : mixed

如果对象来自数据库,则更新现有的数据库对象,否则插入对象。

参数

document

要保存的 Array 或 Object。 如果用的是 Object,它不能有 protected 或 private 的属性。

Note:

如果参数不具有 _id 的键或者属性,将会创建并赋值一个新的 MongoId 实例。 关于此行为的更多信息,参见 MongoCollection::insert()。

options

此次保存的选项。

"fsync"

Boolean, defaults to FALSE. Forces the insert to be synced to disk before returning success. If TRUE, an acknowledged insert is implied and will override setting w to 0.

"j"

Boolean, defaults to FALSE. Forces the write operation to block until it is synced to the journal on disk. If TRUE, an acknowledged write is implied and this option will override setting "w" to 0.

Note: If this option is used and journaling is disabled, MongoDB 2.6+ will raise an error and the write will fail; older server versions will simply ignore the option.

"socketTimeoutMS"

This option specifies the time limit, in milliseconds, for socket communication. If the server does not respond within the timeout period, a MongoCursorTimeoutException will be thrown and there will be no way to determine if the server actually handled the write or not. A value of -1 may be specified to block indefinitely. The default value for MongoClient is 30000 (30 seconds).

"w"

See WriteConcerns. The default value for MongoClient is 1.

"wtimeout"

Deprecated alias for "wTimeoutMS".

"wTimeoutMS"

This option specifies the time limit, in milliseconds, for write concern acknowledgement. It is only applicable when "w" is greater than 1, as the timeout pertains to replication. If the write concern is not satisfied within the time limit, a MongoCursorException will be thrown. A value of 0 may be specified to block indefinitely. The default value for MongoClient is 10000 (ten seconds).

"safe"

Deprecated. Please use the WriteConcern w option.

"timeout"

Integer, defaults to MongoCursor::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, a MongoCursorTimeoutException will be thrown.

返回值

如果设置了 w,返回包含此次保存状态的一个 array。 否则,返回一个 boolean,表示数组是否为空(空数组不会被插入)。

错误/异常

如果插入的文档时空的,或者包含零长度的键,将抛出 MongoException。 尝试插入包含 protected 和 private 属性的对象将导致零长度键的错误。

Throws MongoCursorException if the "w" option is set and the write fails.

Throws MongoCursorTimeoutException if the "w" option is set to a value greater than one and the operation takes longer than MongoCursor::$timeout milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in MongoCollection::$wtimeout is milliseconds.

更新日志

版本 说明 1.5.0

增加 "wTimeoutMS" 选项来代替 "wtimeout"。 使用 "wtimeout" 时出现 E_DEPRECATED 错误。

新增 "socketTimeoutMS" 选项来代替 "timeout"。 使用 "timeout" 时出现 E_DEPRECATED 错误。

使用 "safe" 时出现 E_DEPRECATED 错误。

1.2.0 增加 "timeout" 选项。 1.0.11 设置 "safe" 时,当出现 "not master" 错误时主动断开连接。 1.0.9

增加 "fsync" 选项。

1.0.5 增加 options 参数。

范例

Example #1 MongoCollection::save() 例子

<?php

$obj = array('x' => 1);

// 插入 $obj 到 db
$collection->save($obj);
var_dump($obj);

// 增加额外的字段
$obj['foo'] = 'bar';

// $obj 不能被再次插入,导致 duplicate _id 错误
$collection->insert($obj);

// 保存、更新附带新字段的 $obj
$collection->save($obj);

?>

以上例程的输出类似于:

array(2) {
  ["x"]=>
  int(1)
  ["_id"]=>
  object(MongoId)#4 (1) {
    ["$id"]=>
    string(24) "50b6afe544415ed606000000"
  }
}
add a note

User Contributed Notes 1 note

up down -5 cuisdy at gmail dot com6 years ago Same as with method insert(), it is worth noting that creating a reference to $obj will have the same effect as $obj being a reference itself, i.e. no _id field will be added.

<?php

$a = &$obj;

$m = new MongoClient;
$collection = $m->test->phpmanual;

$obj = array('x' => 1);

// Suppose you create a reference for some reason
$a = &$obj;

$collection->save($obj);

var_dump($obj);
// prints: array(1) { ["x"]=> int(1) }
?>
add a note

官方地址:https://www.php.net/manual/en/mongocollection.save.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net