I have a groups collection, which has the following fields:
- A "users" array field, this field of type array contains the ids of the "users" from the User schema (defined below)
- A subgroups array field, this field of type array contains ids of groups that are part of this group. There can be n-th level of nesting for subgroups.
Is there anyway I can run a query on the groups collection that will search for a user _id that either exists in the "users" field in the groups collection or in the subgroups.
// Group schema
"use strict";
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const groupSchema = new Schema(
{
name: {
type: String,
unique: true
},
users: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
],
subgroups: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Group"
}
]
},
{ timestamps: true }
);
module.exports = groupSchema;
// User schema
const userSchema = new Schema(
{
name: {
type: String
}
},
{ timestamps: true }
);
module.exports = userSchema;