我有一个Cheques和一个Payees的集合,每一张支票都有对应的Payee ID.

我想要做的是编写一些关于支票的查询,但我需要在填充收款人(以获得姓名)后进行搜索

const search = req.query.search || "";
const cheques = await Cheque
    .find({
        isCancelled: false,
        dueDate: { $gte: sinceDate, $lte: tillDate }
    })
    .select("_id serial dueDate value payee")
    .skip(page * limit)
    .limit(limit)
    .sort({ dueDate: -1, serial: 1 })
    .populate({
        path: "payee",
        select: "name"
    })

我想我要做的就是把这个放进我的代码里,

match: {
    name: { $regex: search, $options: "i" }
   },

我try 将匹配放在填充中,但它仍然会找到所有支票,即使它们不满足填充匹配但填充为空.

推荐答案

我讨厌这个答案,我希望有人会发布一个更好的答案,但我已经在网上冲浪,但没有运气. 我所能找到的唯一方法是在聚合中使用$lookup方法.

因此,您必须将您的代码从呼叫.find()更改为.aggregate().

这不是悲伤的消息,它是伟大的,稳定的,没有任何问题. 但我讨厌它,因为它将改变您在代码中可能遵循的一些模式.

const search = req.query.search || "";
    const cheques = await Cheque
        .aggregate([
             {
               $lookup: { // similar to .populate() in mongoose
                from: 'payees', // the other collection name
                localField: 'payee', // the field referencing the other collection in the curent collection
                foreignField: '_id', // the name of the column where the cell in the current collection can be found in the other collection
                as: 'payee' // the field you want to place the db response in. this will overwrite payee id with the actual document in the response (it only writes to the response, not on the database, no worries)
             },
             { // this is where you'll place your filter object you used to place inside .find()
               $match: {
                 isCancelled: false,
                 dueDate: { $gte: sinceDate, $lte: tillDate }
                 'payee.branch': 'YOUR_FILTER', // this is how you access the actual object from the other collection after population, using the dot notation but inside a string.
               }
             },
             { // this is similar to .select()
               $project: {_id: 1, serial: 1, dueDate: 1, value: 1, payee: 1}
             },
             {
               $unwind: '$payee' // this picks the only object in the field payee: [ { payeeDoc } ] --> { payeeDoc }
             }
        ])
        .skip(page * limit)
        .limit(limit)
        .sort({ dueDate: -1, serial: 1 })

请注意,您不能再像以前在.find()上那样在模型查询上链接.select().populate(),因为现在您使用的是.aggregate(),它返回Mongoose中的不同类实例.

  • 如果愿意,您可以调用.projcet(),而不是在聚合数组中调用,但据我所知,您不能使用.select()方法.

我对这个问题的基于意见的解决方案是将您需要过滤的收款人信息包括在支票集合中.

在我的管理员中,当我为了我的用户--角色和权限--而进行过滤时,就会发生这种情况,这样别人就看不到别人看到的东西了.

这由您决定,但这会在以后您想要生成报告时变得更容易(我假设您正在使用支付服务).

Javascript相关问答推荐

如何避免移动设备中出现虚假调整大小事件?

MongoDB中的引用

在这种情况下,如何 for each 元素添加id?

在观察框架中搜索CSV数据

单击ImageListItemBar的IconButton后,在Material—UI对话框中显示图像

CheckBox作为Vue3中的一个组件

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

覆盖加载器页面避免对页面上的元素进行操作

如何在Bootstrap中减少网格系统中单个div的宽度

当代码另有说明时,随机放置的圆圈有时会从画布上消失

使用类型:assets资源 /资源&时,webpack的配置对象&无效

AddEventListner,按键事件不工作

使用jQuery find()获取元素的属性

当我点击一个按钮后按回车键时,如何阻止它再次被点击

为什么我看到的是回复,而不是我的文档?

在Press Reaction本机和EXPO av上播放单个文件

正在发出错误的URL请求

使用导航时,路径的所有子组件都必须是路径

在给定的最小值、最大值和分辨率下计算相同分布的值

为什么Reaction useEffect函数会将控制台日志(log)呈现两次