Php/docs/mongocollection.save
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
. If journaling is enabled, it works exactly like"j"
. If journaling is not enabled, the write operation blocks until it is synced to database files on disk. Iftrue
, an acknowledged insert is implied and this option will override setting"w"
to0
.Note: If journaling is enabled, users are strongly encouraged to use the
"j"
option instead of"fsync"
. Do not use"fsync"
and"j"
simultaneously, as that will result in an error."j"
Boolean, defaults to
false
. Forces the write operation to block until it is synced to the journal on disk. Iftrue
, an acknowledged write is implied and this option will override setting"w"
to0
.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 is30000
(30 seconds)."w"
See Write Concerns. 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 than1
, as the timeout pertains to replication. If the write concern is not satisfied within the time limit, a MongoCursorException will be thrown. A value of0
may be specified to block indefinitely. The default value for MongoClient is10000
(ten seconds)."safe"
Deprecated. Please use the write concern
"w"
option."timeout"
Deprecated alias for
"socketTimeoutMS"
.
返回值
如果设置了 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 |
增加 新增 使用 |
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" } }