0

I have a certain Schema on my MongoDB Database for representing a Brand

var BrandSchema = new Schema({
  name: {type: String, required: true, unique: true},
  peopleInterested: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}]
}).plugin(mongoosePaginate);

Now, this collection has been populated via the mongo shell with the command db.Brand.insert({}) but, if I try to query the collection again via the shell or via my Node.JS app, Node.js won't return anything even though there are matching elements.
I was thinking that there might be an issue with the way I defined the peopleInterested array, so I redefined the Schema without the relationship reference but I still have the same issue.
Following is a sample of the User's schema

var UserSchema = new Schema({
  isAdmin: {type: Boolean, default: false},
  name: String,
  surname: String,
  email: { type: String, lowercase: true, required: true, trim: true, unique: true, dropDubs: true },
  password: { type: String, required: true },
  salt: { type: String },
  verified: { type: Boolean, default: false },
  bio: {
    type: { type: String, enum: [0,1] }, // 0='Squadra', 1='Giocatore'
    birthday: String,
    height: Number,
    number: Number,
    role: { type: String, enum: [0,1,2,3] }, // 0='Playmaker', 1='Ala', 2='Guardia', 3='Centro'
    team: String,
    city: String,
    fiscalCode: {type: String, maxlength:16}
  },
  newsletter: {type: Boolean, default: false},
  lastCheckin: {type: mongoose.Schema.Types.ObjectId, ref: 'Checkin'},
  follows: [{type: mongoose.Schema.Types.ObjectId, ref: 'Structure'}],
  interestedBrands: Array,
  score: { type: Number, default: 0 },
  profilePicture: String,
  lastLogin: {type: Date},
  facebook: {
    id: String,
    accessToken: String,
    profileImage : String
  }
}, {
  collection: 'users',
  retainKeyOrder: true,
  timestamps: true,
}).plugin(mongoosePaginate);

What am I missing?
EDIT : These are two sample queries that I am doing either via shell or via Node.JS
Shell: db.Brand.find({name:"Canterbury"})
Node.js :

exports.findByName = (req, res) => {
    Brand.find({name: req.body.name},function(err, results) {
    return err ? res.status(404).json(err) : res.status(202).json(results);
  });
};


EDIT 2: An example of what the mongo shell returns (as requested in the comments)

{ "_id" : ObjectId("5be05876f2daa245bf1d33e4"), "name" : "Canterbury", "peopleInterested" : [ ] }


EDIT 3: An example of what db.Brand.find({}) query via shell returns

> db.Brand.find({})
{ "_id" : ObjectId("5be05876f2daa245bf1d33e4"), "name" : "Canterbury", "peopleInterested" : [ ] }
{ "_id" : ObjectId("5be0587ff2daa245bf1d33e5"), "name" : "Adidas", "peopleInterested" : [ ] }
{ "_id" : ObjectId("5be05887f2daa245bf1d33e6"), "name" : "Nike", "peopleInterested" : [ ] }
Gianmarco F.
  • 780
  • 2
  • 12
  • 36

0 Answers0