Jay*_*ayC 12 mongoose node.js express
我正在尝试根据属性子文档从我的数据库中获取文档列表.我正在使用的模型和模式是:
var elementSchema = new mongoose.Schema({
name: String,
description: String,
_story: { type: mongoose.Schema.Types.ObjectId, ref: 'Story' },
date_created: Date,
date_modified: Date,
};
var storySchema = new mongoose.Schema({
title: {type: String, default: '', trim: true},
isPrivate: {type: Boolean, default: false},
});
mongoose.model("Story", storySchema);
mongoose.model("Element", elementSchema);
Run Code Online (Sandbox Code Playgroud)
我试图让属于一个故事,是不是私人所有元素,并根据一些帖子,我看到在这里(1,2,3)解决方案是使用_story.isPrivate与发现.我现在正在这样做:
Element.find({'_story.isPrivate': false})
.populate('_story')
.exec(function(err, elements){
if(err){
return next(err);
}
else if(elements.length > 0){
return res.send(elements);
}
else{
return res.send(404, {message: "No elements found"});
}
});
Run Code Online (Sandbox Code Playgroud)
但结果总是一个空集(返回404).没有条件,find返回所有元素并正确填充_story.我还激活了调试输出以查看正在执行的查询,我得到了这个:
Mongoose: elements.find({ '_story.isPrivate': false }) { fields: undefined, safe: undefined }
Run Code Online (Sandbox Code Playgroud)
试图在MongoDB中执行它我得不到任何结果.这可能有什么不对?
谢谢
Pet*_*ons 10
听@JohnnyHK.他说的是实话.Mongodb查询一次只使用一个且只有一个集合中的数据.由于'elements'集合中的文档没有_story.isPrivate密钥路径,Element.find({'_story.isPrivate': false})因此永远不会匹配任何文档.mongodb中没有联接.真.但是,考虑到"无连接"约束,仍然可以构建应用程序并满足用例,但是您需要替代模式和查询设计.有时人们会对数据进行反规范化并复制内容.有时您运行多个相关查询等.
| 归档时间: |
|
| 查看次数: |
9407 次 |
| 最近记录: |