Amazon web services 使用$sample从Amazon DocumentDB检索随机文档
为了支持应用程序功能,我需要从AmazonDocumentDB中的集合中检索单个文档,而不是每次都检索相同的文档 声明$sample聚合阶段可用于使用伪随机游标选择多个文档。我在本地MongoDB实例上尝试过这个方法,它确实返回了一个随机选择的文档,这正是我所需要的Amazon web services 使用$sample从Amazon DocumentDB检索随机文档,amazon-web-services,mongodb-query,nosql,nosql-aggregation,aws-documentdb,Amazon Web Services,Mongodb Query,Nosql,Nosql Aggregation,Aws Documentdb,为了支持应用程序功能,我需要从AmazonDocumentDB中的集合中检索单个文档,而不是每次都检索相同的文档 声明$sample聚合阶段可用于使用伪随机游标选择多个文档。我在本地MongoDB实例上尝试过这个方法,它确实返回了一个随机选择的文档,这正是我所需要的 db.benchmark.aggregate([ { $sample: { size: 1}} ]) 但是,当我尝试在AmazonDocumentDB上使用相同的查询时,它不会返回随机记录,而是一致地返回集合中的第一条记录
db.benchmark.aggregate([
{ $sample: { size: 1}}
])
但是,当我尝试在AmazonDocumentDB上使用相同的查询时,它不会返回随机记录,而是一致地返回集合中的第一条记录。这似乎不是很有用,因为它的功能与limit相同。说明DocumentDB支持$sample阶段,但没有提供有关其实现的进一步信息
有没有办法让DocumentDB使用$sample aggregation stage操作符选择一个随机记录
例如:
db.temp.insertMany([
{ "_id": 1, "temperature" : 97.5, "humidity": 0.61, "timestamp" : new Date() },
{ "_id": 2, "temperature" : 97.2, "humidity": 0.60, "timestamp" : new Date() },
{ "_id": 3, "temperature" : 97.4, "humidity": 0.61, "timestamp" : new Date() },
{ "_id": 4, "temperature" : 97.9, "humidity": 0.61, "timestamp" : new Date() },
{ "_id": 5, "temperature" : 97.6, "humidity": 0.61, "timestamp" : new Date() },
{ "_id": 6, "temperature" : 97.5, "humidity": 0.62, "timestamp" : new Date() },
{ "_id": 7, "temperature" : 97.2, "humidity": 0.62, "timestamp" : new Date() },
{ "_id": 8, "temperature" : 97.1, "humidity": 0.63, "timestamp" : new Date() },
{ "_id": 9, "temperature" : 96.9, "humidity": 0.62, "timestamp" : new Date() },
{ "_id": 10, "temperature" : 97.4, "humidity": 0.63, "timestamp" : new Date()}
])
db.temp.aggregate(
[ { $sample: { size: 1 } } ]
)
db.temp.aggregate(
[ { $sample: { size: 1 } } ]
)
Windows上的MongoDB 4.4.3
[
{
"_id": 3,
"humidity": 0.61,
"temperature": 97.4,
"timestamp": {"$date": "2021-04-13T22:00:09.361Z"}
}
]
[
{
"_id": 8,
"humidity": 0.63,
"temperature": 97.1,
"timestamp": {"$date": "2021-04-13T22:00:09.361Z"}
}
]
DocumentDB 4.0.0
[
{
"_id": 1,
"humidity": 0.61,
"temperature": 97.5,
"timestamp": {"$date": "2021-04-13T22:00:45.628Z"}
}
]
[
{
"_id": 1,
"humidity": 0.61,
"temperature": 97.5,
"timestamp": {"$date": "2021-04-13T22:00:45.628Z"}
}
]
您可以尝试在此处创建自己的逻辑
在DocumentDB中选择一组随机记录不会像在MongoDB中那样使用$sample操作符 DocumentDB的当前实现将只选择从集合的静态子集中随机选择的记录开始的连续记录系列。静态子集的大小与整个集合的大小有关,并且可能与磁盘上记录的分组方式有关。对于足够小的集合(大约<50条记录),静态子集可能只包含一个点,使用$sample运算符的查询每次都将返回相同的记录序列 要获得MongoDB和DocumentDB之间的兼容行为,请使用结合skip和limit的管道:
db.temp.aggregate([
{
$skip : db.temp.countDocuments() * Math.random()
},
{
$limit: 1
}
]);
根据他们2019年的博客,这似乎与预期的效果一致。你能提供一些关于
基准测试中所有文档的更多内容吗?
?感谢@Nagaraj链接。这证实了DocumentDB的$sample用于返回随机文档。现在只需要弄清楚为什么我的实例上没有。是否有控制台配置选项?管理员命令?(需要通过adminCommand在DocumentDB上专门启用更改流)。也许这是版本4.0.0中的一个bug。我添加了复制命令。从外观上看,这似乎是一个bug。您可以检查一下DocumentDB的早期版本吗?DocumentDB构建在云本地架构上,具有解耦的计算和存储,因此使用了与MongoDB不同的引擎。发生的情况是,您可能有少量文档,因为$sample正在返回它找到的第一个文档。当您添加更多文档时,$sample应该能够选择一个随机文档。谢谢Himanshu,但我对使用$sample操作符特别感兴趣。我意识到我可以编写一个函数来模拟$sample,但我需要避免使我的实现依赖于到NoSQL引擎的实现,因为它可能会在MongoDB或DocumentDB上运行,具体取决于它的部署位置*Math.random()},{$limit:1}]);异步计数会有一个缺陷,即如果在计数和查询之间减少集合的大小,它可能会尝试查询不再存在的记录。我同意@RichardWoods的说法。如果我发现什么,我会回来:)