multi condition in $match mongodb aggregate framework _ mongoose [duplicate]












0
















This question already has an answer here:




  • Moongoose aggregate $match does not match id's

    1 answer




I have multiple conditions for the query in my controller that I need path if exists.



condition 1 :



{ tags: mongoose.Types.ObjectId(req.params.tagId)}


condition 2:



{ reportedBy: { '$ne': req.user._id }} // if this video object reported dont show


condition 3:



{ owner: { '$ne': userblocks } } // userblocks is a array of objectIds


so this is my $match filter:



{
'$match':
_.isString(req.params.tagId) ?
{ tags: mongoose.Types.ObjectId(req.params.tagId),
reportedBy:{ '$ne': req.user._id}, owner: { '$ne': userblocks}}:
{ reportedBy:{ '$ne': req.user._id},owner: {'$ne': userblocks}}
},


I used ... spread operator if tagId passed to params.this condition works for tagId but other conditions are not working.



with @Anthony Winzlet hint I tried to :



{reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}} 


and userblocks is a list of objects, I checked the type of them and they are objects too.so no need to cast them to objectIds.










share|improve this question















marked as duplicate by Anthony Winzlet, Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 '18 at 21:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You need to cast your other id to mongoose objectId. Just you did with the tagId

    – Anthony Winzlet
    Nov 18 '18 at 9:01













  • @AnthonyWinzlet in my user blocks all of them are objects not string. let userblocks = [...req.user.blockedUser, ...req.user.blockedBy].map(blocks=>mongoose.Types.ObjectId(blocks));

    – babak abadkheir
    Nov 18 '18 at 9:08













  • @AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}

    – babak abadkheir
    Nov 18 '18 at 9:13
















0
















This question already has an answer here:




  • Moongoose aggregate $match does not match id's

    1 answer




I have multiple conditions for the query in my controller that I need path if exists.



condition 1 :



{ tags: mongoose.Types.ObjectId(req.params.tagId)}


condition 2:



{ reportedBy: { '$ne': req.user._id }} // if this video object reported dont show


condition 3:



{ owner: { '$ne': userblocks } } // userblocks is a array of objectIds


so this is my $match filter:



{
'$match':
_.isString(req.params.tagId) ?
{ tags: mongoose.Types.ObjectId(req.params.tagId),
reportedBy:{ '$ne': req.user._id}, owner: { '$ne': userblocks}}:
{ reportedBy:{ '$ne': req.user._id},owner: {'$ne': userblocks}}
},


I used ... spread operator if tagId passed to params.this condition works for tagId but other conditions are not working.



with @Anthony Winzlet hint I tried to :



{reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}} 


and userblocks is a list of objects, I checked the type of them and they are objects too.so no need to cast them to objectIds.










share|improve this question















marked as duplicate by Anthony Winzlet, Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 '18 at 21:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You need to cast your other id to mongoose objectId. Just you did with the tagId

    – Anthony Winzlet
    Nov 18 '18 at 9:01













  • @AnthonyWinzlet in my user blocks all of them are objects not string. let userblocks = [...req.user.blockedUser, ...req.user.blockedBy].map(blocks=>mongoose.Types.ObjectId(blocks));

    – babak abadkheir
    Nov 18 '18 at 9:08













  • @AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}

    – babak abadkheir
    Nov 18 '18 at 9:13














0












0








0









This question already has an answer here:




  • Moongoose aggregate $match does not match id's

    1 answer




I have multiple conditions for the query in my controller that I need path if exists.



condition 1 :



{ tags: mongoose.Types.ObjectId(req.params.tagId)}


condition 2:



{ reportedBy: { '$ne': req.user._id }} // if this video object reported dont show


condition 3:



{ owner: { '$ne': userblocks } } // userblocks is a array of objectIds


so this is my $match filter:



{
'$match':
_.isString(req.params.tagId) ?
{ tags: mongoose.Types.ObjectId(req.params.tagId),
reportedBy:{ '$ne': req.user._id}, owner: { '$ne': userblocks}}:
{ reportedBy:{ '$ne': req.user._id},owner: {'$ne': userblocks}}
},


