Get Data from the different collection without any relation in mongoose

doshi smit :

Get Data from the different collection without any relation in mongoose

Collection Schema

mongoose.Schema({
    skillid:{type:Number},
    name:{type:String},
});

Skill Collection

mongoose.Schema({
  userid: {type: mongoose.Types.ObjectId},
  OverView:{type:String},
  location:{type:String},
  skills:[{Type:Number}] 
});
 { 
_id: 5f48d5d1b98bffee67b49917
 skillid: 1
 name: 'HTML' 
}

{ 
_id: 5f48d612b98bffee67b49919
skillid: 2
 name: 'PHP' 
}


User Collection

{
  _id: 5f425bdb311b791670d60de6,
  userid: 5f41115fbd904134883ae2d8,
  OverView: 'sdsdssdsd',
  skills: [1,2],   // skill id
  Education: [ 5f453e7f53895727f0e39d82, 5f453fb963d4ab181c115982 ],
  location: 'India',
}

How can u get skill name from Skill Collection - mongoose

i want result like this

 { 
  _id: 5f425bdb311b791670d60de6, 
   userid: 5f41115fbd904134883ae2d8, 
   OverView: 'sdsdssdsd', 
   skills: ['HTML','PHP'], // skill id
   Education: [ 5f453e7f53895727f0e39d82, 5f453fb963d4ab181c115982 ],
   location: 'India'
 } ```

turivishal :

You can use aggregate(),

  • $lookup join skill collection, match skills in skill collection
  • $addFields to add skill name in array format using $reduce
db.user.aggregate([
  {
    $lookup: {
      from: "skill",
      localField: "skills",
      foreignField: "skillid",
      as: "skills"
    }
  },
  {
    $addFields: {
      skills: {
        $reduce: {
          input: "$skills",
          initialValue: [],
          in: {
            $concatArrays: [["$$this.name"], "$$value"]
          }
        }
      }
    }
  }
])

Playground


Another option you can use mongoose Virtual and also look at this

// SKILL SCHEMA
const SkillSchema = mongoose.Schema({
    skillid:{type:Number},
    name:{type:String},
});

// USER SCHEMA
const UserSchema = mongoose.Schema({
  userid: {type: mongoose.Types.ObjectId},
  OverView:{type:String},
  location:{type:String},
  skills:[{Type:Number}] 
});

// CREATES VIRTUAL CONNECTION WITH SKILL COLLECTION
UserSchema.virtual('skills', {
  ref: 'Skill', // The model to use
  localField: 'skills', // Find people where `localField`
  foreignField: 'skillid', // is equal to `foreignField`
  // If `justOne` is true, 'members' will be a single doc as opposed to
  // an array. `justOne` is false by default.
  justOne: false,
  // you can use other options
  // options: { sort: { name: -1 }, limit: 5 } 
});

// CREATE MODELS
const User = mongoose.model('User', UserSchema);
const Skill = mongoose.model('Skill', SkillSchema);

// QUERY TO FIND USERS AND POPULATE SKILLS
User.find({}).populate('skills').exec(function(error, user) {
  console.log(user);
});

Note: please refer documentation, if you are getting any problem, This is not tested code!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get data from different doc to a different collection in flutter firebase

Select and compare two Datetime columns from different table without having any relation

How to get data back from Mongoose without using a callback?

Mongoose: Cannot fetch file documents from .chunk collection without the data field

Mongoose find data from dynamic Collection

Mongoose lookup fetching data from multi collection

Can you get any data from the Binance API without authentification?

Get relation of a collection in rails

How to execute function based on URL in Iron Router without any data from collection?

mongoose get relative data without query

Fetch different data from different tables with no relation to the same view in Laravel

Laravel Livewire Get Relation Data From String

get relation data from one table in oracle

Laravel get data from many to many relation

Get only specific attributes with from Laravel Collection and relation

Getting data from a different collection while performing aggregation on a different collection

mongoose get count of relation with condition

Get any item from the Set (Collection)

Is there any solution to extract all data from collection?

How to get nodes from relationships with any depth including relation filter

express+mongoose get multi collection data and render to html

Combining data from different tables into one collection

Is there any way to get date from ObjectId from mongoose using aggregate?

Get an Object from a collection without looping in java

How to get counting of documents from another collection in mongoose?

How to get collection name from a Mongoose model object

Is there a way to get only User item from a collection using Mongoose DB?

Can't get collection array from mongoDB with mongoose query

How to remove data from mongodb collection using nodejs and mongoose