【问题标题】:What is the command to access my user details in Mongo shell?在 Mongo shell 中访问我的用户详细信息的命令是什么?
【发布时间】:2019-04-08 04:08:44
【问题描述】:

显然,我的 MongoDB 数据库的本地副本中确实有一个用户。

我通过use server_users 访问了相应的数据库,但是当我访问db.getUsers() 时,我得到一个空数组。

但是当我在我的node REPL 中运行它时:

> Buffer.from(session, 'base64').toString('utf8')
'{"passport":{"user":"5ad25c401dfbaee22188a93b"}}'

您可以清楚地看到我的 Mongo 数据库中确实有一个用户。

如何在我的 mongo shell 中启动该用户?

这是我的passport.js 文件:

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');

const User = mongoose.model('users');

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id).then(user => {
    done(null, user);
  });
});

// passport.use() is a generic register to make Passport
// aware of new strategy
// creates a new instance to authenticate users
passport.use(
  new GoogleStrategy(
    {
      clientID: keys.googleClientID,
      clientSecret: keys.googleClientSecret,
      callbackURL: '/auth/google/callback',
      proxy: true
    },
    async (accessToken, refreshToken, profile, done) => {
      const existingUser = await User.findOne({ googleId: profile.id });

      if (existingUser) {
        // we already have a record with given profile id
        done(null, existingUser);
      }
      // we dont have a user record with this id, make a new record
      const user = await new User({ googleId: profile.id }).save();
      done(null, user);
    }
  )
);

【问题讨论】:

  • 您是指用于对 MongoDB 进行身份验证的 MongoDB 用户,还是用于存储应用程序用户帐户的 users 集合?
  • 通过显示的内容,问题实际上意味着“存储在集合中的护照身份验证详细信息”,这当然与db.getUsers() 无关,因为指“MongoDB 访问控制和身份验证”,它不属于“应用程序用户”。因此,数据包含在应用程序的“护照”设置中命名的集合中。您只需在适当的集合上执行 find() 即可“获取用户”。
  • @NeilLunn,感谢您唤醒我的记忆。我写的 Mongo shell 查询没有我想写的那么多。请继续发布作为答案。

标签: node.js mongodb mongo-shell


【解决方案1】:

第一步

> show dbs
    admin         0.000GB
    auth          0.000GB
    local         0.000GB
    muber         0.000GB
    seed_data     0.000GB
    server_users  0.000GB
    upstar_music  0.004GB
    users_test    0.000GB

第 2 步

> use auth
switched to db auth

第 3 步

> db.getCollection('users').find({}).pretty()
{
    "_id" : ObjectId("5cedf41999c20922fd05cdce"),
    "email" : "test123@example.com",
    "password" : "$2a$10$Yu194aPMrwuA6JZStTwu7OmaAugFNM1XvXDDBVpF1d5wKKHaPmLFe",
    "__v" : 0
}
{
    "_id" : ObjectId("5ceee2237d1b22dfbf37820f"),
    "email" : "test99@example.com",
    "password" : "$2a$10$.a9jWOSQbLG32lInCCUwGeqZDM9ksbTN7MDMo1kbOaApmMUr3cCLO",
    "__v" : 0
}
{
    "_id" : ObjectId("5ceeeb02d35e4ee61d602040"),
    "email" : "test919@example.com",
    "password" : "$2a$10$fSq31UO/4IEPMaXzgIHXK.zlMO8IUPsVh4Vf9MRo7zldk9dSgiwVS",
    "__v" : 0
}
{
    "_id" : ObjectId("5ceeee0bd72c8ae85dcbbc7f"),
    "email" : "test900@example.com",
    "password" : "$2a$10$l/99bU2MpElHhVFNXk1tB.k69DOAcdc/ySuek33MjtjAB0yLTqAqi",
    "__v" : 0
}
{
    "_id" : ObjectId("5ceef1cddf3064eaf8530f68"),
    "email" : "test901@example.com",
    "password" : "$2a$10$aKzilNGIm9HZx0iSXhsvbe9UCFqNt5FWjVd7T.GX7uKawAfCyG3JS",
    "__v" : 0
}
{
    "_id" : ObjectId("5cef25fc43a429ec05759849"),
    "email" : "test902@example.com",
    "password" : "$2a$10$JY0w/UuvT4Ub7T3RR9.VVOIH6V4RX9bTwoWzwEBbTiidPEiasakNq",
    "__v" : 0
}

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多