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