【发布时间】:2017-09-02 07:35:15
【问题描述】:
我的帖子架构如下所示。
const Post = new Schema({
author: String,
view: Number,
point: Number,
title: String,
images: [Schema.Types.Mixed],
content: String,
tags: [String],
comments: [{ displayName: String, body: String, createdAt: Date }],
createdAt: {
type: Date,
default: Date.now
},
editedAt: Date
})
我的查询是:
Post.statics.findByTags = function(tags) {
return this.find({
tags: { $in: tags }
}).
limit(10).
sort({ createdAt: -1 }).
exec()
}
我要做的是匹配具有参数“tags”提供的标签之一的帖子。
ex) 如果标签等于 ["tag1", "tag2"] 和
Post1 = { ...一些值,标签:[“tag1”]}, Post2 = { ...一些值,标签:[“tag1”,“tag2”]}
我想匹配两个帖子。
我正在使用 Postman 来测试这些查询,现在除了一个空数组我什么都没有。
【问题讨论】:
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及必要的最短代码 重现它在问题本身。没有明确的问题陈述的问题对其他读者没有用处。见:How to create a Minimal, Complete, and Verifiable example.
-
那么“MCVE”是什么意思?这意味着您应该验证您的收藏实际上被称为“帖子”just as mongoose is expecting,并且您确实连接到正确的数据库空间(我们需要看到它是“可验证的”)以及您对所示方法的使用(即称为“完整”)以其他人可以复制的方式显示。或者实际上通过执行这些步骤,您实际上意识到自己做错了什么。