在MongoDB中如何选择满足一些条件的随机文档。

huangapple go评论79阅读模式
英文:

how to select random documents with some conditions fulfilled in MongoDB

问题

你好,以下是翻译好的部分:

"Basically I have documents in which I have one field called 'Difficulty Level,' and the value of this field is between 1 to 10 for each document.

So, I have to select random 10 or 20 documents so that in randomly selected documents, at least 1 document should be there for each difficulty level, i.e., from 1 to 10. means there should at least one document with 'Difficulty level' : 1, 'Difficulty level' : 2, 'Difficulty level' : 3 ............. 'Difficulty level' : 10.

So, How can I select documents randomly with this condition fulfilled?

Thanks

I tried $rand operator for selecting random documents but can't get a solution for that condition."

英文:

Basically I have documents in which I have on field called "Difficulty Level" and value of this filed is between 1 to 10 for each documents.

So, I have to select random 10 or 20 documents so that in randomly selected documents , atleast 1 document should be there for each difficulty level i.e. from 1 to 10. means there should atlease one document with "Difficulty level" : 1 ,"Difficulty level" : 2 ,"Difficulty level" : 3 ............."Difficulty level" : 10.

So, How can I select documents randomly with this condition fulfilled ?

Thanks

I tried $rand operator for selecting random documents but can't getting solution for that condition.

答案1

得分: 0

以下是您要求的翻译:

如果我理解正确,您可以尝试类似以下的操作:

这里的目标是创建一个查询 类似这个示例

这个查询使用 $sample 获取两个随机元素,一个是 level1,另一个是 level2。并且使用 $facet 可以获得多个结果。

db.collection.aggregate([
  {
    "$facet": {
      "difficulty_level_1": [
        {
          "$match": { "difficulty_level": 1 } },
        { "$sample": { "size": 1 } }
      ],
      "difficulty_level_2": [
        { "$match": { "difficulty_level": 2 } },
        { "$sample": { "size": 1 } } 
      ]
    }
  }
])

所以关键是以动态方式执行此查询。这样,您可以使用 JavaScript 创建查询对象并将其传递给 MongoDB 调用。

const random = Math.floor((Math.random()*10)+1) // 或其他方式获取随机数

let query = {"$facet":{}}
for(let i = 1; i <= random; i++){
  const difficulty_level = `difficulty_level_${i}`
  query["$facet"][difficulty_level] = [
      { $match: { difficulty_level: i }},
      { $sample: { size: 1 }}
    ]
}
console.log(query) // 此输出可以在 mongoplayground 中使用,并且有效!
// 要使用查询,您可以使用类似这样的方式(或者您调用数据库的其他方式)
this.db.aggregate([query])

请注意,代码部分已经忽略,只提供翻译。

英文:

If I've understood correctly you can try something like this:

The goal here is to create a query like this example

This query gets two random elements using $sample, one for level1 and another for level2. And using $facet you can get multiple results.

db.collection.aggregate([
  {
    &quot;$facet&quot;: {
      &quot;difficulty_level_1&quot;: [
        {
          &quot;$match&quot;: { &quot;difficulty_level&quot;: 1 } },
        { &quot;$sample&quot;: { &quot;size&quot;: 1 } }
      ],
      &quot;difficulty_level_2&quot;: [
        { &quot;$match&quot;: { &quot;difficulty_level&quot;: 2 } },
        { &quot;$sample&quot;: { &quot;size&quot;: 1 } } 
      ]
    }
  }
])

So the point is to do this query in a dynamic way. So you can use JS to create the object query an pass it to the mongo call.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const random = Math.floor((Math.random()*10)+1) // Or wathever to get the random number

let query = {&quot;$facet&quot;:{}}
for(let i = 1 ; i &lt;= random; i++){
  const difficulty_level = `difficulty_level_${i}`
  query[&quot;$facet&quot;][difficulty_level] = [
      { $match: { difficulty_level: i }},
      { $sample: { size: 1 }}
    ]
}
console.log(query) // This output can be used in mongoplayground and it works!
// To use the query you can use somethiing like this (or other way you call the DB) 
this.db.aggregate([query])

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月5日 06:07:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75011865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定