问题描述
当用户向我的node.js
应用程序提交内容时,他会放置四件事:start_date,他的facebook用户名,文本内容和一个标志,该内容对所有人还是仅对他的facebook朋友可见.
When user submits a content into my node.js
app, he puts four things: start_date, his facebook username, text content and a flag, whether the content should be visible to everyone or only to his facebook friends.
当用户获取内容时,他可以从其他用户那里获取公开内容,也可以从他的facebook朋友那里获取内容(即使他们仅为他们的朋友提交内容).
When user fetches content, he can get the public content from other users and the content from his facebook friends (even when they submitted their content only for their friends).
我创建了一个mongoose
查询,该查询在这种情况下可以正常工作.我是这样的:
I created a mongoose
query that works correctly in this scenario. I do it like this:
var query = Test.find({})
if(startDate != undefined) {
var startDate = new Date(req.param('startDate'));
query = query.where('updated_at').gte(startDate);
}
if (friends != undefined) {
var friendsSplitted = friends.split(",");
for(var i=0; i<friendsSplitted.length; i++) {
query = query.or([{ 'facebook_username': friendsSplitted[i] }]);
}
}
query = query.where('hidden').equals(false);
if (publicFlag != undefined && publicFlag === "true") {
query = query.or({friends_only: false});
}
使用上面的代码,当用户仅从朋友(可能是私人的或公共的)中查询内容时,他的POST
的startDate
,其friends
的数组,标志hidden
设置为false,并且publicFlag
设置为false.构造查询:
With that code above, when user queries only for content from his friends (that might be private or public), he POST
s startDate
, an array of his friends
, a flag hidden
set to false and publicFlag
set to false. A query is constructed:
用户还不仅可以查询他的朋友(私人或公共)内容,还可以查询其他所有人的公共内容.为此,他POST
s startDate
是他的friends
的数组,标志hidden
设置为false,publicFlag
设置为true.构造查询:
User can also query not only his friends (private or public) content, but also public content of everyone else. For that, he POST
s startDate
, an array of his friends
, a flag hidden
set to false and publicFlag
set to true. A query is constructed:
上面的代码可以正常工作.
Above code works fine.
我想添加另一种情况,即用户可以获取带有特定主题标签的内容.基本上,当用户选择此选项时,他可以看到其他用户的仅包含这些标签的内容.不过,该内容是他的fb朋友帖子(私人或公开)和其他人的帖子(公开).
I want to add another case, when user can fetch content with specific hashtags.Basically, when user selects this option, he can see other users' content that only includes those hashtags. That content though is his fb friends posts (private or public) and other people's posts (that is public).
为此,我考虑将这种条件添加到我的原始node.js
代码中:
For that I thought about adding this condition to my original node.js
code:
if (hashtagsInput != undefined) {
var hashtags = hashtagsInput.split(",");
for(var i=0; i<hashtags.length; i++) {
query = query.or([{ 'hashtags': hashtags[i] }]);
}
}
但是此解决方案无法正常工作.
but this solution does not work properly.
当用户想要获取其朋友的(私人和公共)内容以及其他人的公共内容(但仅包含一个标签之一)时,他将POST
s startDate
(他的friends
的数组) ,将标志hidden
设置为false,将publicFlag
设置为true,以及其hashtags
的数组.当他这样做时,它会创建一个查询:
When user wants to fetch (private and public) content of his friends and public content of others - but only that one that contains one of the hashtags - he POST
s startDate
, an array of his friends
, a flag hidden
set to false, publicFlag
set to true and an array of his hashtags
. When he does it, it creates a query:
基本上,我只想将常规查询仅限于特定的主题标签.
Basically I want to limit the general query only to specific hashtags.
此查询未返回正确的数据.现在我不是mongoose
专家,昨天我花了几个小时试图弄清楚,但我认为问题在于它返回的数据要么包含其中一个标签,要么其作者是我的标签之一.脸谱网朋友.
This query does not return correct data. Now I'm not a mongoose
specialist and I've spent couple hours yesterday of trying to figure it out, but I think the problem is that it returns the data that either contains one of the hashtags OR its author is one of my facebook friend.
我想修复此查询,以便在用户POST
使用#标签的情况下–它获取对他的朋友和所有人均公开和不公开的内容,但仅获取其中至少包含其中一个的内容主题标签.而且,当用户不提供主题标签时,它应按原样工作,以获取他的朋友和所有人的所有私有和公开内容.
I would like to fix this query so that - in case user POST
s hashtags - it fetches the content that is public and private of his friends and public of everyone, but only that one that contains at least one of those hashtags. And when user does not provide the hashtags - it should work as it was, fetching all private and public content of his friends and public of everyone.
能帮我解决我的node.js
代码,以便它创建正确的mongoose
查询吗?
Can you please help me fix my node.js
code so that it creates a correct mongoose
query?
推荐答案
下面的代码将根据需要工作
The below code will work as you want
var query= {};
query.$and = [];
// and condition on start date
if(startDate != undefined) {
var startDate = new Date(req.param('startDate'));
query.$and.push({"updated_at":{$gte: startDate}});
}
// and condition on hastags
if (hashtagsInput != undefined) {
var hashtags = hashtagsInput.split(",");
query.$and.push({"hashtags":{$in: hashtags}});
}
// and condition on hidden
query.$and.push({"hidden":false});
// creating a OR condition for facebook friends and public flag
var friend_query = {};
friend_query.$or = [];
if (friends != undefined) {
var friendsSplitted = friends.split(",");
friend_query.$or.push({"facebook_username":{$in: friendsSplitted}});
}
if (publicFlag != undefined && publicFlag === "true") {
friend_query.$or.push({friends_only: false});
}
//Merging facebook friend condition with other condition with AND operator.
query.$and.push(friend_query);
var finalquery = Test.find(query)
对不起,我使用Json格式直接创建mongodb查询,与您所做的没有什么不同.如您提到的那样,这将起作用.
Sorry I use to create mongodb query directly in Json format, it is little different than you are doing. This will work as you mentioned you want in question.
如果不能解决您的问题,请告诉我.
If it don't solve your problem tell me.
这篇关于我的mongodb/mongoose查询条件将其排除在外.你能帮我解决这个问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!