Okay, one last thing I'd like to look at before
we move on to some more advanced query topics. Is the
ability to specify a projection document in addition to our query
document for calls to find. Now, what projection does is it
essentially describes the shape we would like documents to take in
the result set. I'm also going to break this out a
little bit here with the query and projection variable, and pass
those to find, instead of directly passing the dictionary that we
constructed to find. I point this out merely because we'll
see this later on in the lesson, and I don't want
anyone to be confused. Now, let's imagine that instead of getting
back all of the documents for our query specifying constraints on
manufacture and class, we actually are only interested in getting the
name of the document back. Well, we could simply print out
the name down here. But another way we can do that
is by specifying a projection as a second parameter to find.
And what that's going to do is say, all of the result
documents that come back, instead of having their full contents, I only
want to see name. No, by default, unless we explicitly say, I
don't want to see the ID field, we'll also get ID. So
that's why I've got ID set to zero here. Okay? So the
way we do this, is, essentially, we specify all the fields for
the documents in this collection. And then specify whether or not we
want them to occur in a result set. Alright? Let's run this.
Alright and here we can see that the documents
we get back simply have the name field in them.
حسنًا، أمر أخير أود أن ألقي نظرة عليه قبل
الانتقال إلى موضوعات الاستعلام الأعلى في المستوى. وهو القدرة
على تحديد مستند projection (عرض) بالإضافة إلى مستند query
استعلام) لاستدعاءات ()find. ويصف العرض في الأساس)
الشكل الذي نرغب في أن تظهر المستندات عليه في
مجموعة النتائج. سأعمد أيضًا إلى إجراء عملية تقسيم
،(هنا باستخدام متغير query (استعلام) ومتغير projection (عرض
وأمرر هذين المتغيرين إلى ()find بدلاً من تمرير القاموس
الذي أنشأناه تمريرًا مباشرًا. أشير إلى ذلك باختصار
لأننا سنتعرف عليه لاحقًا خلال الدرس، ولا أريد أن
يختلط الأمر على أحد. لنفترض أننا بدلاً من أن نعمد إلى استرجاع
جميع المستندات اللازمة للاستعلام الذي ينحصر في
الشركة المصنِّعة والفئة، نهتم فقط باسترجاع
اسم المستند. حسنًا، يمكننا كتابة
الاسم هنا. إلا أن بإمكاننا تنفيذ ذلك
.من خلال تحديد projection (عرض) كمعلمة ثانية لـ ()find
وما أنا بصدد تنفيذه افتراضًا أني أريد الاطلاع على الاسم فقط
بدلاً من استرجاع جميع مستندات النتائج
بكامل محتوياتها. وذلك ما لا يحدث افتراضيًا، حيث ما لم نصرح
،بعدم الرغبة في الاطلاع على حقل id، فسيظهر لنا أيضًا حقل id. ولذلك
لجأت إلى تعيين حقل id هنا إلى 0. Okay? فما نفعله هنا
يتلخص أساسًا في تحديد جميع حقول
المستندات الموجودة في هذه المجموعة. ثم تحديد ما إذا كنا نريد
.وجودها في مجموعة النتائج أم لا. Alright? لنشغِّل هذا
حسنًا، يمكننا هنا ملاحظة أن المستندات التي نعمد إلى
.(استرجاعها لا تتضمن سوى حقل name (الاسم
Eu gostaria de falar de mais uma coisa
antes de seguirmos para tópicos de consulta mais avançados. A
habilidade de especificar um documento de projeção, além do nosso documento
de consulta, é para ser encontrada por chamadas? A projeção
basicamente descreve o formato que queremos que os documentos tenham
no conjunto de resultados. Também vou desmembrar isso um
pouco aqui com as variáveis projection e query, e passá-las
para o find, em vez de passar diretamente o dicionário
que construímos para encontrar. Estou chamando atenção para isso porque
aparecerá mais tarde nesta lição, e não quero
que ninguém se confunda. Agora, imaginemos que, em vez de
recuperar todos os documentos das nossas restrições de especificação de consulta em
manufacturer e class, estejamos interessados só em
recuperar o nome do documento. Bem, poderíamos simplesmente imprimir
o nome aqui embaixo. Mas há outra forma de fazer isso:
especificando uma projeção como um segundo parâmetro a encontrar.
E o que isso fará é dizer todos os documentos de
resultado que retornam. Em vez do conteúdo completo deles, só
quero ver name. Por padrão, a não ser que seja dito explicitamente que
não quero ver o campo de ID, também o obteremos. Então,
é por isso que temos _id definido como zero aqui. Certo? A
forma que fazemos isso é, basicamente, especificando todos os campos dos
documentos neste conjunto. E, depois, especificando se queremos
ou não que ocorram em um conjunto de resultados. OK? Vamos executar isto.
Aqui podemos ver que os documentos
que recuperamos simplesmente têm o campo name neles.
在我们继续进入一些更高级的查询主题之前
我想查看的最后一件事
就是指定我们调用查找的查询文档之外 投影文档的能力
现在 投影是什么 投影本质上描述了
我们希望文档在结果集中的形状
我也在这里通过查询和投影变量
将其分成几块
并通过它们进行查找
而不是直接通过我们构建的字典查找 我指出这一点
仅仅是因为我们将在后面的课程中看到这一点
我不希望任何人感到困惑 现在
让我们想象不针对指定制造和类型约束的查询获取所有文档
我们实际上只对获取文档名称感兴趣
我们可以简单地打印出名称
但是 我们可以采取的另一种方法
就是通过指定投影作为第二个参数进行查找
将要做的是获取所有结果文档
而不是获取其全部内容
我只需要看到名字 在默认情况下 除非我们明确说明
我不需要看到标识字段 我们还将获得标识
所以 这就是我在这里将身份设置为零的原因 明白吗?
我们做的方式 本质上是我们为本集合中的文档指定所有字段
然后指定我们是否希望它们在结果集内发生
好吗?让我们运行它
在这里 我们可以看到
我们获得的文档中只有名字字段