how to join two collection with multiple condition from two collection in mongodb? [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • How do I perform the SQL Join equivalent in MongoDB?

    19 answers



  • Aggregation filter after $lookup

    1 answer




how to give condition from second collection. i gave condition from first collection but also need to give condition from second collection asset_type.asset_type_name like search_key



if(search_key!=''){

let query_string = "/^.*" + search_key + ".*$/i";

dbo.collection("assets").aggregate([
{
"$match": { $and: [ { status: 1 }, { $or: [ { maker_name: { $regex: `${search_key}.*`, $options: 'i' } }, { serial_number: { $regex: `${search_key}.*`, $options: 'i' } } ] } ] }
},
{
$lookup:
{
from: 'asset_type',
localField: 'asset_type',
foreignField: 'asset_type_id',
as: 'asset_type_details'
}
}
]).sort({_id:-1}).toArray(function(err, result) {
if (err){
res.status(200).json({'return_data': err });
}else{
res.status(200).json({'return_data': result });
}

db.close();
});

}









share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb 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 15 at 5:54


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.















  • Please also see Is “How do I convert code from this language to this language” too broad?. Questions like this are off-topic. There are plenty of "general examples" for how to join with MongoDB. If you have a specific question, then it requires specific details of your structure in MongoDB, intended usage and some attempt at resolving this yourself. Ideally, you should not need a JOIN at all, since you knew enough to realize you should be remodeling your data for this completely different engine.
    – Neil Lunn
    Nov 15 at 6:02










  • This edit isn't much better. It's completely unclear what you are even asking and as such we cannot reopen from a duplicate when there's no clear question here since that's another reason to put the question on hold. I suggest at the very least looking at the additional duplicate and fixing up at least the things that would throw errors in the code.
    – Neil Lunn
    Nov 15 at 6:41















up vote
0
down vote

favorite













This question already has an answer here:




  • How do I perform the SQL Join equivalent in MongoDB?

    19 answers



  • Aggregation filter after $lookup

    1 answer




how to give condition from second collection. i gave condition from first collection but also need to give condition from second collection asset_type.asset_type_name like search_key



if(search_key!=''){

let query_string = "/^.*" + search_key + ".*$/i";

dbo.collection("assets").aggregate([
{
"$match": { $and: [ { status: 1 }, { $or: [ { maker_name: { $regex: `${search_key}.*`, $options: 'i' } }, { serial_number: { $regex: `${search_key}.*`, $options: 'i' } } ] } ] }
},
{
$lookup:
{
from: 'asset_type',
localField: 'asset_type',
foreignField: 'asset_type_id',
as: 'asset_type_details'
}
}
]).sort({_id:-1}).toArray(function(err, result) {
if (err){
res.status(200).json({'return_data': err });
}else{
res.status(200).json({'return_data': result });
}

db.close();
});

}









share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb 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 15 at 5:54


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.















  • Please also see Is “How do I convert code from this language to this language” too broad?. Questions like this are off-topic. There are plenty of "general examples" for how to join with MongoDB. If you have a specific question, then it requires specific details of your structure in MongoDB, intended usage and some attempt at resolving this yourself. Ideally, you should not need a JOIN at all, since you knew enough to realize you should be remodeling your data for this completely different engine.
    – Neil Lunn
    Nov 15 at 6:02










  • This edit isn't much better. It's completely unclear what you are even asking and as such we cannot reopen from a duplicate when there's no clear question here since that's another reason to put the question on hold. I suggest at the very least looking at the additional duplicate and fixing up at least the things that would throw errors in the code.
    – Neil Lunn
    Nov 15 at 6:41













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • How do I perform the SQL Join equivalent in MongoDB?

    19 answers



  • Aggregation filter after $lookup

    1 answer




how to give condition from second collection. i gave condition from first collection but also need to give condition from second collection asset_type.asset_type_name like search_key



if(search_key!=''){

let query_string = "/^.*" + search_key + ".*$/i";

dbo.collection("assets").aggregate([
{
"$match": { $and: [ { status: 1 }, { $or: [ { maker_name: { $regex: `${search_key}.*`, $options: 'i' } }, { serial_number: { $regex: `${search_key}.*`, $options: 'i' } } ] } ] }
},
{
$lookup:
{
from: 'asset_type',
localField: 'asset_type',
foreignField: 'asset_type_id',
as: 'asset_type_details'
}
}
]).sort({_id:-1}).toArray(function(err, result) {
if (err){
res.status(200).json({'return_data': err });
}else{
res.status(200).json({'return_data': result });
}

db.close();
});

}









share|improve this question
















This question already has an answer here:




  • How do I perform the SQL Join equivalent in MongoDB?

    19 answers



  • Aggregation filter after $lookup

    1 answer




how to give condition from second collection. i gave condition from first collection but also need to give condition from second collection asset_type.asset_type_name like search_key



if(search_key!=''){

let query_string = "/^.*" + search_key + ".*$/i";

dbo.collection("assets").aggregate([
{
"$match": { $and: [ { status: 1 }, { $or: [ { maker_name: { $regex: `${search_key}.*`, $options: 'i' } }, { serial_number: { $regex: `${search_key}.*`, $options: 'i' } } ] } ] }
},
{
$lookup:
{
from: 'asset_type',
localField: 'asset_type',
foreignField: 'asset_type_id',
as: 'asset_type_details'
}
}
]).sort({_id:-1}).toArray(function(err, result) {
if (err){
res.status(200).json({'return_data': err });
}else{
res.status(200).json({'return_data': result });
}

db.close();
});

}




This question already has an answer here:




  • How do I perform the SQL Join equivalent in MongoDB?

    19 answers



  • Aggregation filter after $lookup

    1 answer








mongodb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 6:21

























asked Nov 15 at 5:53









Nabin Samanta

104




104




marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb 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 15 at 5:54


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 Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb 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 15 at 5:54


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.














  • Please also see Is “How do I convert code from this language to this language” too broad?. Questions like this are off-topic. There are plenty of "general examples" for how to join with MongoDB. If you have a specific question, then it requires specific details of your structure in MongoDB, intended usage and some attempt at resolving this yourself. Ideally, you should not need a JOIN at all, since you knew enough to realize you should be remodeling your data for this completely different engine.
    – Neil Lunn
    Nov 15 at 6:02










  • This edit isn't much better. It's completely unclear what you are even asking and as such we cannot reopen from a duplicate when there's no clear question here since that's another reason to put the question on hold. I suggest at the very least looking at the additional duplicate and fixing up at least the things that would throw errors in the code.
    – Neil Lunn
    Nov 15 at 6:41


















  • Please also see Is “How do I convert code from this language to this language” too broad?. Questions like this are off-topic. There are plenty of "general examples" for how to join with MongoDB. If you have a specific question, then it requires specific details of your structure in MongoDB, intended usage and some attempt at resolving this yourself. Ideally, you should not need a JOIN at all, since you knew enough to realize you should be remodeling your data for this completely different engine.
    – Neil Lunn
    Nov 15 at 6:02










  • This edit isn't much better. It's completely unclear what you are even asking and as such we cannot reopen from a duplicate when there's no clear question here since that's another reason to put the question on hold. I suggest at the very least looking at the additional duplicate and fixing up at least the things that would throw errors in the code.
    – Neil Lunn
    Nov 15 at 6:41
















Please also see Is “How do I convert code from this language to this language” too broad?. Questions like this are off-topic. There are plenty of "general examples" for how to join with MongoDB. If you have a specific question, then it requires specific details of your structure in MongoDB, intended usage and some attempt at resolving this yourself. Ideally, you should not need a JOIN at all, since you knew enough to realize you should be remodeling your data for this completely different engine.
– Neil Lunn
Nov 15 at 6:02




Please also see Is “How do I convert code from this language to this language” too broad?. Questions like this are off-topic. There are plenty of "general examples" for how to join with MongoDB. If you have a specific question, then it requires specific details of your structure in MongoDB, intended usage and some attempt at resolving this yourself. Ideally, you should not need a JOIN at all, since you knew enough to realize you should be remodeling your data for this completely different engine.
– Neil Lunn
Nov 15 at 6:02












This edit isn't much better. It's completely unclear what you are even asking and as such we cannot reopen from a duplicate when there's no clear question here since that's another reason to put the question on hold. I suggest at the very least looking at the additional duplicate and fixing up at least the things that would throw errors in the code.
– Neil Lunn
Nov 15 at 6:41




This edit isn't much better. It's completely unclear what you are even asking and as such we cannot reopen from a duplicate when there's no clear question here since that's another reason to put the question on hold. I suggest at the very least looking at the additional duplicate and fixing up at least the things that would throw errors in the code.
– Neil Lunn
Nov 15 at 6:41

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?