【问题标题】:Mongoose query exact fieldMongoose 查询精确字段
【发布时间】:2016-10-04 04:35:12
【问题描述】:

我正在使用以下(缩写)架构创建的集合:

username: String,
social: {
    github: {
        id: String,
        username: String,
        token: String
    },
    linkedin: {
        id: String,
        token: String,
        tokenSecret: String
    }
}

我有以下查询 UserModel.findOne({'username': req.body.username}...,我想返回具有匹配 username 字段的文档,返回 social.github.username 字段。不过,查询返回的文档与这两个字段都匹配。

如何修复查询?

【问题讨论】:

  • 您所做的已经是正确的。这里肯定发生了其他事情。

标签: mongodb mongoose


【解决方案1】:

下面是:

UserModel.findOne({'username': req.body.username, 
'social.github.username': {  $ne: req.body.username}})

【讨论】:

    【解决方案2】:

    原来req.body.usernameundefined,这就是查询不起作用的原因。

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 1970-01-01
      • 2019-10-14
      • 2021-11-03
      • 1970-01-01
      • 2019-06-14
      • 2012-07-15
      • 2015-07-25
      • 2016-03-07
      相关资源
      最近更新 更多