<?xml version="1.0"?>
<page xmlns="http://projectmallard.org/1.0/"
      type="topic"
      style="function"
      xmlns:api="http://projectmallard.org/experimental/api/"
      xmlns:ui="http://projectmallard.org/experimental/ui/"
      id="mongoc_collection_find_and_modify">
  <info><link type="guide" xref="mongoc_collection_t" group="function"/></info>
  <title>mongoc_collection_find_and_modify()</title>

  <section id="synopsis">
    <title>Synopsis</title>
    <synopsis><code mime="text/x-csrc"><![CDATA[bool
mongoc_collection_find_and_modify (mongoc_collection_t *collection,
                                   const bson_t        *query,
                                   const bson_t        *sort,
                                   const bson_t        *update,
                                   const bson_t        *fields,
                                   bool                 _remove,
                                   bool                 upsert,
                                   bool                 _new,
                                   bson_t              *reply,
                                   bson_error_t        *error);
]]></code></synopsis>
  </section>

  <section id="parameters">
    <title>Parameters</title>
    <table>
      <tr><td><p>collection</p></td><td><p>A <code xref="mongoc_collection_t">mongoc_collection_t</code>.</p></td></tr>
      <tr><td><p>query</p></td><td><p>A <code xref="bson:bson_t">bson_t</code> containing the query to locate target document(s).</p></td></tr>
      <tr><td><p>sort</p></td><td><p>A <code xref="bson:bson_t">bson_t</code> containing the sort order for <code>query</code>.</p></td></tr>
      <tr><td><p>update</p></td><td><p>A <code xref="bson:bson_t">bson_t</code> containing an update spec.</p></td></tr>
      <tr><td><p>fields</p></td><td><p>An optional <code xref="bson:bson_t">bson_t</code> containing the fields to return or <code>NULL</code>.</p></td></tr>
      <tr><td><p>_remove</p></td><td><p>If the matching documents should be removed.</p></td></tr>
      <tr><td><p>upsert</p></td><td><p>If an upsert should be performed.</p></td></tr>
      <tr><td><p>_new</p></td><td><p>If the new version of the document should be returned.</p></td></tr>
      <tr><td><p>reply</p></td><td><p>An optional location for a <code xref="bson:bson_t">bson_t</code> that will be initialized with the result or <code>NULL</code>.</p></td></tr>
      <tr><td><p>error</p></td><td><p>An optional location for a <code xref="bson:bson_error_t">bson_error_t</code> or <code>NULL</code>.</p></td></tr>
    </table>
  </section>

  <section id="description">
    <title>Description</title>
    <p>Update and return an object.</p>
    <p>This is a thin wrapper around the <code>findAndModify</code> command. Either <code>update</code> or <code>_remove</code> arguments are required.</p>
    <note style="tip"><p>As of MongoDB 3.2 and mongoc 1.3.0, the <link xref="mongoc_write_concern_t">mongoc_write_concern_t</link> specified on the <link xref="mongoc_collection_t">mongoc_collection_t</link> will be used, if any.</p></note>
  </section>


  <section id="errors">
    <title>Errors</title>
    <p>Errors are propagated via the <code>error</code> parameter.</p>
  </section>

  <section id="return">
    <title>Returns</title>
    <p>Returns either the document before or after modification based on the <code>_new</code> parameter.</p>
  </section>

  <section id="example">
    <title>Example</title>
    <listing>
      <title><file>find-and-modify.c</file></title>
      <code mime="text/x-csrc"><include parse="text" href="../examples/find-and-modify.c" xmlns="http://www.w3.org/2001/XInclude" /></code>
    </listing>
  </section>

</page>
