MongoDB查詢如何只輸出部分欄位內容
例如集合test,欄位有id,url和data三列,現在我們只想輸出id和url欄位,可以按如下方法來查:
> db.test.find( {}, { id: 1, url: 1 } )
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39d"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39e"), "url" : "http://mp.weixin.qq.com/s?__biz=MzIwOTY0NjY5NA==&mid=2247487505&idx=3&sn=8bdfc5b35eaa5eb7d2dbc2ff6247bf06&chksm=9771f518a0067c0e982d834821f9a44b7eea39f60cff8d50886175bbe1c93b27c70a8af9bfd9&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39f"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a0"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a1"), "url" : "http://mp.weixin.qq.com/s?__biz=MzU1NzE3MTEzOQ==&mid=2247486132&idx=7&sn=e031435df54cc5c6dcbdab4136c73222&chksm=fc38a218cb4f2b0e0b581ea5d7ef13f95d29321b385fe51137c8cd102b715ebfb75c284aacb0&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a2"), "url" : "http://mp.weixin.qq.com/s?__biz=MzAxOTMxMTAxOA==&mid=2650490009&idx=2&sn=020a9d674e4f48da10f3ed376bdda67f&chksm=83c7145cb4b09d4a153515ef56755098bccfbca92f7fb379621ffbe4177fc8431181a2d1f2cc&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a3"), "url" : "http://mp.weixin.qq.com/s?__biz=MzI0NTU3NDY1NQ==&mid=2247487947&idx=1&sn=0daadc7945dd56fa7f1c7c61557646ed&chksm=e94d2f5bde3aa64dc6578e1ce9997c5cbe57fc0500222dd9939da33877168afb3297045a50f8&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a4"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a5"), "url" : "http://mp.weixin.qq.com/s?__biz=MzA5ODU0NzQ4OA==&mid=2649879715&idx=1&sn=5bdf0a2ca64b51df037252da73fe9d1c&chksm=888ab6ecbffd3ffa97bc454aef5e490eb8c4d5bd79e7dd61c833107dd907e3b7b6600bb7d111&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a6"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a7"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a8"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a9"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3aa"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ab"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ac"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ad"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ae"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3af"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3b0"), "url" : "" }
Type "it" for more
備註:第一個引數為查詢條件,空代表查詢所有
//如果不想要某個欄位,可以用排除欄位的方法
例如排除data欄位的內容:
> db.test.find( {}, {data: 0 } )
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39d"), "url" : "", "error" : "BSON document too large (25962119 bytes) - the connected server supports BSON document sizes up to 16793598 bytes." }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39e"), "url" : "http://mp.weixin.qq.com/s?__biz=MzIwOTY0NjY5NA==&mid=2247487505&idx=3&sn=8bdfc5b35eaa5eb7d2dbc2ff6247bf06&chksm=9771f518a0067c0e982d834821f9a44b7eea39f60cff8d50886175bbe1c93b27c70a8af9bfd9&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39f"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a0"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a1"), "url" : "http://mp.weixin.qq.com/s?__biz=MzU1NzE3MTEzOQ==&mid=2247486132&idx=7&sn=e031435df54cc5c6dcbdab4136c73222&chksm=fc38a218cb4f2b0e0b581ea5d7ef13f95d29321b385fe51137c8cd102b715ebfb75c284aacb0&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a2"), "url" : "http://mp.weixin.qq.com/s?__biz=MzAxOTMxMTAxOA==&mid=2650490009&idx=2&sn=020a9d674e4f48da10f3ed376bdda67f&chksm=83c7145cb4b09d4a153515ef56755098bccfbca92f7fb379621ffbe4177fc8431181a2d1f2cc&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a3"), "url" : "http://mp.weixin.qq.com/s?__biz=MzI0NTU3NDY1NQ==&mid=2247487947&idx=1&sn=0daadc7945dd56fa7f1c7c61557646ed&chksm=e94d2f5bde3aa64dc6578e1ce9997c5cbe57fc0500222dd9939da33877168afb3297045a50f8&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a4"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a5"), "url" : "http://mp.weixin.qq.com/s?__biz=MzA5ODU0NzQ4OA==&mid=2649879715&idx=1&sn=5bdf0a2ca64b51df037252da73fe9d1c&chksm=888ab6ecbffd3ffa97bc454aef5e490eb8c4d5bd79e7dd61c833107dd907e3b7b6600bb7d111&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a6"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a7"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a8"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a9"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3aa"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ab"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ac"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ad"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ae"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3af"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3b0"), "url" : "" }
Type "it" for more
> db.test.find( {}, { id: 1, url: 1 } )
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39d"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39e"), "url" : "http://mp.weixin.qq.com/s?__biz=MzIwOTY0NjY5NA==&mid=2247487505&idx=3&sn=8bdfc5b35eaa5eb7d2dbc2ff6247bf06&chksm=9771f518a0067c0e982d834821f9a44b7eea39f60cff8d50886175bbe1c93b27c70a8af9bfd9&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39f"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a0"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a1"), "url" : "http://mp.weixin.qq.com/s?__biz=MzU1NzE3MTEzOQ==&mid=2247486132&idx=7&sn=e031435df54cc5c6dcbdab4136c73222&chksm=fc38a218cb4f2b0e0b581ea5d7ef13f95d29321b385fe51137c8cd102b715ebfb75c284aacb0&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a2"), "url" : "http://mp.weixin.qq.com/s?__biz=MzAxOTMxMTAxOA==&mid=2650490009&idx=2&sn=020a9d674e4f48da10f3ed376bdda67f&chksm=83c7145cb4b09d4a153515ef56755098bccfbca92f7fb379621ffbe4177fc8431181a2d1f2cc&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a3"), "url" : "http://mp.weixin.qq.com/s?__biz=MzI0NTU3NDY1NQ==&mid=2247487947&idx=1&sn=0daadc7945dd56fa7f1c7c61557646ed&chksm=e94d2f5bde3aa64dc6578e1ce9997c5cbe57fc0500222dd9939da33877168afb3297045a50f8&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a4"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a5"), "url" : "http://mp.weixin.qq.com/s?__biz=MzA5ODU0NzQ4OA==&mid=2649879715&idx=1&sn=5bdf0a2ca64b51df037252da73fe9d1c&chksm=888ab6ecbffd3ffa97bc454aef5e490eb8c4d5bd79e7dd61c833107dd907e3b7b6600bb7d111&scene=0#rd" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a6"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a7"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a8"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a9"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3aa"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ab"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ac"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ad"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ae"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3af"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3b0"), "url" : "" }
Type "it" for more
備註:第一個引數為查詢條件,空代表查詢所有
//如果不想要某個欄位,可以用排除欄位的方法
例如排除data欄位的內容:
> db.test.find( {}, {data: 0 } )
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39d"), "url" : "", "error" : "BSON document too large (25962119 bytes) - the connected server supports BSON document sizes up to 16793598 bytes." }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39e"), "url" : "http://mp.weixin.qq.com/s?__biz=MzIwOTY0NjY5NA==&mid=2247487505&idx=3&sn=8bdfc5b35eaa5eb7d2dbc2ff6247bf06&chksm=9771f518a0067c0e982d834821f9a44b7eea39f60cff8d50886175bbe1c93b27c70a8af9bfd9&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f39f"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a0"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a1"), "url" : "http://mp.weixin.qq.com/s?__biz=MzU1NzE3MTEzOQ==&mid=2247486132&idx=7&sn=e031435df54cc5c6dcbdab4136c73222&chksm=fc38a218cb4f2b0e0b581ea5d7ef13f95d29321b385fe51137c8cd102b715ebfb75c284aacb0&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a2"), "url" : "http://mp.weixin.qq.com/s?__biz=MzAxOTMxMTAxOA==&mid=2650490009&idx=2&sn=020a9d674e4f48da10f3ed376bdda67f&chksm=83c7145cb4b09d4a153515ef56755098bccfbca92f7fb379621ffbe4177fc8431181a2d1f2cc&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a3"), "url" : "http://mp.weixin.qq.com/s?__biz=MzI0NTU3NDY1NQ==&mid=2247487947&idx=1&sn=0daadc7945dd56fa7f1c7c61557646ed&chksm=e94d2f5bde3aa64dc6578e1ce9997c5cbe57fc0500222dd9939da33877168afb3297045a50f8&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a4"), "url" : "" }
{ "_id" : ObjectId("5ad8a51bb5cec3000b17f3a5"), "url" : "http://mp.weixin.qq.com/s?__biz=MzA5ODU0NzQ4OA==&mid=2649879715&idx=1&sn=5bdf0a2ca64b51df037252da73fe9d1c&chksm=888ab6ecbffd3ffa97bc454aef5e490eb8c4d5bd79e7dd61c833107dd907e3b7b6600bb7d111&scene=0#rd", "web_url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a6"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a7"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a8"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3a9"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3aa"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ab"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ac"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ad"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3ae"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3af"), "url" : "" }
{ "_id" : ObjectId("5ad8a51cb5cec3000b17f3b0"), "url" : "" }
Type "it" for more
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2153135/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在 with 查詢中只查詢個別欄位
- 如何在MySQL中實現替換欄位部分內容MySql
- MongoDB(13)- 查詢操作返回指定的欄位MongoDB
- Laravel 查詢資料庫欄位內容是 Json 陣列時的查詢語句Laravel資料庫JSON陣列
- 如何在Spring Data MongoDB 中儲存和查詢動態欄位SpringMongoDB
- 技術乾貨| MongoDB如何查詢Null或不存在的欄位?MongoDBNull
- vue: el-form只校驗部分欄位VueORM
- mongodb如何改_id欄位?MongoDB
- MongoDB查詢內嵌文件MongoDB
- 揭秘PostgreSQL:如何查詢表欄位名稱SQL
- MySQL Connector/Python 查詢如何返回欄位名MySqlPython
- 使用SQL語言 替換資料庫某欄位內的部分內容SQL資料庫
- Elasticsearch 最佳化查詢中獲取欄位內容的方式,效能提升5倍!Elasticsearch
- 表格輸出內容
- Elasticsearch 複合查詢——多字串多欄位查詢Elasticsearch字串
- Rafy 框架 - 實體支援只更新部分變更的欄位框架
- 查詢oracle欄位預設值Oracle
- ArcGIS對欄位分割查詢操作
- Elasticsearch 單字串多欄位查詢Elasticsearch字串
- SEO如何提高原創內容輸出增量?
- 記如何在預載入中指定查詢的欄位
- 模型聯合查詢返回指定欄位模型
- CYmysql根據json欄位內容作為查詢條件(包括json陣列)檢索資料dvnMySqlJSON陣列
- MongoDB 如何支援類 SQL 查詢MongoDBSQL
- [Elasticsearch] 多欄位搜尋 (二) - 最佳欄位查詢及其調優(轉)Elasticsearch
- MySQL:查詢欄位數量多少對查詢效率的影響MySql
- MySQL如何查詢某個欄位含有字母數字的值MySql
- SQL 如何查詢每個分組都出現的欄位值SQL
- oracle查詢語句查詢增加一列內容Oracle
- 泛微oa流程表單之明細表欄位通過文字輸入內容使另外一個欄位必填
- 查詢資料庫表及表欄位資料庫
- SQL Server 查詢表註釋和欄位SQLServer
- mysql多表多欄位查詢並去重MySql
- 大資料MongoDB之mgo驅動如何對查詢結果進行排序(正序逆序多欄位排序)?大資料MongoDB排序
- mongodb如何查詢最後幾項?MongoDB
- MongoDB更改欄位型別MongoDB型別
- mongodb查詢資料庫中某個欄位中的值包含某個字串的方法MongoDB資料庫字串
- springboot~ConstraintValidatorContext驗證兩個欄位內容相同Spring BootAIContext