英文:
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([
{
"$facet": {
"difficulty_level_1": [
{
"$match": { "difficulty_level": 1 } },
{ "$sample": { "size": 1 } }
],
"difficulty_level_2": [
{ "$match": { "difficulty_level": 2 } },
{ "$sample": { "size": 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 = {"$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) // 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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论