1"use strict";(globalThis.webpackChunkwww||=[]).push([[5643],{41654(e,t,n){n.r(t),n.d(t,{assets:()=>d,contentTitle:()=>r,default:()=>p,frontMatter:()=>c,metadata:()=>s,toc:()=>l});const s=JSON.parse('{"id":"data-services/elasticsearch/mutations","title":"Mutations","description":"Index, partially update, and delete Elasticsearch documents using ElasticCollectionService \u2014 with options for bulk updates, document replacement, and filtered deletes.","source":"@site/docs/data-services/elasticsearch/mutations.mdx","sourceDirName":"data-services/elasticsearch","slug":"/data-services/elasticsearch/mutations","permalink":"/docs/data-services/elasticsearch/mutations","draft":false,"unlisted":false,"tags":[],"version":"current","sidebarPosition":4,"frontMatter":{"title":"Mutations","sidebar_position":4,"description":"Index, partially update, and delete Elasticsearch documents using ElasticCollectionService \u2014 with options for bulk updates, document replacement, and filtered deletes."},"sidebar":"docsSidebar","previous":{"title":"Querying","permalink":"/docs/data-services/elasticsearch/querying"},"next":{"title":"Lifecycle Hooks","permalink":"/docs/data-services/elasticsearch/lifecycle-hooks"}}');var i=n(74848),a=n(28453);const c={title:"Mutations",sidebar_position:4,description:"Index, partially update, and delete Elasticsearch documents using ElasticCollectionService \u2014 with options for bulk updates, document replacement, and filtered deletes."},r="Mutations",d={},l=[{value:"create",id:"create",level:2},{value:"update",id:"update",level:2},{value:"updateMany",id:"updatemany",level:2},{value:"delete",id:"delete",level:2},{value:"deleteMany",id:"deletemany",level:2},{value:"Full API reference",id:"full-api-reference",level:2}];function o(e){const t={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",header:"header",hr:"hr",p:"p",pre:"pre",strong:"strong",...(0,a.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(t.header,{children:(0,i.jsx)(t.h1,{id:"mutations",children:"Mutations"})}),"\n",(0,i.jsx)(t.admonition,{type:"info",children:(0,i.jsxs)(t.p,{children:["Unlike MongoDB and SQL services, ",(0,i.jsx)(t.code,{children:"create()"})," and ",(0,i.jsx)(t.code,{children:"update()"})," in ",(0,i.jsx)(t.code,{children:"ElasticCollectionService"})," ",(0,i.jsx)(t.strong,{children:"do not return the document"}),". They return Elasticsearch response objects. Fetch the document separately with ",(0,i.jsx)(t.code,{children:"get()"})," or ",(0,i.jsx)(t.code,{children:"findById()"})," if needed."]})}),"\n",(0,i.jsx)(t.hr,{}),"\n",(0,i.jsx)(t.h2,{id:"create",children:"create"}),"\n",(0,i.jsxs)(t.p,{children:["Indexes a new document. Auto-generates an ID if ",(0,i.jsx)(t.code,{children:"_id"})," is not included in the input."]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-typescript",children:"const response = await svc.create({\n name: 'Wireless Headphones',\n category: 'electronics',\n price: 149.99,\n inStock: true,\n});\n// response.result === 'created'\n// response._id \u2014 the generated or provided ID\n"})}),"\n",(0,i.jsxs)(t.p,{children:["Use ",(0,i.jsx)(t.code,{children:"replaceIfExists"})," to overwrite an existing document with the same ID:"]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-typescript",children:"await svc.create(input, { replaceIfExists: true });\n"})}),"\n",(0,i.jsx)(t.hr,{}),"\n",(0,i.jsx)(t.h2,{id:"update",children:"update"}),"\n",(0,i.jsx)(t.p,{children:"Applies a partial update to the document with the given ID. Only the fields you include are changed; everything else is untouched."}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-typescript",children:"const response = await svc.update(id, { price: 129.99, inStock: false });\n// response.updated === 1 (number of updated documents)\n// response.total === 1\n"})}),"\n",(0,i.jsx)(t.hr,{}),"\n",(0,i.jsx)(t.h2,{id:"updatemany",children:"updateMany"}),"\n",(0,i.jsx)(t.p,{children:"Applies a partial update to all documents matching the filter."}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-typescript",children:"const response = await svc.updateMany(\n { inStock: false },\n { filter: 'price < 10' },\n);\n// response.updated \u2014 number of updated documents\n"})}),"\n",(0,i.jsx)(t.hr,{}),"\n",(0,i.jsx)(t.h2,{id:"delete",children:"delete"}),"\n",(0,i.jsx)(t.p,{children:"Deletes the document with the given ID."}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-typescript",children:"const response = await svc.delete(id);\n// response.deleted === 1 (or 0 if not found)\n"})}),"\n",(0,i.jsx)(t.hr,{}),"\n",(0,i.jsx)(t.h2,{id:"deletemany",children:"deleteMany"}),"\n",(0,i.jsx)(t.p,{children:"Deletes all documents matching the filter."}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-typescript",children:"const response = await svc.deleteMany({\n filter: 'inStock = false',\n});\n// response.deleted \u2014 number of deleted documents\n"})}),"\n",(0,i.jsx)(t.hr,{}),"\n",(0,i.jsx)(t.h2,{id:"full-api-reference",children:"Full API reference"}),"\n",(0,i.jsxs)(t.p,{children:["\u2192 ",(0,i.jsx)(t.a,{href:"/docs/reference/elastic/elastic-collection-service",children:(0,i.jsx)(t.code,{children:"ElasticCollectionService"})})]})]})}function p(e={}){const{wrapper:t}={...(0,a.R)(),...e.components};return t?(0,i.jsx)(t,{...e,children:(0,i.jsx)(o,{...e})}):o(e)}},28453(e,t,n){n.d(t,{R:()=>c,x:()=>r});var s=n(96540);const i={},a=s.createContext(i);function c(e){const t=s.useContext(a);return s.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function r(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:c(e.components),s.createElement(a.Provider,{value:t},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.