0

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"

2
  • can you give an example of your two collections and how will be the output? Commented Aug 22, 2019 at 20:35
  • Looks like you're building a total relational application. You probably needs a relational database instead. Commented Jul 26, 2021 at 15:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.