node.js - 从MongoDB中的数组中获取随机元素

标签 node.js mongodb aggregation-framework

这是我的数据库结构

{
     "_id" : ObjectId("576155226d1d298c2cc3edca"),
     "questionLibrary" : {
             "technologyName" : "CSS",
             "questions" : [
                     {
                             "correctanswer" : {
                                     "A1" : "CSS1"
                             },
                             "answeroption" : {
                                     "A4" : "CSS1",
                                     "A3" : "CSS1",
                                     "A2" : "CSS1",
                                     "A1" : "CSS1"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS1"
                     },
                     {
                             "question" : "CSS2",
                             "tags" : "CSS",
                             "answeroption" : {
                                     "A1" : "CSS2",
                                     "A2" : "CSS2",
                                     "A3" : "CSS2",
                                     "A4" : "CSS2"
                             },
                             "level" : "Amature",
                             "correctanswer" : {
                                     "A1" : "CSS2"
                             }
                     },
                     {
                             "correctanswer" : {
                                     "A1" : "CSS3"
                             },
                             "answeroption" : {
                                     "A4" : "CSS3",
                                     "A3" : "CSS3",
                                     "A2" : "CSS3",
                                     "A1" : "CSS3"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS3"
                     },
                     {
                             "correctanswer" : {
                                     "A1" : "CSS4"
                             },
                             "answeroption" : {
                                     "A4" : "CSS4",
                                     "A3" : "CSS4",
                                     "A2" : "CSS4",
                                     "A1" : "CSS4"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS4"
                     },
                     {
                             "correctanswer" : {
                                     "A1" : "CSS5"
                             },
                             "answeroption" : {
                                     "A4" : "CSS5",
                                     "A3" : "CSS5",
                                     "A2" : "CSS5",
                                     "A1" : "CSS5"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS5"
                     },
                     {
                             "correctanswer" : {
                                     "A1" : "CSS6"
                             },
                             "answeroption" : {
                                     "A4" : "CSS6",
                                     "A3" : "CSS6",
                                     "A2" : "CSS6",
                                     "A1" : "CSS6"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS6"
                     },
                     {
                             "correctanswer" : {
                                     "A1" : "CSS7"
                             },
                             "answeroption" : {
                                     "A4" : "CSS7",
                                     "A3" : "CSS7",
                                     "A2" : "CSS7",
                                     "A1" : "CSS7"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS7"
                     },
                     {
                             "correctanswer" : {
                                     "A1" : "CSS8"
                             },
                             "answeroption" : {
                                     "A4" : "CSS8",
                                     "A3" : "CSS8",
                                     "A2" : "CSS8",
                                     "A1" : "CSS8"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS8"
                     },
                     {
                             "correctanswer" : {
                                     "A1" : "CSS9"
                             },
                             "answeroption" : {
                                     "A4" : "CSS9",
                                     "A3" : "CSS9",
                                     "A2" : "CSS9",
                                     "A1" : "CSS9"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS9"
                     },
                     {
                             "correctanswer" : {
                                     "A1" : "CSS10"
                             },
                             "answeroption" : {
                                     "A4" : "CSS10",
                                     "A3" : "CSS10",
                                     "A2" : "CSS10",
                                     "A1" : "CSS10"
                             },
                             "level" : "Amature",
                             "tags" : "CSS",
                             "question" : "CSS10"
                     }
             ]
     },
     "__v" : 3
}

每次触发查询时,我都想从问题数组中获取一个随机对象(随机问题)。

我不想一次收集所有对象并在 Node 中操作它们。是否可以编写一个查询,以便每次都返回一个随机对象?

最佳答案

试试这个逻辑

1) 使用 $unwind在数组 "questions"上,如果使用 includeArrayIndex,它将创建带有索引的文档,请参见 examples在展开文档中

2) 展开数组后,传递一个随机数来检索问题

关于node.js - 从MongoDB中的数组中获取随机元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37853003/

相关文章:

node.js - 使用 Typescript 扩展快速响应对象的正确方法

node.js - 从 Node.js 中的二进制数据创建 XLS 文件

mongodb - MongoDB的数据库管理工具

javascript - 如何在 Mongoose 中更新/插入文档?

node.js - mongoDB 中的多个 $group

javascript - Node.js - 阻止用户打开包含数据库详细信息的 .js 文件?

node.js - Angular 8 未在 nodejs 版本 8.10.0 上运行

python - Mongoengine + Django : strange errors with fields during rendering

java - 为什么 CustomProjectAggregationOperation 在 SpringMongoTemplate 中不起作用

mongodb - 根据计算值对集合进行排序