javascript - Make or simulate a join in mongodb -
i simulate bring together query in mongodb using mongoose. there no nested data, 2 schemas, orderschema:_id, referencing detailschema: orderid
it's bad solution orders , query each of details 1 one. have tried using populate in mongoose, worked if changed scheme, , not possibility. cannot think of clean way it!
var orderschema = mongoose.schema({ _id: number, customerid: string, employeeid: number, orderdate: string, requireddate: string, shippeddate: string, shipvia: string, freight: number, shipname: }); exports.ordermodel = mongoose.model('orders', orderschema); var detailsschema = mongoose.schema({ orderid: number, productid: quantity: number, discount: number });
just alter detailschema
definition define orderid
reference order
:
var detailsschema = mongoose.schema({ orderid: {type: number, ref: 'order'}, productid: number, quantity: number, discount: number });
this doesn't require alter actual info in existing doc, ref
metadata mongoose uses know model's _id
field contains reference to.
with in place, can utilize populate
pull in order details as:
details.find().populate('orderid').exec(function(err, details) { ... });
javascript node.js mongodb mongoose entity-relationship
No comments:
Post a Comment