mongodb连表字段的类型不一致处理

需求:需要从member , memberMessage两个集合中按照memberId,msgTimesTamp 分组 并返回sex , city , msgTimesTamp。member 中有个memberId 字段是NumberLong 类型的,memberMessage 中的fromUserId 是String类型的。

难点:就是在于 memberId,fromUserId 字段类型不一致, 导致连表查询不出数据

解决方案就是把fromUserId由String 类型 转为NumberLong 类型,因为不熟悉mongodb 的函数就找了很久,于是记录一下


db.messageRoute.aggregate([
    
    {
        $project: {
            msgTimestamp: {
                $dateToString: {
                    format: "%Y-%m-%d",
                    date: "$msgTimestamp"
                }
            },
            fromUserId: {
                $convert: {
                    input: "$fromUserId",
                    to: "long",
                    onError: "An error occurred",
                    onNull: "Input was null or empty"
                }
            }
        }
    },
    {
        
        $match: {
            msgTimestamp: {
                $gte: '2021-11-01'
            },
            "$and": [
                {
                    fromUserId: {
                        $ne: 'sys-system'
                    }
                },
                {
                    fromUserId: {
                        $ne: 'sys-gift'
                    }
                }
            ]
        }
    },
    {
        $group: {
            _id: {
                "msgTimestamp": "$msgTimestamp",
                "fromUserId": "$fromUserId"
            },
            msgTimestamp: {
                $first: "$msgTimestamp"
            },
            fromUserId: {
                $first: "$fromUserId"
            }
        }
    }
    ,
    {
        $lookup: {
            // 左连接
            from: "memberMongo", // 关联到memberMongo表
            localField: "fromUserId",
            foreignField: "memberId", // memberMongo 表关联的字段
            as: "memberMongo"
        }
    },
    {
        $unwind: "$memberMongo"
    },
    {
        
        $match: {
            "memberMongo.sex": 0
        }
    },
    {
        $project: {
            _id: 0,
            msgTimestamp: 1,
            username: "$memberMongo.username",
            city: "$memberMongo.city"
        }
    },
    {
        
        $sort: {
            msgTimestamp: - 1
        }
    }
]);

用到了**$convert** 操作符,有兴趣的可以查阅一下mongodb中文文档

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过表名查询 MongoDB 的所有字段,你可以使用 `db.collection` 的 `find()` 方法来获取集合中的文档,并使用 `Object.keys()` 来提取文档的键(字段名)。以下是一个使用 Koa 框架的例子: ```javascript const Koa = require('koa'); const Router = require('koa-router'); const MongoClient = require('mongodb').MongoClient; const app = new Koa(); const router = new Router(); // MongoDB连接URL const url = 'mongodb://localhost:27017'; // 数据库名称 const dbName = 'your_database_name'; router.get('/collection/:collectionName/fields', async (ctx) => { const collectionName = ctx.params.collectionName; try { // 连接MongoDB const client = await MongoClient.connect(url); const db = client.db(dbName); // 获取集合中的第一个文档 const document = await db.collection(collectionName).findOne(); // 提取文档的键(字段名) const fields = Object.keys(document); // 关闭连接 client.close(); // 返回结果 ctx.body = { fields }; } catch (error) { ctx.status = 500; ctx.body = { error: 'Error retrieving fields' }; } }); app.use(router.routes()).use(router.allowedMethods()); app.listen(3000, () => { console.log('Server listening on port 3000'); }); ``` 在上面的示例中,我们通过路由参数 `:collectionName` 获取要查询的表名。然后,我们使用 `findOne()` 方法从集合中获取第一个文档,并使用 `Object.keys()` 提取文档的键(字段名)。最后,我们将字段名作为响应体返回给客户端。 请确保将 `your_database_name` 替换为实际的数据库名称,并确保 MongoDB 服务器正在运行并监听在默认端口 `27017` 上。同样,你可能需要根据自己的需求进行适当的错误处理和路由设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值