node.js - 从 MongoDb 集合中检索前 5 个字段
问题描述
如何从MongoDb
数据库集合中检索前 5 个字段?我什至不知道数据库集合的长度。请告诉我从集合中检索前 5 个字段的查询。
这是我在 MERN stack 面试中问我的问题,希望有人能帮助找到答案。
提前致谢。
解决方案
// Define an empty query document.
const query = {};
// Sort in descending (-1) order by length.
const sort = { length: -1 };
const limit = 5;
const cursor = collection.find(query).sort(sort).limit(limit);
await cursor.forEach(console.dir);
推荐阅读
- python - 在 PySpark 上将日期时间转换为日期
- android - 是否可以为 Android 中的 ImageView 或 Button 赋值?
- python - 如何将 python --version stdout 保存到文本文件?
- uwp - 在 UWP NavigationView 中更改后退按钮的前景色
- html - 无法使主标签扩展到显示器中的所有空间
- google-cloud-platform - 如何将请求的响应正文格式化为 Google Cloud Platform API 请求的预测端点
- opengl - 如何防止缩放低于定义(OpenGL)
- javascript - 具有最大正方形的 CSS 网格,没有溢出
- javascript - 改变对象数组中的项目的最有效方法?[VUEX]
- vue.js - 区域设置在切换页面之间不断重置。Nuxt-i18n