仅在满足条件时填充
Posted
技术标签:
【中文标题】仅在满足条件时填充【英文标题】:Populate Only When Conditions Are Met 【发布时间】:2017-12-12 08:26:21 【问题描述】:我有一个 mongodb 数据库,我使用 mongoose 和 nodejs。
只有当我有字段“temtussvinculado=true”时,我才需要从填充“tabela_tuss”的下一个查询中返回数据。
这是我正在做的:
ConvProced.find('convenioId':new ObjectId(req.params.id))
.populate('convenioId')
.populate(
path:'procedId',
populate:
path:'tabela_tuss',
match: 'procedId.temtussvinculado': true
)
.exec( (err,data) =>
callback(err,data,res)
)
我的问题是我与“procedId.temtussvinculado:true”的匹配没有效果,并且“tabela_tuss”从未填充。
我做错了什么?
这是我的架构:
////
var conveniosSchema = new mongoose.Schema(
nome: type: String, unique:true,
ativo: type: Boolean
);
module.exports = mongoose.model('Convenio', conveniosSchema,'convenios' );
////
////
const agProcedimentosSchema = new mongoose.Schema(
ativo:type:Boolean,
temtussvinculado:type:Boolean,
tabela_tuss:type:mongoose.Schema.Types.ObjectId, ref:'Tuss_22'
);
module.exports = mongoose.model('Ag_procedimento', agProcedimentosSchema,'ag_procedimentos' );
///
////
const tuss_22Schema = new mongoose.Schema(
codigo: type: String, unique:true,
descricao:type: String,
tabela:type: String
);
module.exports = mongoose.model('Tuss_22', tuss_22Schema,'tuss_22' );
////
//../models/convenioprocedimento
var conveniosProcedsSchema = new mongoose.Schema(
convenioId:type:mongoose.Schema.Types.ObjectId, ref:'Convenio',
procedId:type:mongoose.Schema.Types.ObjectId, ref:'Ag_procedimento',
valor_particular:type:Number,
valor_convenio:type:Number,
);
module.exports = mongoose.model('ConvenioProcedimento', conveniosProcedsSchema,'conveniosprocedimentos' );
//my query:
const ConvProced = require('../models/convenioprocedimento');
ConvProced.find('convenioId':new ObjectId(req.params.id))
.populate('convenioId')
.populate(
path:'procedId',
populate:
path:'tabela_tuss',
match: 'procedId.temtussvinculado': true
)
.exec( (err,data) =>
callback(err,data,res)
)
【问题讨论】:
.populate()
的 match
参数实际上是在模拟任何“加入”之前针对集合处理的,就像填充的实际工作方式一样。所以应该改为.populate( path: 'procedId', match: 'temtussvinculado': true , populate: path: 'tabela_tuss' )
谢谢尼尔伦先生。它解决了我的问题
@Neil Lunn,对不起,我的错。问题还在继续。必须始终填充路径“procedId”。如果我按照您的提示进行操作,那么当 temtussvinculadotrue 时它不会被填充。仅当 temtussvinculado=true 时,我才需要填充“tabela_tuss”
【参考方案1】:
您在这里实际上要问的是“仅填充数据中的条件要求这样做”,这实际上不是.populate()
的“直接”支持的操作或使用“嵌套填充”语法。
因此,如果您想对实际填充或不填充的项目施加“条件”,则必须“手动”处理填充调用。
在您的情况下,基本前提是您需要检查需要从“初始”***.populate()
调用中获取的值,然后在给定条件下“仅”调用“内部”填充实际上允许它。
所以你的代码应该看起来像这样使用“Promises”使用Promise.all()
,你基本上“循环”或.map()
每个查询结果并测试proceedid.temtussvinculado
看它是否是true/false
,以及在哪里true
我们实际上发出了 Model.populate()
调用,否则只返回当前状态的数据:
ConvProced.find('convenioId':new ObjectId(req.params.id))
.populate('convenioId procedId')
.exec()
.then(data =>
Promise.all(
data.map( d =>
( d.proceedid.temtussvinculado )
? mongoose.model('Tuss_22').populate(d, path: 'proceedId.tabela_tuss' )
: d
)
)
)
)
// Populated conditionally
.then( data =>
// Do something with data
)
.catch(err => console.error(err)); // or something else with error
除了“Promises”之外,还有其他可用的选项,但它是无依赖选项。 async.map
之类的替代案例可以做很多相同的事情,但如果您还没有它,则它是一个额外的依赖项:
ConvProced.find('convenioId':new ObjectId(req.params.id))
.populate('convenioId procedId')
.exec((err,data) =>
if (err) throw err;
async.map(data,(d,callback) =>
( d.proceedid.temtussvinculado )
? mongoose.model('Tuss_22').populate(d, path: 'proceedId.tabela_tuss' ,callback)
: callback(null,d)
(err,data) =>
if (err) throw err; // or something
// Conditionally populated
)
)
还通过一个完整的工作示例进行了演示,这实际上比您需要做的要复杂一些,因为在此示例中“条件”嵌套在另一个数组中:
const async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.set('debug',true);
mongoose.connect('mongodb://localhost/test');
const subInnerSchema = new Schema(
label: String
);
const innerSchema = new Schema(
name: String,
populate: Boolean,
subs: [ type: Schema.Types.ObjectId, ref: 'Sub' ]
);
const outerSchema = new Schema(
title: String,
inners: [ type: Schema.Types.ObjectId, ref: 'Inner' ]
);
const Sub = mongoose.model('Sub', subInnerSchema);
const Inner = mongoose.model('Inner', innerSchema);
const Outer = mongoose.model('Outer', outerSchema);
function log(data)
console.log(JSON.stringify(data, undefined, 2))
async.series(
[
// Clean data
(callback) =>
async.each(mongoose.models,(model,callback) =>
model.remove(,callback),callback),
// Insert some data
(callback) =>
async.waterfall(
[
(callback) =>
Sub.create([1,2,3,4].map( label => ( label )),callback),
(subs,callback) =>
Inner.create(
[0,2].map(x => subs.slice(x,x+2))
.map((el,i) => (
name: i+i,
populate: i == 1,
subs: el
)),
callback
),
(inners,callback) =>
Outer.create(
inners.map((inner,i) => (
title: i+1,
inners: [inner]
)),
callback
),
],
callback
),
// Conditional populate async.map version
(callback) =>
Outer.find().populate('inners').exec((err,outers) =>
if (err) callback(err);
async.map(
outers,
(outer,callback) =>
async.map(
outer.inners,
(inner,callback) =>
(inner.populate)
? Inner.populate(inner, path: 'subs' ,callback)
: callback(null,inner),
(err,inners) =>
if (err) callback(err);
outer.inners = inners
callback(null,outer);
),
(err,outers) =>
if (err) callback(err);
log(outers);
callback();
);
),
// Conditional populate Promise
(callback) =>
Outer.find().populate('inners').exec()
.then(outers =>
Promise.all(
outers.map( outer =>
new Promise((resolve,reject) =>
Promise.all(
outer.inners.map( inner =>
(inner.populate)
? Inner.populate(inner, path: 'subs' )
: inner
)
).then(inners =>
outer.inners = inners;
resolve(outer)
)
.catch(reject)
)
)
)
)
.then(outers =>
log(outers);
callback();
)
.catch(err => callback(err))
],
(err) =>
if (err) throw err;
mongoose.disconnect();
);
当然,这会产生显示“条件”选择的输出:
Mongoose: subs.remove(, )
Mongoose: inners.remove(, )
Mongoose: outers.remove(, )
Mongoose: subs.insert( label: '1', _id: ObjectId("5961830256bf9e2d0fcf13b3"), __v: 0 )
Mongoose: subs.insert( label: '2', _id: ObjectId("5961830256bf9e2d0fcf13b4"), __v: 0 )
Mongoose: subs.insert( label: '3', _id: ObjectId("5961830256bf9e2d0fcf13b5"), __v: 0 )
Mongoose: subs.insert( label: '4', _id: ObjectId("5961830256bf9e2d0fcf13b6"), __v: 0 )
Mongoose: inners.insert( name: '0', populate: false, _id: ObjectId("5961830256bf9e2d0fcf13b7"), subs: [ ObjectId("5961830256bf9e2d0fcf13b3"), ObjectId("5961830256bf9e2d0fcf13b4") ], __v: 0 )
Mongoose: inners.insert( name: '2', populate: true, _id: ObjectId("5961830256bf9e2d0fcf13b8"), subs: [ ObjectId("5961830256bf9e2d0fcf13b5"), ObjectId("5961830256bf9e2d0fcf13b6") ], __v: 0 )
Mongoose: outers.insert( title: '1', _id: ObjectId("5961830256bf9e2d0fcf13b9"), inners: [ ObjectId("5961830256bf9e2d0fcf13b7") ], __v: 0 )
Mongoose: outers.insert( title: '2', _id: ObjectId("5961830256bf9e2d0fcf13ba"), inners: [ ObjectId("5961830256bf9e2d0fcf13b8") ], __v: 0 )
Mongoose: outers.find(, fields: )
Mongoose: inners.find( _id: '$in': [ ObjectId("5961830256bf9e2d0fcf13b7"), ObjectId("5961830256bf9e2d0fcf13b8") ] , fields: )
Mongoose: subs.find( _id: '$in': [ ObjectId("5961830256bf9e2d0fcf13b5"), ObjectId("5961830256bf9e2d0fcf13b6") ] , fields: )
[
"_id": "5961830256bf9e2d0fcf13b9",
"title": "1",
"__v": 0,
"inners": [
"_id": "5961830256bf9e2d0fcf13b7",
"name": "0",
"populate": false,
"__v": 0,
"subs": [
"5961830256bf9e2d0fcf13b3",
"5961830256bf9e2d0fcf13b4"
]
]
,
"_id": "5961830256bf9e2d0fcf13ba",
"title": "2",
"__v": 0,
"inners": [
"_id": "5961830256bf9e2d0fcf13b8",
"name": "2",
"populate": true,
"__v": 0,
"subs": [
"_id": "5961830256bf9e2d0fcf13b5",
"label": "3",
"__v": 0
,
"_id": "5961830256bf9e2d0fcf13b6",
"label": "4",
"__v": 0
]
]
]
Mongoose: outers.find(, fields: )
Mongoose: inners.find( _id: '$in': [ ObjectId("5961830256bf9e2d0fcf13b7"), ObjectId("5961830256bf9e2d0fcf13b8") ] , fields: )
Mongoose: subs.find( _id: '$in': [ ObjectId("5961830256bf9e2d0fcf13b5"), ObjectId("5961830256bf9e2d0fcf13b6") ] , fields: )
[
"_id": "5961830256bf9e2d0fcf13b9",
"title": "1",
"__v": 0,
"inners": [
"_id": "5961830256bf9e2d0fcf13b7",
"name": "0",
"populate": false,
"__v": 0,
"subs": [
"5961830256bf9e2d0fcf13b3",
"5961830256bf9e2d0fcf13b4"
]
]
,
"_id": "5961830256bf9e2d0fcf13ba",
"title": "2",
"__v": 0,
"inners": [
"_id": "5961830256bf9e2d0fcf13b8",
"name": "2",
"populate": true,
"__v": 0,
"subs": [
"_id": "5961830256bf9e2d0fcf13b5",
"label": "3",
"__v": 0
,
"_id": "5961830256bf9e2d0fcf13b6",
"label": "4",
"__v": 0
]
]
]
因此您可以看到,以几乎相同的方式,有一个“布尔”字段正在测试以确定是执行 .populate()
还是只返回纯数据。
【讨论】:
以上是关于仅在满足条件时填充的主要内容,如果未能解决你的问题,请参考以下文章