简体   繁体   中英

Query multiple collections Nodejs

Hello im trying to join these collections i want to get all users which has "active" attribute equal to false. I couldn't figure out how to acquire this query. There are my schemas:

User Schema

const mongoose = require('mongoose');
    const Schema = mongoose.Schema;

    const UserSchema = new Schema({
        name: {
            type: String,
            required: true
        },
        email: {
            type: String,
            required: true
        },
        password: {
            type: String,
            required: true
        },
        type: {
            type: String,
            required: true
        },
        active:{
            type:Boolean
        }
    });

    module.exports = mongoose.model('users', UserSchema);

Company Schema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

        const CompanySchema = new Schema({
            userId: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'users'
            },
            companies: [{
                name: {
                    type:String
                },
                country:{
                    type:String
                }
            }
            ]
        });

        module.exports = Company = mongoose.model('company', CompanySchema);

Note: Not all users have companies only the type "client" and i want to get both, "client" and "employe"

You may want to refactor your Schema to better accommodate the type of data you have available.

For example:

User Schema:

const UserSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    type: {
        type: String,
        required: true
    },
    active:{
        type:Boolean
    },
    companies: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'company'
    }]        
});

And Company Schema:

const CompanySchema = new Schema({
    name: {
        type:String
    },
    country:{
        type:String
    }
});

Then to get a list of all users who are active, and automatically populate any company data for those users (Assuming your user model is called UserModel)

UserModel.find({ active: false }).populate('companies').exec();

If you are unable to edit your data structure for any reason, then you could perform a query similar to:

CompanyModel.aggregate([
    { $lookup: { from: 'users', localField: 'userId', foreignField: '_id', as: 'user' } },
    { $match: { '$user.active': false } }
]).exec()

This will perform an aggregate lookup on the UserId field and then only match on ones where the active property is set to false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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