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"
  }
}