multi condition in $match mongodb aggregate framework _ mongoose [duplicate]
up vote
0
down vote
favorite
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.
node.js mongodb mongoose aggregation-framework
marked as duplicate by Anthony Winzlet, Neil Lunn
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 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.
add a comment |
up vote
0
down vote
favorite
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.
node.js mongodb mongoose aggregation-framework
marked as duplicate by Anthony Winzlet, Neil Lunn
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 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 thetagId
– Anthony Winzlet
Nov 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 at 9:08
@AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}
– babak abadkheir
Nov 18 at 9:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
node.js mongodb mongoose aggregation-framework
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
node.js mongodb mongoose aggregation-framework
edited Nov 18 at 9:26
asked Nov 18 at 8:32
babak abadkheir
446311
446311
marked as duplicate by Anthony Winzlet, Neil Lunn
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 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
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 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 thetagId
– Anthony Winzlet
Nov 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 at 9:08
@AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}
– babak abadkheir
Nov 18 at 9:13
add a comment |
You need to cast your other id to mongoose objectId. Just you did with thetagId
– Anthony Winzlet
Nov 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 at 9:08
@AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}
– babak abadkheir
Nov 18 at 9:13
You need to cast your other id to mongoose objectId. Just you did with the
tagId
– Anthony Winzlet
Nov 18 at 9:01
You need to cast your other id to mongoose objectId. Just you did with the
tagId
– Anthony Winzlet
Nov 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 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 at 9:08
@AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}
– babak abadkheir
Nov 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 at 9:13
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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 hadreportedBy
andowner
in both scenarios so it seems to me some refactoring would not hurt.
owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}
– babak abadkheir
Nov 18 at 9:56
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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 hadreportedBy
andowner
in both scenarios so it seems to me some refactoring would not hurt.
owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}
– babak abadkheir
Nov 18 at 9:56
add a comment |
up vote
1
down vote
accepted
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 hadreportedBy
andowner
in both scenarios so it seems to me some refactoring would not hurt.
owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}
– babak abadkheir
Nov 18 at 9:56
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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 hadreportedBy
andowner
in both scenarios so it seems to me some refactoring would not hurt.
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 hadreportedBy
andowner
in both scenarios so it seems to me some refactoring would not hurt.
edited Nov 18 at 9:43
answered Nov 18 at 9:38
Akrion
7,14611222
7,14611222
owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}
– babak abadkheir
Nov 18 at 9:56
add a comment |
owner: {'$nin': userblocks} this works fine .but this is not working: reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)}
– babak abadkheir
Nov 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 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 at 9:56
add a comment |
You need to cast your other id to mongoose objectId. Just you did with the
tagId
– Anthony Winzlet
Nov 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 at 9:08
@AnthonyWinzlet and I tried this to: {reportedBy:{ '$ne': mongoose.Types.ObjectId(req.user._id)},owner: {'$ne': userblocks}}
– babak abadkheir
Nov 18 at 9:13