简体   繁体   English

猫鼬mongodb查询找到

[英]mongoose mongodb query find

i'm new in mongo and mongoose. 我是mongo和mongoose的新手。

i want to do a simple query in relational database but i have strong problems to do in mongo 我想在关系数据库中做一个简单的查询,但是我在mongo中有很强的问题要做

here is me schema: 这是我的架构:

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]
})

and i my controller file i want to do the following query: 和我我的控制器文件,我想做以下查询:

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)
    });

i have to get the record WHERE _id match with a group id AND (userId exists in pendingUsers OR userid exists in rejectedUsers OR userid exists in users OR userid exists in adminUsers ) 我必须获取记录,其中_id与组ID匹配且(userId存在于pendingUsers中或userid存在于rejectedUsers中或userid存在于用户中或userid存在于adminUsers中)

i know that seems to be a simple query but returns empty when should be returned the record i have something wrong in the query? 我知道这似乎是一个简单的查询,但在应返回记录时返回空值我在查询中有问题吗?

thanks 谢谢

Even if mongoose seems to support some simple and + or operations ( docs ) it seems to me as if you would still need to mix some pure mongodb query into it. 即使mongoose似乎支持某些简单的+或操作( docs ),在我看来,您似乎仍然需要将一些纯mongodb查询混入其中。

Considering that, i would go with a pure mongodb style query. 考虑到这一点,我将使用纯mongodb样式查询。 This one should fit your needs(untested) or will at least point you in the right direction: 这应该适合您的需求(未经测试),或者至少会为您指明正确的方向:

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} } },
                ]
            }
        ]
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM