如何在MongooseJS中使用多个模式的查询?

时间:2014-07-06 21:11:38

标签: node.js mongodb mongoose nested-queries database

我有两个模式,存储和收据。我想找到商店名称为x的所有收据。你会怎么做?

模型/ 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 );

模型/ 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);

0 个答案:

没有答案