简体   繁体   English

MongoDB find()仅从集合中检索一个属性

[英]MongoDB find() which is retrieving only one property from the collection

I have a collection of blog posts where the structure of the records is {title, author, contents} and I want to retrieve all the names of all the authors I am doing this: 我有一个博客帖子集合,其中记录的结构为{title,author,contents},我想检索我正在执行的所有作者的所有姓名:

this.db.collection("ContentText").find("authors");

but my output is undefined Can someone point out the error? 但是我的输出是undefined有人可以指出错误吗?

尝试这个:

this.db.collection("ContentText").distinct("author");

Try this: 尝试这个:

this.db.collection('<collection_name>').find(<queryObject>,{author:1});

This query will return you array of authors with _id, you can exclude _id by 该查询将返回带有_id的作者数组,您可以通过以下方式排除_id

{_id:0,author:1}

For more read this 欲了解更多,请阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM