![](/img/trans.png)
[英]How do I move fields of data from all documents in one collection into new documents of a different collection?
[英]How do I list all documents in a collection but show a single property?
如何列出集合中的所有文檔,但如何通過控制台窗口顯示單個屬性?
//Trying to just show name property
db.mycollection.find({name});
您可以通過以下方式設置顯示屬性:
db.mycollection.find({name:name}, {_id:0, name:1});
它只會顯示名稱並隱藏_id
find
mongo命令接受2個參數:
db.collection.find(query, projection)
查詢(可選):
使用查詢運算符指定選擇過濾器。 要返回集合中的所有文檔,請忽略此參數或傳遞空文檔({})。
投影 (可選):
指定要在與查詢過濾器匹配的文檔中返回的字段。 要返回匹配文檔中的所有字段,請省略此參數。 有關詳細信息,請參見投影。
在您的方案中,您必須使用projection參數來指定要返回的字段,如下所示:
db.mycollection.find({}, {_id: 0, name: 1});
由於您不在乎提供條件,因此可以將其保留為空對象。
{_id: 0, name: 1}
表示您不希望結果中包含默認的_id
字段,而只在意/想要name
字段。
db.mycollection.find({},{_id:0,name:1})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.