I used ... spread operator if tagId passed to params.this condition works for tagId but other conditions are not working.



with @Anthony Winzlet hint I tried to :



{reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}} 


and userblocks is a list of objects, I checked the type of them and they are objects too.so no need to cast them to objectIds.










share|improve this question

















This question already has an answer here:




  • Moongoose aggregate $match does not match id's

    1 answer




I have multiple conditions for the query in my controller that I need path if exists.



condition 1 :



{ tags: mongoose.Types.ObjectId(req.params.tagId)}


condition 2:



{ reportedBy: { '$ne': req.user._id }} // if this video object reported dont show


condition 3:



{ owner: { '$ne': userblocks } } // userblocks is a array of objectIds


so this is my $match filter:



{
'$match':
_.isString(req.params.tagId) ?
{ tags: mongoose.Types.ObjectId(req.params.tagId),
reportedBy:{ '$ne': req.user._id}, owner: { '$ne': userblocks}}:
{ reportedBy:{ '$ne': req.user._id},owner: {'$ne': userblocks}}
},


I used ... spread operator if tagId passed to params.this condition works for tagId but other conditions are not working.



with @Anthony Winzlet hint I tried to :



{reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}} 


and userblocks is a list of objects, I checked the type of them and they are objects too.so no need to cast them to objectIds.





This question already has an answer here:




  • Moongoose aggregate $match does not match id's

    1 answer








node.js mongodb mongoose aggregation-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 9:26







babak abadkheir

















asked Nov 18 '18 at 8:32









babak abadkheirbabak abadkheir

462311




462311




marked as duplicate by Anthony Winzlet, Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 '18 at 21:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Anthony Winzlet, Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 '18 at 21:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You need to cast your other id to mongoose objectId. Just you did with the tagId

    – Anthony Winzlet
    Nov 18 '18 at 9:01













  • @AnthonyWinzlet in my user blocks all of them are objects not string. let userblocks = [...req.user.blockedUser, ...req.user.blockedBy].map(blocks=>mongoose.Types.ObjectId(blocks));

    – babak abadkheir
    Nov 18 '18 at 9:08













  • @AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}

    – babak abadkheir
    Nov 18 '18 at 9:13



















  • You need to cast your other id to mongoose objectId. Just you did with the tagId

    – Anthony Winzlet
    Nov 18 '18 at 9:01













  • @AnthonyWinzlet in my user blocks all of them are objects not string. let userblocks = [...req.user.blockedUser, ...req.user.blockedBy].map(blocks=>mongoose.Types.ObjectId(blocks));

    – babak abadkheir
    Nov 18 '18 at 9:08













  • @AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}

    – babak abadkheir
    Nov 18 '18 at 9:13

















You need to cast your other id to mongoose objectId. Just you did with the tagId

– Anthony Winzlet
Nov 18 '18 at 9:01







You need to cast your other id to mongoose objectId. Just you did with the tagId

– Anthony Winzlet
Nov 18 '18 at 9:01















@AnthonyWinzlet in my user blocks all of them are objects not string. let userblocks = [...req.user.blockedUser, ...req.user.blockedBy].map(blocks=>mongoose.Types.ObjectId(blocks));

– babak abadkheir
Nov 18 '18 at 9:08







@AnthonyWinzlet in my user blocks all of them are objects not string. let userblocks = [...req.user.blockedUser, ...req.user.blockedBy].map(blocks=>mongoose.Types.ObjectId(blocks));

– babak abadkheir
Nov 18 '18 at 9:08















@AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}

– babak abadkheir
Nov 18 '18 at 9:13





@AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}

– babak abadkheir
Nov 18 '18 at 9:13












1 Answer
1






active

oldest

votes


















1














Try something like this:



let $match = {
reportedBy: { '$ne': mongoose.Types.ObjectId(req.user._id) },
owner: { '$nin': userblocks } // $nin when comparing against array of object ids
},

