【问题标题】:Get random element from array in MongoDB从MongoDB中的数组中获取随机元素
【发布时间】:2016-06-17 18:15:50
【问题描述】:

这是我的数据库结构

{
     "_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
}

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

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

【问题讨论】:

  • 建议通过将数据存储在单独的集合中(例如 questions)来重构您的架构以非规范化 questions 数组。然后,您可以轻松地使用聚合管道的 $sample 运算符以及标签上的过滤器来绘制示例问题文档。这样查询要容易得多。一个示例查询是db.questions.aggregate([ { "$sample": { "size": 1 } } ])

标签: node.js mongodb aggregation-framework


【解决方案1】:

试试这个逻辑

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

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

【讨论】:

    【解决方案2】:

    您需要使用$unwind 运算符对“问题”数组进行反规范化。从那里您可以使用$sample 管道运算符返回一个随机文档(随机问题)。

    db.collection.aggregate(
        [ 
            { "$unwind": "$questionLibrary.questions" }, 
            { "$sample": { "size": 1 } } 
        ]
    )
    

    但我建议您更改文档结构并将“问题”保存到它自己的集合中,因为$unwind 可以产生非常大的结果。

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 2018-05-03
      • 2019-12-10
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多