考虑以下攻击:
let getUsersWithNoPersonsPromise = () => {
let pipeline = [
{
$lookup: {
from: "persons",
localField: "id",
foreignField: "person_id",
as: "persons_users"
}
},
{
$match: {
"persons_users:0": {
$exists: false
}
}
}
];
return User.aggregate(pipeline).exec();
}
如何将$match
结果转换为UserModel类型?我正在上面获取普通的javascript对象,我希望收到mongoose用户类型对象。
答案 0 :(得分:1)
您可以使用聚合查询的结果,并通过迭代结果数组来简单地实例化新对象,如下所示(假设您使用异步等待);
r.table('upcs').filter({AcceptedUPC:data}).filter(r.row.hasFields("UPCCount")).update({UPCCount: r.row("UPCCount").sub(1)}).run(conn, (err,cursor) => {
if (err) throw err;
});
答案 1 :(得分:0)
你可以使用这个:
const { Query } = require('mongoose');
const mongooseQuery = new Query();
// pass the model and filter to mongoose to cast
// you can add this to pre aggregation middleware
mongooseQuery.cast(Model, filter);