if(_.isString(req.params.tagId)) {
$match.tags = mongoose.Types.ObjectId(req.params.tagId)
}


Then just use $match in your aggregation pipeline or as part of whatever else parts of the pipeline you would have.



Things to note:




  • When comparing to _id mongoose.Types.ObjectId function should be used.

  • When comparing against an array $in or $nin are usually what you would want to use.

  • It seems in your _.isSting check logic you had reportedBy and owner in both scenarios so it seems to me some refactoring would not hurt.






share|improve this answer


























  • owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}

    – babak abadkheir
    Nov 18 '18 at 9:56


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try something like this:



let $match = {
reportedBy: { '$ne': mongoose.Types.ObjectId(req.user._id) },
owner: { '$nin': userblocks } // $nin when comparing against array of object ids
},

if(_.isString(req.params.tagId)) {
$match.tags = mongoose.Types.ObjectId(req.params.tagId)
}


Then just use $match in your aggregation pipeline or as part of whatever else parts of the pipeline you would have.



Things to note:




  • When comparing to _id mongoose.Types.ObjectId function should be used.

  • When comparing against an array $in or $nin are usually what you would want to use.

  • It seems in your _.isSting check logic you had reportedBy and owner in both scenarios so it seems to me some refactoring would not hurt.






share|improve this answer


























  • owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}

    – babak abadkheir
    Nov 18 '18 at 9:56
















1














Try something like this:



let $match = {
reportedBy: { '$ne': mongoose.Types.ObjectId(req.user._id) },
owner: { '$nin': userblocks } // $nin when comparing against array of object ids
},

if(_.isString(req.params.tagId)) {
$match.tags = mongoose.Types.ObjectId(req.params.tagId)
}


Then just use $match in your aggregation pipeline or as part of whatever else parts of the pipeline you would have.



Things to note:




  • When comparing to _id mongoose.Types.ObjectId function should be used.

  • When comparing against an array $in or $nin are usually what you would want to use.

  • It seems in your _.isSting check logic you had reportedBy and owner in both scenarios so it seems to me some refactoring would not hurt.






share|improve this answer


























  • owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}

    – babak abadkheir
    Nov 18 '18 at 9:56














1












1








1







Try something like this:



let $match = {
reportedBy: { '$ne': mongoose.Types.ObjectId(req.user._id) },
owner: { '$nin': userblocks } // $nin when comparing against array of object ids
},

if(_.isString(req.params.tagId)) {
$match.tags = mongoose.Types.ObjectId(req.params.tagId)
}


Then just use $match in your aggregation pipeline or as part of whatever else parts of the pipeline you would have.



Things to note:




  • When comparing to _id mongoose.Types.ObjectId function should be used.

  • When comparing against an array $in or $nin are usually what you would want to use.

  • It seems in your _.isSting check logic you had reportedBy and owner in both scenarios so it seems to me some refactoring would not hurt.






share|improve this answer















Try something like this:



let $match = {
reportedBy: { '$ne': mongoose.Types.ObjectId(req.user._id) },
owner: { '$nin': userblocks } // $nin when comparing against array of object ids
},

if(_.isString(req.params.tagId)) {
$match.tags = mongoose.Types.ObjectId(req.params.tagId)
}


Then just use $match in your aggregation pipeline or as part of whatever else parts of the pipeline you would have.



Things to note:




  • When comparing to _id mongoose.Types.ObjectId function should be used.

  • When comparing against an array $in or $nin are usually what you would want to use.

  • It seems in your _.isSting check logic you had reportedBy and owner in both scenarios so it seems to me some refactoring would not hurt.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 '18 at 9:43

























answered Nov 18 '18 at 9:38









AkrionAkrion

9,43711224




9,43711224













  • owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}

    – babak abadkheir
    Nov 18 '18 at 9:56



















  • owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}

    – babak abadkheir
    Nov 18 '18 at 9:56

















owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}

– babak abadkheir
Nov 18 '18 at 9:56





owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}

– babak abadkheir
Nov 18 '18 at 9:56



這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini