mongodb - 从嵌套数组中分页嵌入对象
问题描述
我对 mongodb 比较陌生,想知道是否可以查询下面的 BusinessDetails 集合,对于 _id: 6164bd360ab23b1482e1f10a 的业务,我可以分页其嵌入的客户对象,比如一次 10 个客户?
{
"_id" : ObjectId("6164bd360ab23b1482e1f10a"),
"name" : "Biz1",
"user" : {
"$ref" : "jhi_user",
"$id" : "user-1"
},
"customers" : [
{
"$ref" : "customer_details",
"$id" : ObjectId("6164bd150ab23b1482e1f109")
},
{
"$ref" : "customer_details",
"$id" : ObjectId("6164bd080ab23b1482e1f108")
},
{..},{..},{..}
],
"_class" : "com.mycompany.seol.domain.BusinessDetails"
}
解决方案
您可以使用 $slice
db.collection.aggregate([
{
$match: {
"_id": ObjectId("6164bd360ab23b1482e1f10a")
}
},
{
$addFields: {
customers: {
"$slice": [
"$customers",
0, // position
1 // limit
]
}
}
}
])
工作Mongo游乐场
推荐阅读
- regex - Bash 将两条特定的行合并为一条
- c++ - Why does my interrupt get called, but won't enter the handler?
- reporting-services - 如何在 RDLC 报告的每一页上显示页眉?
- python - 需要类“numpy.int32”,但 type() 函数仅显示:numpy.ndarray
- python - Django Forms - Is 'readonly' safe to use?
- http - What is the difference between THTTPClient and TNetHTTPClient?
- android - issue in while using `WorkContinuation.combine()` when WorkManger is initialized using dagger2
- python - 更新 Anaconda 时出错:UnsatisfiableError: 发现以下规范有冲突
- excel - How to open files in folder with specific name in VBA?
- excel - 将公式输入vba时出现错误代码