Question

Theser are my two schemas that i want to ref. I want the listingsScheme userId to refer to the _id of userScheme. Its been 2 days working on this please help.

var userScheme = new Schema({
email: { type: String },
password: { type: String },
fullName: { type: String },
coverPhoto: { type: String},
profilePhoto: { type: String},
fbUserID: { type: String},
profileExtraInfo: { type: Array},
listings: {type: Schema.Types.ObjectId, ref: "Listings"}

var listingsScheme = new Schema({
category: { type: String, index: true }, 
title: { type: String },
description: { type: String },
attributes: { type: Array },
nrViews: {type: Number },
photos: { type: Array },
location: { type: Array, index: '2d' },
created: { type: Date, index: true },
updated: { type: Date },
active: { type: Boolean, index: true } ,
userId : { type: Schema.Types.ObjectId, ref : 'User' } ,
fullName : { type: String },
profilePhoto : { type : String},
followers : {type : String},
Was it helpful?

Solution

Apart from your syntax errors, your code should work.

  1. Remember to use plural model names (you have 'User'=>'users').
  2. don't forget to model your schema's.
  3. don't forget to use the .populate('userId') method.
  4. don't forget to connect with the server (happens more often than you think).
  5. ensure that you really have the documents connected to eachother.

Tested code:

var mongoose  = require('mongoose');

//  connect with database.
mongoose.connect('mongodb://localhost:27017/temp');

var userScheme = new mongoose.Schema({
  email: { type: String },
  password: { type: String },
  fullName: { type: String },
  coverPhoto: { type: String},
  profilePhoto: { type: String},
  fbUserID: { type: String},
  profileExtraInfo: { type: Array},
  listings: {type: mongoose.Schema.Types.ObjectId, ref: "Listings"}
});

var listingsScheme = new mongoose.Schema({
  category: { type: String, index: true }, 
  title: { type: String },
  description: { type: String },
  attributes: { type: Array },
  nrViews: {type: Number },
  photos: { type: Array },
  location: { type: Array, index: '2d' },
  created: { type: Date, index: true },
  updated: { type: Date },
  active: { type: Boolean, index: true } ,
  userId : { type: mongoose.Schema.Types.ObjectId, ref : 'User' } ,
  fullName : { type: String },
  profilePhoto : { type : String},
  followers : {type : String}
});

//  don't forget to 'model' your scheme.
var users    = mongoose.model('User', userScheme);
var listings = mongoose.model('listings', listingsScheme);

//  create mock user.
var user = new users({
  email:'test'
});

//  create mock listing with the user_id attached to property userId.
var listing = new listings({
  userId:user._id
});

//  save the created user and listing.
user.save();
listing.save();

//  find ONE document in the collection 'listings'.
//  populate the userId.
//  execute the function and let mongoose call your callback.
listings.findOne().populate('userId').exec(function(err, doc)
{
  //  ensure there are no errors.
  if(!err && doc)
  {
    //  print the doc we found.
    //  should give userdoc inside userId property.
    console.log(doc);
  }
});

//  clear collections.
//users.remove({});
//listings.remove({});

Runnable link: http://runnable.com/U24GoC9x3L9alfPc/output

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top