【问题标题】:mongoose mongodb query findmongoose mongodb 查询查找
【发布时间】:2017-09-11 19:30:35
【问题描述】:

我是 mongo 和 mongoose 的新手。

我想在关系数据库中做一个简单的查询,但我在 mongo 中遇到了很多问题

这是我的架构:

const GroupSchema = new Schema({
    name: { type: String , required:true},
    image: { type: String , required:true},
    location : {type:String , required: true},
    locationCode: {type:String , required:true },
    created: {type:Date, default:Date.now() , required:true },
    createdBy: {type: Schema.Types.ObjectId , ref:'User' , required:true},
    //category: [{type:String,enum:[ config.TYPES ]}],
    pendingUsers: [
        {   text:String, 
            user:{type: Schema.Types.ObjectId , ref: 'User'}
        }
    ],
    rejectedUsers: [ {type: Schema.Types.ObjectId , ref: 'User'} ],
    users: [ {type: Schema.Types.ObjectId , ref: 'User' , required:true } ],
    adminUsers:[{type:Schema.Types.ObjectId , ref:'User', required:true}],
    events :[Event],
    activity:[Activity]
})

我的控制器文件我想做以下查询:

let groupId = '123123hgvhgj
let userId = 'asdfsadf3434

Group.find()
    .where('_id').equals(groupId)
    .where('pendingUsers.user')
    .in(userId)
.where('users')
    .in(userId)
.where('adminUsers')
    .in(userId)
.where('rejectedUsers')
    .in(userId)
    .exec(function (err, records) {
        //make magic happen
        console.log(records)
    });

我必须获取记录 WHERE _id 与组 id 匹配并且(userId 存在于 pendingUsers 中或 userid 存在于 deniedUsers 中或 userid 存在于用户中或 userid 存在于 adminUsers 中)

我知道这似乎是一个简单的查询,但在应该返回记录时返回空,我的查询有问题?

谢谢

【问题讨论】:

    标签: javascript arrays node.js mongodb


    【解决方案1】:

    即使猫鼬似乎支持一些简单的和+或操作(docs) 在我看来,您似乎仍然需要将一些纯 mongodb 查询混入其中。

    考虑到这一点,我会使用纯 mongodb 样式的查询。这个应该适合您的需求(未经测试)或至少会为您指明正确的方向:

    Group.find({
        $and: [
            {_id: groupId},
            {
                $or: [
                    { pendingUsers.user: { $elemMatch: {userId} } },
                    { rejectedUsers: { $elemMatch: {userId} } },
                    { users: { $elemMatch: {userId} } },
                    { adminUsers: { $elemMatch: {userId} } },
                ]
            }
        ]
    });
    

    【讨论】:

    • 谢谢,我得稍微改一下:Group.find({ $and: [ { _id: groupId}, { $or: [ { "pendingUsers": { $elemMatch: { "user": userId } } }, //{ 'pendingUsers.user' : { $elemMatch: {userId} } }, { rejectedUsers: { $elemMatch: {userId} } }, { users: { $elemMatch: {userId} } }, { adminUsers: { $elemMatch: {userId} } }, ] } ] })
    【解决方案2】:
    Group.find({
            $and: [
                { _id: groupId},
                {
                    $or: [
                        { "pendingUsers": { $elemMatch: { "user": userId } } },
                        //{ 'pendingUsers.user' : { $elemMatch: {userId} } },
                        { rejectedUsers: { $elemMatch: {userId} } },
                        { users: { $elemMatch: {userId} } },
                        { adminUsers: { $elemMatch: {userId} } },
                    ]
                }
            ]
        })
    

    【讨论】:

      猜你喜欢
      • 2014-10-10
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 2021-03-03
      • 2015-06-24
      相关资源
      最近更新 更多