【问题标题】:Retrieve first 5 fields from MongoDb collection从 MongoDb 集合中检索前 5 个字段
【发布时间】:2020-12-09 17:33:49
【问题描述】:

如何从MongoDb 数据库集合中检索前 5 个字段?我什至不知道数据库集合的长度。请告诉我从集合中检索前 5 个字段的查询。

这是我在 MERN stack 面试中问我的问题,希望有人能帮助找到答案。

提前致谢。

【问题讨论】:

标签: node.js mongodb express


【解决方案1】:
// 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2018-12-09
    • 2021-09-11
    • 2020-10-02
    • 2019-07-11
    相关资源
    最近更新 更多