0

I'm creating a basic app for a school project. I want to match dog owners with dogs they own. I have set up two models one for dogs and one for the owners. A dog owner can have multiple dogs so I guess it's a one to many. One owner can have multiple dogs...

I have the following models for Dog:

var mongoose = require('mongoose'); var Schema = mongoose.Schema;

var DogSchema = new Schema({
  name: String,
  age: Number,
  location: String,
  breed: String,
  sex: String
});

module.exports = mongoose.model('Dog', DogSchema);

and owner:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var OwnerSchema = new Schema({
  firstName: String,
  lastName: String,
  age: Number,
  location: String,
  favorite: String,
  numberOfBreeds: Number,
  numberOfDogs: String,
  username: String,
  password: String
});

module.exports = mongoose.model('Owner', OwnerSchema);

how can I join the two? I'm not sure I understand the process of joining in mongo (or sql for that matter)...

Thanks in advance!

enRaiser
  • 2,606
  • 2
  • 21
  • 39
Atlante Avila
  • 1,340
  • 2
  • 16
  • 37
  • Possible dupe of https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb – JohnnyHK Aug 05 '16 at 01:43

1 Answers1

1

I show the answers for which this question is requested for duplication. but I dont thinks its duplicate so I am answering.

Basically by adding relation between one Schema to another schema

var DogSchema = new Schema({
   name: String,
   age: Number,
   location: String,
   breed: String,
   sex: String,
   owner : [{ type: Schema.Types.ObjectId, ref: 'Owner' }]
});
enRaiser
  • 2,606
  • 2
  • 21
  • 39