0

i have one collection name CategoryCollection and Other is SubcategoryCollection and i want data merging this two collection and i am using mongoose schema for collection

var mongoose = require('mongoose');
var Category= mongoose.Schema({
_id: {
    type: string,
    default: 0
},
oldinsta: {
    type: String,
},
var category= mongoose.model('Cat', Category)
module.exports = category;

and other collection schema is as below

var SubCat= mongoose.Schema({

_id:
    { type: String },

 cat_id :{
     {type : string}
    }


var subcat= mongoose.model('subCategory', SubCat)
module.exports= subcat;

then how to get data betwween this two collection i am new in node js so please help me

i am try like this as below

    category.aggregate([
{
    $unwind: "$_id"
},

{
    $lookup:
      {
          from: "SubCat",
          localField: "_id",

          foreignField: "cat_id",
          as: "embeddedData"
      }

}
    ], function (err, data) {
        console.log(data);
        res.send(data);
    })
  • What have you tried? How is your question different from [How do I perform the SQL Join equivalent in MongoDB?](https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb). Because if you cannot show you tried something and have a specific question then that answers your question as far as I see. – Neil Lunn Jul 27 '17 at 08:27
  • @NeilLunn i have edited my quetion please can you help me – Imran Kureshi Jul 27 '17 at 08:46
  • Nothing to help you with that is not already answered before. `$lookup` is a server side operation so it needs the real collection name. This will be `subcategories` using lowercase and plural from the name provided to the `mongoose.model` constructor argument. Or alternately use `mongoose.model('subCategory').collection.name` and that code accesses the real collection name all of the time. – Neil Lunn Jul 27 '17 at 09:00
  • @NeilLunn please give me some more details and something like hint how can i do that, i am suffering this problem some few days so please help me – Imran Kureshi Jul 27 '17 at 09:39

0 Answers0