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 锟侥硷拷锟斤拷锟斤拷锟斤拷锟皆o拷锟斤拷锟结创锟斤拷锟斤拷锟斤拷值一锟斤拷锟铰碉拷 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锟斤拷锟斤拷示锟斤拷锟斤拷锟角凤拷为锟秸o拷锟斤拷锟斤拷锟介不锟结被锟斤拷锟诫)锟斤拷

锟斤拷锟斤拷锟届常

锟斤拷锟斤拷锟斤拷锟斤拷锟侥碉拷时锟秸的o拷锟斤拷锟竭帮拷锟斤拷锟姐长锟饺的硷拷锟斤拷锟斤拷锟阶筹拷 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"
  }
}