Javascript 猫鼬与种群
我读过一些关于猫鼬的文章,好像它无法管理巢穴种群 事实上,当我执行findOne()调用时,返回的对象能够使用populate函数。但它似乎不起作用 你能帮我处理一个2级的文件吗?多个查询不是问题 以下是我的咖啡脚本代码:Javascript 猫鼬与种群,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我读过一些关于猫鼬的文章,好像它无法管理巢穴种群 事实上,当我执行findOne()调用时,返回的对象能够使用populate函数。但它似乎不起作用 你能帮我处理一个2级的文件吗?多个查询不是问题 以下是我的咖啡脚本代码: Agency.findOne({"agencyUsers": idUser}, '_id agencyUsers agencySites agencyDevices').populate([ {path: 'agencyUsers', match: { actif
Agency.findOne({"agencyUsers": idUser}, '_id agencyUsers agencySites agencyDevices').populate([
{path: 'agencyUsers', match: { actif: true}, select: '_id userPin'},
{path: 'agencySites', match: { actif: true}, select: '_id siteName siteBuildings siteSectors siteUsers'},
{path: 'agencyDevices', match: { actif: true}, select: '_id deviceMac'}
])
.exec((err, res)=>
if err
deffered.reject err
else
res.populate('agencySites.siteBuildings', (errs, ress)=>
deffered.resolve(res)
)
)
编辑:
根据我们需要在现场(机构内)填充建筑物的事实
代理架构:
{
agencyName: String,
agencyStreet: String,
agencyCity: String,
agencyPostal: String,
agencyCountry: String,
agencyPhone: String,
agencyFax: String,
agencyMail: String,
agencySiret: String,
agencyAgencies: [
{ type: Schema.ObjectId, ref: 'Agency' }
],
agencyDevices: [
{ type: Schema.ObjectId, ref: 'Device' }
],
agencySites: [
{ type: Schema.ObjectId, ref: 'Site' }
],
agencyUsers: [
{ type: Schema.ObjectId, ref: 'User' }
],
agencyModules: [
{ type: Schema.ObjectId, ref: 'Module' }
],
actif: {type: Boolean, default: true},
actif_date: { type: Date, default: null },
creation_date: { type: Date, default: Date.now },
edit_date: { type: Date, default: null }
}
地点:schéma:
{
siteName: String,
siteStreet: String,
siteCity: String,
sitePostal: String,
siteCountry: String,
sitePhone: String,
siteMail: String,
siteAgencies: [
{ type: Schema.ObjectId, ref: 'Agency' }
],
siteUsers: [
{ type: Schema.ObjectId, ref: 'User' }
],
siteSectors: [
{ type: Schema.ObjectId, ref: 'Sector' }
],
siteBuildings: [
{ type: Schema.ObjectId, ref: 'Building' }
],
siteModules: [
{ type: Schema.ObjectId, ref: 'Module' }
],
actif: {type: Boolean, default: true},
actif_date: { type: Date, default: null },
creation_date: { type: Date, default: Date.now },
edit_date: { type: Date, default: null }
}
建筑模式:
{
buildingName: String,
buildingFloors: [
{ type: Schema.ObjectId, ref: 'Floor' }
],
actif: {type: Boolean, default: true},
actif_date: { type: Date, default: null },
creation_date: { type: Date, default: Date.now },
edit_date: { type: Date, default: null }
}
仅JavaScript响应。你做的是:) 由于我目前无法理解的实际原因,在这种情况下,这并不能真正起作用,但为了实现您想要的,您需要称之为的“模型”形式:
Agency.findOne({“agencyUsers”:idUser},“\u id agencyUsers agencySites agencyDevices”)
.填充([
{“path”:“agencyUsers”,“match”:{“actif”:true},“select”:“\u id userPin”},
{
“路径”:“代理站点”,
“匹配”:{“actif”:true},
“选择”:“\u id siteName siteBuildings SiteSector siteUsers”
},
{
“路径”:“代理设备”,
“匹配”:{“actif”:true},
“选择”:“\u id deviceMac”
}
]).exec(函数(错误、文档){
如果(错误){
拒绝(错误);
}否则{
异步瀑布([
函数(回调){
填充(doc,{“path”:“agencyAgencies.siteAgenerations”},回调);
},
函数(文档、回调){
填充(doc,{“path”:“agencyAgencies.siteUsers”},回调);
},
函数(文档、回调){
填充(doc,{“path”:“agencyAgencies.siteSectors”},回调);
},
函数(文档、回调){
填充(doc,{“path”:“agencyAgencies.siteBuildings”},回调);
},
函数(文档、回调){
填充(doc,{“path”:{“agencyAgencies.siteModules”},回调);
}
],函数(err,res){
如果(错误){
拒绝(错误);
}否则{
Floor.populate(res,{“path”:“agencyAgencies.siteBuildings.buildingFloors”},函数(err,res){
如果(错误){
拒绝(错误);
}否则{
不同的解决方案(res);
}
});
}
});
}
});
尽管我使用了,但我只是试图通过每次嵌入的迭代来避免“缩进蠕变”
因此,正如您所看到的,对于每个特定的模型类型,实际上有必要以这种方式调用.populate()
。此外,“再次嵌套的”“构建”模型引用中也有必要的调用,也需要在“该”文档填充后调用。因此,“调用堆栈”也是如此这是您前面提到的嵌套填充问题的一部分
<> P>这么重的引用,你可以考虑架构重新设计和/或嵌入大量的数据,看起来很“关系”。因此,通过这种方式,您可能无法获得MongoDB的全部好处。@Neil Lunn的回答是正确的,我只是想说明一下,如果您有兴趣清理一下代码:
co(function* {
var agency = yield Agency.findOne({'agencyUsers': idUser}, '_id agencyUsers agencySites agencyDevices')
.populate(
{ 'path': 'agencyUsers', 'match': { 'actif': true }, 'select': '_id userPin' },
{ 'path': 'agencySites', 'match': { 'actif': true }, 'select': '_id siteName siteBuildings siteSectors siteUsers' },
{ 'path': 'agencyDevices', 'match': { 'actif': true }, 'select': '_id deviceMac' })
agency = yield Agency.populate(agency, 'agencyAgencies.siteAgencies')
agency = yield User.populate(agency, 'agencyAgencies.siteUsers')
agency = yield Sector.populate(agency, 'agencyAgencies.siteSectors')
agency = yield Building.populate(agency, 'agencyAgencies.siteBuildings')
agency = yield Module.populate(agency, 'agencyAgencies.siteModules')
return agency
})
.then(agency => {
Floor.populate(agency, 'agencyAgencies.siteBuildings.buildingFloors', function (err, res) {
if (err)
deferred.reject(err)
deferred.resolve(agency)
})
})
.catch(deferred.reject)
从^4.1.x版开始,深层种群实际上是猫鼬中的一种东西,所以这样做是有效的:-
Post.find({}).populate({
path: 'user',
select: 'id name',
populate: {
path: 'friends',
select: 'id name,'
populate: {
path: 'friends',
select: 'id name',
}
}
})
populate
可以将对象或对象数组作为值,这意味着您可以填充引用不同集合的同级属性。
有关更多信息,请查看此帖子:您的模式将是此处要查看的重要部分。我添加了Coffeescript标记,因为这与JavaScript无关。