【问题标题】:Mongo find users that are not blockedMongo 查找未被屏蔽的用户
【发布时间】:2017-08-01 20:07:55
【问题描述】:

我在 Mongo 中有一个用户数据库。我想检索不在我被阻止的数组中的所有用户。试图通过检索所有文档来过滤结果,但我想知道这是否是最有效的方法。 Mongo 有没有办法找到不属于数组的所有用户?

这是我的用户模型:

var UserSchema = mongoose.Schema({
    username: {
        type: String,
        index:true, 
        unique: true
    },
    password: {
        type: String
    },
    email: {
        type: String,
        unique: true
    },
    name: {
        type: String
    },
    latitude:{
        type: String
    },
    longitude:{
        type: String
    },
    blocked:{
        type: Array
    },
    friends:{
        type: Array
    },
    photo:{
        type: String
    },
    identity:{
        type: String
    },
    age:{
        type: String
    },
    herefor:{
        type: String
    },
    conns:{
        type: Number
    },
    status:{
        type: String
    }
}, { collection: 'users' });

【问题讨论】:

  • 不清楚你在问什么。您可能想要$nin,但您还应该阅读所有query operators 的一般用途,并了解在哪里应用它们。
  • 你试过$nin吗?

标签: mongodb mongoose


【解决方案1】:

假设您的被阻止用户数组包含用户名并且您的 mongoose 模型称为 UserModel,您可能正在寻找类似以下内容:

var blockedUsernamesArray = [];

UserModel.find({ username: { $nin: blockedUsernamesArray } }, function(err, docs) {
    // Handle result
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多