I'm having some hard times trying to discovery the right way to query something in the mongoose when I have a relationship.
Basically I have one document with ObjectId relating another document (as you can see bellow).
But When I try to filter a property of the reference, nothing works anymore. Basically, the problem is this line ".where({ "Recipe.Title": new RegExp("*") })"
// const configs
const config = require('./config');
// mongodb setup
const mongoose = require('mongoose');
mongoose.connect(config.database);
var Schema = mongoose.Schema
// recipe schema
const RecipeSchema = mongoose.Schema({
Title: { type: String },
Description: { type: String },
Complaints: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Complaint' }],
});
const Recipe = mongoose.model('Recipe', RecipeSchema);
// complaint schema
const ComplaintSchema = mongoose.Schema({
Recipe : { type: mongoose.Schema.Types.ObjectId, ref: 'Recipe' },
Message: { type: String }
});
const Complaint = mongoose.model('Complaint', ComplaintSchema);
/*
after inserting some items
*/
Complaint
.find()
.populate("Recipe")
.where({ "Recipe.Title": new RegExp("*") }) // this is not working!
.exec((error, items) => {
items.map((item) => {
console.log(item);
});
});
Does someone have the correct way to solve it?