【发布时间】:2017-12-31 21:11:45
【问题描述】:
当我有一段关系时,我很难找到在猫鼬中查询某些内容的正确方法。
基本上我有一个带有 ObjectId 的文档与另一个文档相关(如下所示)。
但是当我尝试过滤引用的属性时,没有任何效果。 基本上,问题出在这一行 ".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);
});
});
有人有正确的方法解决吗?
【问题讨论】: