diff --git a/404.html b/404.html index 2ef751b..d69f81e 100644 --- a/404.html +++ b/404.html @@ -7,7 +7,7 @@ - +
diff --git a/assets/js/06213a2c.8d8d3af8.js b/assets/js/06213a2c.8d8d3af8.js deleted file mode 100644 index e97f080..0000000 --- a/assets/js/06213a2c.8d8d3af8.js +++ /dev/null @@ -1 +0,0 @@ -"use strict";(self.webpackChunkaggregation_pipeline_workshop=self.webpackChunkaggregation_pipeline_workshop||[]).push([[995],{2637:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>d,contentTitle:()=>o,default:()=>p,frontMatter:()=>s,metadata:()=>c,toc:()=>u});var l=t(4848),r=t(8453),i=t(9489),a=t(7227);const s={},o="\ud83d\udc50 $project",c={id:"simple-queries/project",title:"\ud83d\udc50 $project",description:"Including fields in a projection",source:"@site/docs/30-simple-queries/3-project.mdx",sourceDirName:"30-simple-queries",slug:"/simple-queries/project",permalink:"/aggregation-pipeline-lab/docs/simple-queries/project",draft:!1,unlisted:!1,editUrl:"https://github.com/mongodb-developer/aggregation-pipeline-lab/blob/main/docs/30-simple-queries/3-project.mdx",tags:[],version:"current",sidebarPosition:3,frontMatter:{},sidebar:"tutorialSidebar",previous:{title:"\ud83d\udc50 $match",permalink:"/aggregation-pipeline-lab/docs/simple-queries/match"},next:{title:"\ud83d\udc50 $limit",permalink:"/aggregation-pipeline-lab/docs/simple-queries/limiting-results"}},d={},u=[{value:"Including fields in a projection",id:"including-fields-in-a-projection",level:2},{value:"Excluding fields",id:"excluding-fields",level:2},{value:"Including and excluding fields",id:"including-and-excluding-fields",level:2}];function h(e){const n={admonition:"admonition",code:"code",h1:"h1",h2:"h2",li:"li",p:"p",pre:"pre",ul:"ul",...(0,r.R)(),...e.components},{Details:t}=n;return t||function(e,n){throw new Error("Expected "+(n?"component":"object")+" `"+e+"` to be defined: you likely forgot to import, pass, or provide it.")}("Details",!0),(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(n.h1,{id:"-project",children:"\ud83d\udc50 $project"}),"\n",(0,l.jsx)(n.h2,{id:"including-fields-in-a-projection",children:"Including fields in a projection"}),"\n",(0,l.jsxs)(n.p,{children:["A document from the ",(0,l.jsx)(n.code,{children:"books"})," collection looks like:"]}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"{\n _id: '0395623650',\n title: 'Platero y yo / Platero and I (Spanish-English Bilingual Edition) (English and Spanish Edition)',\n authors: [\n {\n _id: '64cc2db4830ba29148db5180',\n name: 'Juan Ram\xf3n Jim\xe9nez'\n }\n ],\n pages: 64,\n year: 1994,\n synopsis: 'Selections from a classic of world literature present a picture of life in the town of Moguer, in Andalusia, Spain.\\n' +\n '\\n' +\n '\\n' +\n ' Presents a picture of life in the town of Moguer, in Andalusia, Spain, as seen through the eyes of a wandering poet and his faithful donkey.\\n',\n cover: 'https://images.isbndb.com/covers/36/57/9780395623657.jpg',\n attributes: [\n {\n key: 'edition',\n value: 'F First Edition Thus'\n },\n {\n key: 'dimensions',\n value: 'Height: 10.499979 Inches, Length: 6.999986 Inches, Weight: 2.0502990366 Pounds, Width: 0.12499975 Inches'\n },\n {\n key: 'isbn13',\n value: '9780395623657'\n },\n {\n key: 'msrp',\n value: 1.99\n },\n {\n key: 'isbn',\n value: '0395623650'\n },\n {\n key: 'isbn10',\n value: '0395623650'\n }\n ],\n totalInventory: 1,\n available: 1,\n binding: 'Hardcover',\n language: 'en',\n longTitle: 'Platero y yo / Platero and I (Spanish-English Bilingual Edition) (English and Spanish Edition)',\n publisher: 'Clarion Books'\n}\n"})}),"\n",(0,l.jsxs)(n.p,{children:["If we're interested just in the titles, we can use ",(0,l.jsx)(n.code,{children:"$project"})," to select just the fields we're interested in. As an example, to get just the book's title and year we'll write:"]}),"\n",(0,l.jsxs)(i.A,{groupId:"aggregations",children:[(0,l.jsx)(a.A,{value:"atlas",label:"Atlas UI",children:(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"[\n {\n $project: {title: 1, year: 1}\n }\n]\n"})})}),(0,l.jsx)(a.A,{value:"mongodb-shell",label:"MongoDB Shell",children:(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"db.books.aggregate([{$project: {title: 1, year: 1}}])\n"})})})]}),"\n",(0,l.jsxs)(n.ul,{children:["\n",(0,l.jsx)(n.li,{children:'1 means "show that field"'}),"\n",(0,l.jsx)(n.li,{children:'0 means "hide that field"'}),"\n",(0,l.jsxs)(n.li,{children:["the primary key ",(0,l.jsx)(n.code,{children:"_id"})," field is shown by default"]}),"\n"]}),"\n",(0,l.jsxs)(n.p,{children:["So we can exclude fields and show all fields except ",(0,l.jsx)(n.code,{children:"attributes"})," using:"]}),"\n",(0,l.jsxs)(i.A,{groupId:"aggregations",children:[(0,l.jsx)(a.A,{value:"atlas",label:"Atlas UI",children:(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"[\n {\n $project: {attributes: 0}\n }\n]\n"})})}),(0,l.jsx)(a.A,{value:"mongodb-shell",label:"MongoDB Shell",children:(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"db.books.aggregate([{$project: {attributes: 0}}])\n"})})})]}),"\n",(0,l.jsxs)(n.p,{children:["\ud83d\udc50 Show only ",(0,l.jsx)(n.code,{children:"title"})," and ",(0,l.jsx)(n.code,{children:"cover"}),"."]}),"\n",(0,l.jsxs)(t,{children:[(0,l.jsx)("summary",{children:"Answer"}),(0,l.jsx)("div",{children:(0,l.jsxs)(i.A,{groupId:"aggregations",children:[(0,l.jsx)(a.A,{value:"atlas",label:"Atlas UI",children:(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"[\n {\n $project: {title: 1, cover: 1}\n }\n]\n"})})}),(0,l.jsx)(a.A,{value:"mongodb-shell",label:"MongoDB Shell",children:(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"db.books.aggregate([{$project: {title: 1, cover: 1}}])\n"})})})]})})]}),"\n",(0,l.jsx)(n.h2,{id:"excluding-fields",children:"Excluding fields"}),"\n",(0,l.jsxs)(n.p,{children:["\ud83d\udc50 Exclude the ",(0,l.jsx)(n.code,{children:"cover"}),", ",(0,l.jsx)(n.code,{children:"attributes"})," and ",(0,l.jsx)(n.code,{children:"_id"})," fields from the result."]}),"\n",(0,l.jsxs)(t,{children:[(0,l.jsx)("summary",{children:"Answer"}),(0,l.jsx)("div",{children:(0,l.jsxs)(i.A,{groupId:"aggregations",children:[(0,l.jsx)(a.A,{value:"atlas",label:"Atlas UI",children:(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"[\n {\n $project: {_id: 0, attributes: 0, cover: 0}\n }\n]\n"})})}),(0,l.jsx)(a.A,{value:"mongodb-shell",label:"MongoDB Shell",children:(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"db.books.aggregate([{$project: {_id: 0, attributes: 0, cover: 0}}])\n"})})})]})})]}),"\n",(0,l.jsx)(n.h2,{id:"including-and-excluding-fields",children:"Including and excluding fields"}),"\n",(0,l.jsx)(n.admonition,{type:"danger",children:(0,l.jsx)(n.p,{children:"You can't include and exclude fields in the same projection: if you start including, you must keep including and vice versa."})}),"\n",(0,l.jsx)(n.p,{children:"For instance, this will fail:"}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"db.books.aggregate([{$project: {title: 1, attributes: 0}}])\n\nMongoServerError: Invalid $project :: caused by :: Cannot do exclusion on field attributes in inclusion projection\n"})}),"\n",(0,l.jsx)(n.admonition,{type:"info",children:(0,l.jsxs)(n.p,{children:["The only exception to this rule is the ",(0,l.jsx)(n.code,{children:"_id"})," field, which we can exclude in an inclusion projection."]})}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-js",children:"db.books.aggregate([{$project: {title: 1, _id: 0}}])\n"})})]})}function p(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,l.jsx)(n,{...e,children:(0,l.jsx)(h,{...e})}):h(e)}},7227:(e,n,t)=>{t.d(n,{A:()=>a});t(6540);var l=t(8215);const r={tabItem:"tabItem_Ymn6"};var i=t(4848);function a(e){let{children:n,hidden:t,className:a}=e;return(0,i.jsx)("div",{role:"tabpanel",className:(0,l.A)(r.tabItem,a),hidden:t,children:n})}},9489:(e,n,t)=>{t.d(n,{A:()=>w});var l=t(6540),r=t(8215),i=t(4245),a=t(6347),s=t(6494),o=t(2814),c=t(5167),d=t(1269);function u(e){return l.Children.toArray(e).filter((e=>"\n"!==e)).map((e=>{if(!e||(0,l.isValidElement)(e)&&function(e){const{props:n}=e;return!!n&&"object"==typeof n&&"value"in n}(e))return e;throw new Error(`Docusaurus error: BadA document from the books
collection looks like:
{
_id: '0395623650',
title: 'Platero y yo / Platero and I (Spanish-English Bilingual Edition) (English and Spanish Edition)',
authors: [
{
_id: '64cc2db4830ba29148db5180',
name: 'Juan Ramón Jiménez'
}
],
pages: 64,
year: 1994,
synopsis: 'Selections from a classic of world literature present a picture of life in the town of Moguer, in Andalusia, Spain.\n' +
'\n' +
'\n' +
' Presents a picture of life in the town of Moguer, in Andalusia, Spain, as seen through the eyes of a wandering poet and his faithful donkey.\n',
cover: 'https://images.isbndb.com/covers/36/57/9780395623657.jpg',
attributes: [
{
key: 'edition',
value: 'F First Edition Thus'
},
{
key: 'dimensions',
value: 'Height: 10.499979 Inches, Length: 6.999986 Inches, Weight: 2.0502990366 Pounds, Width: 0.12499975 Inches'
},
{
key: 'isbn13',
value: '9780395623657'
},
{
key: 'msrp',
value: 1.99
},
{
key: 'isbn',
value: '0395623650'
},
{
key: 'isbn10',
value: '0395623650'
}
],
totalInventory: 1,
available: 1,
binding: 'Hardcover',
language: 'en',
longTitle: 'Platero y yo / Platero and I (Spanish-English Bilingual Edition) (English and Spanish Edition)',
publisher: 'Clarion Books'
}
If we're interested just in the titles, we can use $project
to select just the fields we're interested in. As an example, to get just the book's title and year we'll write:
If we're interested in the titles, we can use $project
to select just the fields we're interested in. As an example, to get just the book's title and year, we'll write:
[
{
$project: {title: 1, year: 1}
}
]
db.books.aggregate([{$project: {title: 1, year: 1}}])
_id
field is shown by default_id
field is shown by default.So we can exclude fields and show all fields except attributes
using:
[
{
$project: {attributes: 0}
}
]
db.books.aggregate([{$project: {attributes: 0}}])
👐 Show only title
and cover
.
[
{
$project: {title: 1, cover: 1}
}
]
db.books.aggregate([{$project: {title: 1, cover: 1}}])
👐 Exclude the cover
, attributes
and _id
fields from the result.
👐 Exclude the cover
, attributes
, and _id
fields from the result.
[
{
$project: {_id: 0, attributes: 0, cover: 0}
}
]
db.books.aggregate([{$project: {_id: 0, attributes: 0, cover: 0}}])
You can't include and exclude fields in the same projection: if you start including, you must keep including and vice versa.
You can't include and exclude fields in the same projection. If you start including, you must keep including, and vice versa.
For instance, this will fail:
db.books.aggregate([{$project: {title: 1, attributes: 0}}])
MongoServerError: Invalid $project :: caused by :: Cannot do exclusion on field attributes in inclusion projection
The only exception to this rule is the _id
field, which we can exclude in an inclusion projection.