2
我使用mongoose
和定义在我的应用程序的注释部分的模型:如何修改我的mongodb查询,以便结果不包含存储在数组中的特定值?
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CommentSchema = new Schema({
my_username: {type: String, required: true},
text_content: {type: String},
hashtags: {type: [String]},
photo_content: {type: String},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
现在,当我下载属于特定的时间框架,包含主题标签的所有意见,我有:
var hashtagsInput = req.body.hashtags;
var query= {};
query.$and = [];
if(startDate != undefined && endDate != undefined) {
var startDate = new Date(req.param('startDate'));
var endDate = new Date(req.param('endDate'));
query.$and.push({"created_at":{$gte: startDate}});
query.$and.push({"created_at":{$lte: endDate}});
}
if (hashtagsInput != undefined) {
var hashtags = hashtagsInput.split(",");
query.$and.push({"hashtags":{$in: hashtags}});
}
现在,我想另一种情况添加到查询 - 除了主题标签,我路过阻止用户的数组的数组:
var blockedUsers = req.body.blockedUsers;
我想避免每个评论的作者都等于blockedUsers中的任何条目。所以基本上我想要有not in
查询。你能给我一些关于如何在查询中构造这个附加条件的提示吗?
是的,我必须以与标签类似的方式拆分它,您的解决方案看起来不错,谢谢! – randomuser1