0

I have two schemas, Store and Receipt. I want to find all receipts where the store name is x. How would you do that?

models/store.js

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

var StoreSchema = new Schema({
    name:       { type: String, require: true },
    address:    { type: ObjectId, ref: 'Address' },
    items:      [ { type: ObjectId, ref: 'Item' } ]
});

mongoose.model( 'Store', StoreSchema );

models/receipt.js

var mongoose = require('mongoose'),
    Schema   = mongoose.Schema,
    ObjectId = Schema.Types.ObjectId;

var LineItemSchema = new Schema({
    item:   { type: ObjectId, ref: 'item' },
    amount: { type: Number, default: 0 }
});

mongoose.model('LineItem', LineItemSchema);

var ReceiptSchema = new Schema({
    name:       { type: String, default: '' },
    store:      { type: ObjectId, ref: 'Store' },
    items:      [{ type: ObjectId, ref: 'LineItem' }],
    copyNumber: { type: Number, default: 0 } // Number of the receipt of the same store ( Wegmans1, Wegmans2 ...)
});

mongoose.model('Receipt', ReceiptSchema);
Community
  • 1
  • 1
Vongdarakia
  • 379
  • 1
  • 12
  • 25
  • JohnnyHK, this isn't a duplicate. Here in my question, I have stores that could have the same name. I can have two Walmarts with different addresses for instance. The example you linked me to is more specific. – Vongdarakia Jul 06 '14 at 21:43
  • 1
    OK, in that case, here's a better duplicate: http://stackoverflow.com/questions/19380738/mongoose-nested-query-on-model-by-field-of-its-referenced-model – JohnnyHK Jul 06 '14 at 21:48

0 Answers0