mongodb 在使用metamodel getCollectionNames拉取到系统表

Query failed with error code 13 and error message 'not authorized on longtest to execute command { find: "system.profile", limit: 1000, $db: "longtest", $readPreference: { mode: "primaryPreferred" } }

没有开场白,直接说,mongodb 版本 4.0.28、4.0.14、5.0.5发现的问题

 我有个用户 xianchang 对 数据库 longtest 有 readWrite。
然后另一个用户longtestAdmin  对数据库 longtest 有权限开启 Database Profiler 的权限。

Database Profiler — MongoDB Manual

这时当我用metamodel 用xianchang用户 使用 db.getCollectionNames()就会拉取到system.profile 这张表,

 

但是用户 xianchang 对 system.profile 这张表是没有find() 权限的

所以后端报错:

com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized on longtest to execute command { find: "system.profile", limit: 1000, $db: "longtest", $readPreference: { mode: "primaryPreferred" } }' on server 172.16.0.209:28017
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:706)
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:695)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:462)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:695)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:83)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179)
    at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132)
    at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86)
    at org.apache.metamodel.mongodb.mongo3.MongoDbDataContext.detectTable(MongoDbDataContext.java:155)
    at org.apache.metamodel.mongodb.mongo3.MongoDbDataContext.detectSchema(MongoDbDataContext.java:132)
    at org.apache.metamodel.mongodb.mongo3.MongoDbDataContext.<init>(MongoDbDataContext.java:113)

在metamodel 的源码中便会用的find()语句

在客户的那边使用Navicat 遍历 db.getCollectionNames()也发现了这张表,

 暂时先写这么多有空再补……请有懂的大佬赐教

  • 13
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Egg.js 中使用 MongoDB 来生成的过程如下: 1. 首先,确保已经在你的项目中安装了 MongoDB 相关的依赖。可以使用 npm 或者 yarn 进行安装,如下所示: ``` npm install egg-mongoose --save ``` 2. 在 Egg.js 的配置文件 (`config/config.default.js` 或者 `config/config.prod.js`) 中进行相关配置。你需要添加以下内容: ```javascript // config/config.default.js exports.mongoose = { client: { url: 'mongodb://localhost:27017/your_database_name', options: {}, }, }; ``` 注意将 `'mongodb://localhost:27017/your_database_name'` 替换为你实际的 MongoDB 连接地址和数据库名称。 3. 创建 Model 文件,在 Egg.js 中,可以使用 Mongoose 来定义模型。在 `app/model` 目录下创建一个新的 JavaScript 文件,例如 `user.js`,并编写以下代码: ```javascript // app/model/user.js module.exports = app => { const mongoose = app.mongoose; const Schema = mongoose.Schema; const UserSchema = new Schema({ name: { type: String }, age: { type: Number }, email: { type: String }, }); return mongoose.model('User', UserSchema); }; ``` 4. 使用 Model 进行增删改查操作。在 Controller 中引入 Model,并使用相应的方法来操作数据库。例如,在 `app/controller/user.js` 文件中编写以下代码: ```javascript // app/controller/user.js const Controller = require('egg').Controller; class UserController extends Controller { async create() { const { ctx } = this; const { name, age, email } = ctx.request.body; const user = new ctx.model.User({ name, age, email, }); try { await user.save(); ctx.body = 'User created successfully'; } catch (error) { ctx.body = error; } } async find() { const { ctx } = this; try { const users = await ctx.model.User.find(); ctx.body = users; } catch (error) { ctx.body = error; } } // 其他操作方法类似 } module.exports = UserController; ``` 这样,你就可以在 Egg.js 中使用 MongoDB 来生成,并进行相应的增删改查操作了。需要注意的是,上述代码仅为示例,你可能需要根据你的实际需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值