mongoose/mongodb是否可以在聚合期间访问架构中的对象引用? [英] Does mongoose / mongodb have access to object references in schema during aggregate?
问题描述
我正在处理一个查询,该查询从mongo数据库中的2个不同的obj引用中读取.我将使用一个简单的示例说明我正在寻找什么.
I'm working on a query that reads from 2 different obj referentes inside my mongo database. I will use a simple example of what im looking for.
我有3种模式:
User = new Schema({
places:[{type: Schema.Types.ObjectId, ref:'Place'}],
shouts:[{type: Schema.Types.ObjectId, ref:'Shout'}]
});
Place = new Schema({
name:String,
description:String,
});
Shout = new Schema({
content:String,
});
我最大的问题是,执行聚合方法时,mongoose或mongodb是否可以访问objectId引用.请允许我详细说明.
My biggest question is if mongoose or mongodb has access to objectId references when executing the aggregate method. Allow me to elaborate.
module.exports.askForShoutInPlace = function(req, res){
var pname = new RegExp(req.params.pname, 'i');
User.aggregate(
[
{'$match':{ 'places':{
'$elemMatch':{'name':pname}
}
},
{'$project':{ shout:'$shouts'} },
{'$unwind':'$shouts'},
{'$group':{_id:'$shouts'}}
]).exec(function(err, results){
res.send(results);
});
}
通常可以正常工作,但是一旦调用$ match运算符,我就会得到一个空数组,我猜想这与返回未定义子对象的对象引用有关.有什么解决办法吗?还是这意味着我必须走另一条路来雇用人口稠密的人?
That usually works fine, however I'm getting an empty array once the $match operator is called, im guessing it has to do with the object references returning undefined subobjects. is there any work around this? or does this mean I have to take another route to employ populating?
感谢提前提供的所有帮助
thanks for all the help in advance
推荐答案
在聚合过程中无法访问对象引用的数据,我在我的项目中采用的解决方法是在以下模式中添加对所有者的引用问题.
there's no way to access object Referenced data during the aggregate Process, the work around I employed for my project was to add a reference to the owner in the schemas in question.
User = new Schema({
places:[{type: Schema.Types.ObjectId, ref:'Place'}],
shouts:[{type: Schema.Types.ObjectId, ref:'Shout'}]
});
Place = new Schema({
owner:{type: Schema.Types.ObjectId, ref:'Place'},
name:String,
description:String,
});
Shout = new Schema({
owner:{type: Schema.Types.ObjectId, ref:'Place'},
content:String,
});
然后用于直接在子文档上进行聚合,以获得拥有场所实例的唯一用户,这样我就可以获得与查询和场所匹配的喊叫结果.
And then employed to Aggregate directly on the subdocument to obtain the unique users who own the instances of of place, this way I can obtain a shout result matching a query and a place.
示例:
module.exports.askForShoutInPlace = function(req, res){
var pname = new RegExp(req.params.pname, 'i');
var stringQ = new RegExp(req.paramos.qcontent, 'i');
Place.aggregate(
[
//find Places that match criteria
{'$match':{'name':pname}},
//select owner id object to result
{'$project':{ owner:'$owner'}},
//group those results to single array with unique ids of users
{'$group':{_id:'$owner'}}
]).exec(function(err, results){
//find user shouts that match string and belong to owners know to be owners of a place
Shout.find({'content':stringQ}).where({'owner':{'$in':results}}).exec(function(err, shouts){
res.send(shouts);
});
});
}
这只是我发现可以满足我的特殊需求的方式,希望对您有所帮助.
this is just the way I found to work around my particular needs, I hope it might help somebody.
这篇关于mongoose/mongodb是否可以在聚合期间访问架构中的对象引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!