如何使用Mongodb左连接和条件获得所有结果?

如何使用Mongodb左连接和条件获得所有结果?,mongodb,mongoose,Mongodb,Mongoose,我有两张桌子。1) roomList 2)roomUser。这里左边的桌子是房间清单。即使联接的表结果为空,也可以获取所有结果吗 我当前正在使用聚合:$lookup、$unwind、$match 房间名单 客房用户 我希望得到与此mysql查询类似的结果 从roomUser.roomId上的roomList左侧选择*加入roomUser= roomList.id,其中roomUser.userId=123,roomList.status=1 MongoDb和noSql数据库的原理与SQL数据库不

我有两张桌子。1) roomList 2)roomUser。这里左边的桌子是房间清单。即使联接的表结果为空,也可以获取所有结果吗

我当前正在使用聚合:$lookup、$unwind、$match

房间名单

客房用户

我希望得到与此mysql查询类似的结果

从roomUser.roomId上的roomList左侧选择*加入roomUser= roomList.id,其中roomUser.userId=123,roomList.status=1


MongoDb和noSql数据库的原理与SQL数据库不同

对于SQL数据库,我们应该创建两个表来存储roomlist和roomUser。 在NoSQL数据库中,您应该将用户数据存储在roomlist、特殊条目(数组、新对象)上,或者创建一个新表,以存储用户和roomlist之间的链接

之后,您应该能够进行查询


最好的祝愿

MongoDb和noSql数据库与SQL数据库的原理不同

对于SQL数据库,我们应该创建两个表来存储roomlist和roomUser。 在NoSQL数据库中,您应该将用户数据存储在roomlist、特殊条目(数组、新对象)上,或者创建一个新表,以存储用户和roomlist之间的链接

之后,您应该能够进行查询


最好的祝愿

可以通过多个聚合阶段来完成

  • $match by roomUser id
  • $lookup(按房间编号)
  • $打开找到的房间
  • $匹配状态
  • $replaceRoot用于升级房间
  • db.roomUser.aggregate([
    {
    “$match”:{
    “_id”:ObjectId(“5D3473A68041613147424AD”)
    }
    },
    {
    “$lookup”:{
    “发件人”:“房间列表”,
    “localField”:“房间id”,
    “外域”:“\u id”,
    “as”:“r”
    }
    },
    {
    “$REWIND”:“$r”
    },
    {
    “$match”:{
    “r.地位”:1
    }
    },
    {
    “$replaceRoot”:{
    “newRoot”:“$r”
    }
    }
    ])
    

    我不知道mongoose,希望你能想出mongoose等价物…

    它可以通过多个聚合阶段来完成

  • $match by roomUser id
  • $lookup(按房间编号)
  • $打开找到的房间
  • $匹配状态
  • $replaceRoot用于升级房间
  • db.roomUser.aggregate([
    {
    “$match”:{
    “_id”:ObjectId(“5D3473A68041613147424AD”)
    }
    },
    {
    “$lookup”:{
    “发件人”:“房间列表”,
    “localField”:“房间id”,
    “外域”:“\u id”,
    “as”:“r”
    }
    },
    {
    “$REWIND”:“$r”
    },
    {
    “$match”:{
    “r.地位”:1
    }
    },
    {
    “$replaceRoot”:{
    “newRoot”:“$r”
    }
    }
    ])
    

    我不认识猫鼬,希望你能想出猫鼬的等价物…

    我终于明白了,用所谓的连接来解决这个问题太难了。我用Mongoose子文档做的。现在我的收藏看起来像这样:

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    const roomUsers = new Schema (
        {
            userId: {
                type: mongoose.Schema.ObjectId,
                required: true,
                ref: "User"
            },
            joiningDate: {
                type: Date,
                trim: true
            },
            leavingDate: {
                type: Date,
                trim: true
            },
            requestDate:{
                type:Date,
                trim:true
            },
            status: {
                type: Number,
                default: 0 // 0 for none, 1 for request, 2 for joined, 3 for blocked
            }
        }
    );
    
    const roomInfo = new mongoose.Schema({
        roomName: {
            type: String,
            trim: true,
            required: true
        },
        currentMembers: {
            type: Number,
            required: true
        },
        createdBy: {
            type: mongoose.Schema.ObjectId,
            required: true,
            ref: "User"
        },
        creationDate: {
            type: Date,
            trim: true
        },
        status: {
            type: Number,
            required: true,
            default: 1 // 1 for active, 2 for deactive
        },
        roomUsers:[roomUsers]
    });
    
    var roomInfp = mongoose.model("RoomInfo", roomInfo);
    module.exports = roomInfp;
    

    我终于明白了,用所谓的连接来解决这个问题太难了。我用Mongoose子文档做的。现在我的收藏看起来像这样:

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    const roomUsers = new Schema (
        {
            userId: {
                type: mongoose.Schema.ObjectId,
                required: true,
                ref: "User"
            },
            joiningDate: {
                type: Date,
                trim: true
            },
            leavingDate: {
                type: Date,
                trim: true
            },
            requestDate:{
                type:Date,
                trim:true
            },
            status: {
                type: Number,
                default: 0 // 0 for none, 1 for request, 2 for joined, 3 for blocked
            }
        }
    );
    
    const roomInfo = new mongoose.Schema({
        roomName: {
            type: String,
            trim: true,
            required: true
        },
        currentMembers: {
            type: Number,
            required: true
        },
        createdBy: {
            type: mongoose.Schema.ObjectId,
            required: true,
            ref: "User"
        },
        creationDate: {
            type: Date,
            trim: true
        },
        status: {
            type: Number,
            required: true,
            default: 1 // 1 for active, 2 for deactive
        },
        roomUsers:[roomUsers]
    });
    
    var roomInfp = mongoose.model("RoomInfo", roomInfo);
    module.exports = roomInfp;
    

    好的,我明白你的答案。但是,如果我想将ROOMUSER collection用于我的另一个目的,比如在没有复杂逻辑的情况下从特定房间中删除几个选定的用户,那么过程会是什么呢?好的,我理解你的答案。但是,如果我想将ROOMUSER collection用于我的另一个目的,比如在没有复杂逻辑的情况下删除特定房间中的几个选定用户,那么过程会是什么呢?我终于明白了,使用所谓的JOIN来解决这个问题太难了。我用Mongoose子文档做的。现在我的收藏看起来是这样的:我终于明白了,用所谓的JOIN来解决这个问题太难了。我用Mongoose子文档做的。现在我的收藏是这样的:
    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    const roomUsers = new Schema (
        {
            userId: {
                type: mongoose.Schema.ObjectId,
                required: true,
                ref: "User"
            },
            joiningDate: {
                type: Date,
                trim: true
            },
            leavingDate: {
                type: Date,
                trim: true
            },
            requestDate:{
                type:Date,
                trim:true
            },
            status: {
                type: Number,
                default: 0 // 0 for none, 1 for request, 2 for joined, 3 for blocked
            }
        }
    );
    
    const roomInfo = new mongoose.Schema({
        roomName: {
            type: String,
            trim: true,
            required: true
        },
        currentMembers: {
            type: Number,
            required: true
        },
        createdBy: {
            type: mongoose.Schema.ObjectId,
            required: true,
            ref: "User"
        },
        creationDate: {
            type: Date,
            trim: true
        },
        status: {
            type: Number,
            required: true,
            default: 1 // 1 for active, 2 for deactive
        },
        roomUsers:[roomUsers]
    });
    
    var roomInfp = mongoose.model("RoomInfo", roomInfo);
    module.exports = roomInfp;