I currently have two Mongoose schema models set up. Let's start with the GroupSchema model:
const GroupSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
admins: {
type: String,
default: ""
},
blocked: {
type: String,
default: ""
},
createdBy: {
type: mongoose.Types.ObjectId,
required: true,
ref: 'user',
},
members: {
type: String,
default: ""
},
privacy: {
type: String,
required: true,
enum: ['public', 'private', 'deleted'],
},
})
Next, we have the GroupPostSchema model:
const GroupPostSchema = new mongoose.Schema({
user: {
type: mongoose.Types.ObjectId,
required: true,
ref: 'user',
},
text: {
type: String,
required: true
},
group: {
type: mongoose.Types.ObjectId,
required: true,
ref: 'group',
},
image: {
type: String
}
})
When trying to execute a query like this:
var search = {
"group.privacy": "public"
}
GroupPostSchema.find(search).exec((err, data) => {
// handling the result here
})
The result is returning an empty array []. I came across a helpful answer on Stack Overflow regarding nested schema queries: Mongoose query for nested schema
However, I am looking to maintain GroupPostSchema.group
as an Object rather than an Array. How can this be achieved in the simplest way possible?