我知道这个问题已经被问过很多次了,但是我对mongo和mongoose也很陌生,我无法解决!
我的问题:
我有一个看起来像这样的:
var rankingSchema = new Schema({
userId : { type : Schema.Types.ObjectId, ref:'User' },
pontos : {type: Number, default:0},
placarExato : {type: Number, default:0},
golVencedor : {type: Number, default:0},
golPerdedor : {type: Number, default:0},
diferencaVencPerd : {type: Number, default:0},
empateNaoExato : {type: Number, default:0},
timeVencedor : {type: Number, default:0},
resumo : [{
partida : { type : Schema.Types.ObjectId, ref:'Partida' },
palpite : [Number],
quesito : String
}]
});
这将返回这样的文档:
{
"_id" : ObjectId("539d0756f0ccd69ac5dd61fa"),
"diferencaVencPerd" : 0,
"empateNaoExato" : 0,
"golPerdedor" : 0,
"golVencedor" : 1,
"placarExato" : 2,
"pontos" : 78,
"resumo" : [
{
"partida" : ObjectId("5387d991d69197902ae27586"),
"_id" : ObjectId("539d07eb06b1e60000c19c18"),
"palpite" : [
2,
0
]
},
{
"partida" : ObjectId("5387da7b27f54fb425502918"),
"quesito" : "golsVencedor",
"_id" : ObjectId("539d07eb06b1e60000c19c1a"),
"palpite" : [
3,
0
]
},
{
"partida" : ObjectId("5387dc012752ff402a0a7882"),
"quesito" : "timeVencedor",
"_id" : ObjectId("539d07eb06b1e60000c19c1c"),
"palpite" : [
2,
1
]
},
{
"partida" : ObjectId("5387dc112752ff402a0a7883"),
"_id" : ObjectId("539d07eb06b1e60000c19c1e"),
"palpite" : [
1,
1
]
},
{
"partida" : ObjectId("53880ea52752ff402a0a7886"),
"quesito" : "placarExato",
"_id" : ObjectId("539d07eb06b1e60000c19c20"),
"palpite" : [
1,
2
]
},
{
"partida" : ObjectId("53880eae2752ff402a0a7887"),
"quesito" : "placarExato",
"_id" : ObjectId("539d0aa82fb219000054c84f"),
"palpite" : [
2,
1
]
}
],
"timeVencedor" : 1,
"userId" : ObjectId("539b2f2930de100000d7356c")
}
我的问题是,首先:如何通过quesito过滤resumo嵌套文档?是否有可能对此结果进行分页,因为此数组将要增加。最后一个问题,这是解决这种情况的好方法吗?
感谢大伙们 !
如前所述,即使存储外部引用,您的架构也意味着您实际上已经嵌入了数据。因此,尚不清楚您是同时进行嵌入还是引用,还是仅进行自身嵌入。
这里最大的警告是匹配“文档”和实际过滤数组内容之间的区别。由于您似乎在谈论“分页”数组结果,因此这里主要关注的是这样做,但仍会提及警告。
数组中的多个“已过滤”匹配项需要聚合框架。通常,您可以“投影”数组元素的单个匹配项,但是如果您期望多个匹配项,则需要这样做:
Ranking.aggregate(
[
// This match finds "documents" that "contain" the match
{ "$match": { "resumo.quesito": "value" } },
// Unwind de-normalizes arrays as documents
{ "$unwind": "$resumo" },
// This match actually filters those document matches
{ "$match": { "resumo.quesito": "value" } },
// Skip and limit for paging, which really only makes sense on single
// document matches
{ "$skip": 0 },
{ "$limit": 2 },
// Return as an array in the original document if you really want
{ "$group": {
"_id": "$_id",
"otherField": { "$first": "$otherField" },
"resumo": { "$push": "$resumo" }
}}
],
function(err,results) {
}
)
或者使用MongoDB 2.6方式通过$project
使用$map
操作符“过滤”内部。但是仍然需要对$unwind
页面位置进行“分页”,但是可能会减少处理,因为首先对数组进行“过滤”:
Ranking.aggregate(
[
// This match finds "documents" that "contain" the match
{ "$match": { "resumo.quesito": "value" } },
// Filter with $map
{ "$project": {
"otherField": 1,
"resumo": {
"$setDifference": [
{
"$map": {
"input": "$resumo",
"as": "el",
"in": { "$eq": ["$$el.questio", "value" ] }
}
},
[false]
]
}
}},
// Unwind de-normalizes arrays as documents
{ "$unwind": "$resumo" },
// Skip and limit for paging, which really only makes sense on single
// document matches
{ "$skip": 0 },
{ "$limit": 2 },
// Return as an array in the original document if you really want
{ "$group": {
"_id": "$_id",
"otherField": { "$first": "$otherField" },
"resumo": { "$push": "$resumo" }
}}
],
function(err,results) {
}
)
仅当您处理单个文档并仅“过滤”和“分页”数组时,$skip
and的内部用法$limit
才有意义。可以使用多个文档来执行此操作,但是由于无法“切片”数组,因此非常麻烦。这将我们带到了下一点。
对于嵌入式数组,对于不需要任何筛选的分页,您只需使用$slice
运算符,该运算符就是为此目的而设计的:
Ranking.find({},{ "resumo": { "$slice": [0,2] } },function(err,docs) {
});
不过,您的替代方法是只引用外部集合中的文档,然后将参数传递给mongoose.populate()
以筛选和“分页”结果。模式本身的更改将是:
"resumo": [{ "type": "Schema.Types.ObjectId", "ref": "Partida" }]
现在,使用外部引用集合可以保存对象详细信息,而不是直接将其嵌入数组中。的使用.populate()
与过滤和分页是:
Ranking.find().populate({
"path": "resumo",
"match": { "questio": "value" },
"options": { "skip": 0, "limit": 2 }
}).exec(function(err,docs) {
docs = docs.filter(function(doc) {
return docs.comments.length;
});
});
当然,可能存在的问题是您不再能够像现在在另一个集合中那样实际查询包含“嵌入式”信息的文档。这会导致拉入所有文档(尽管可能是通过其他查询条件),但随后会对其进行手动测试,以查看它们是否被发送来检索这些项目的过滤查询“填充”。
因此,这确实取决于您在做什么以及您的方法。如果您打算定期“搜索”内部数组,那么嵌入通常会更适合您。另外,如果您真的只对“分页”感兴趣,那么$slice
操作员可以很好地为此目的使用嵌入式文档。但是请注意,嵌入式阵列的增长太大了。
将引用的模式与猫鼬一起使用有助于解决一些大小问题,并且有适当的方法可以辅助“分页”结果并对其进行过滤。缺点是您不能再从父级本身“查询”这些元素。因此,通过内部元素进行父级选择不适用于此处。还请记住,虽然不是所有数据都被嵌入,但仍然引用_id
了外部文档的值。因此,您仍然可以得到大型阵列,这可能是不希望的。
对于任何较大的东西,请考虑您可能自己进行工作,然后从“子项”向后工作以匹配父项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句