how to reference from orders to lineItem? #1

Closed
piscis opened this Issue Jun 10, 2011 · 1 comment

Projects

None yet

2 participants

@piscis

Hi,

I have a question regarding one to many referencing, in you´re documentation you have the example of Order 1:n LineItems. And i wonder how to query all LineItems by an order document? The following is my example code:

Example

var mongoose = require("mongoose");
var sys = require('sys');

// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");

// Access the mongoose-dbref module and install everything
var dbref = require("mongoose-dbref");
var utils = dbref.install(mongoose);

var DBRef = mongoose.SchemaTypes.DBRef;
var Schema = mongoose.Schema;

var LineItemSchema = new Schema({
    order: {type: DBRef, resolve: true},
    description: String,
    cost: Number
});

var OrderSchema = new Schema({
    poNumber: String,
    lineItems: [DBRef]
});

db.model('LineItem',LineItemSchema);
db.model('Order',OrderSchema);

db.open('mongodb://localhost/sampledb');
db.on('open',function() {

    var LineItem = db.model('LineItem');
    var Order = db.model('Order');

    console.log('open');

    var order = new Order();
    order.save(function(err){

        var lineItem = new LineItem();

        lineItem.setOrder(order);

        lineItem.save(function(err) {

            LineItem.findOne(function(err, doc1) {

                utils.fetch(db, doc1.order,function(err, doc) {
                    if (err) throw err;
                    console.log('Order:');
                    console.dir(doc);

                    console.log('LineItem:');
                    console.dir(doc1);

                    console.log('closing');
                    db.close();
                });
            });
        });
    });
});

Output

node test1.js 
open
Order:
{ lineItems: [], _id: 4df221e54a675bc90b000001 }
LineItem:
{ _id: 4df221e54a675bc90b000002,
  order: 
   { namespace: 'orders',
     oid: 4df221e54a675bc90b000001,
     db: 'sampledb' } }
closing

As you can see the reference to LineItem is correct but the reference in the Order Array is missing. Is there a way to work around that issue?

Thx,

Alex

@goulash1971
Owner

Hi Alex

I can see what you are trying to do i.e. bi-directional / cascaded join setter/getter functions.

As you know MongoDB as a doc oriented db doesn't really understand the concept of constraints and transactions, both of which are really needed to support bi-directional joins, and the dbref type is intended to be a reference (as opposed to a foreign key).

I have another plugin that I will push live which provides some simple join support for mongoose - it's extensible so that it might help.

Until then I've added an example which does what you want to do ... using promises would make it a lot cleaner (that's what I use in mongoose-joins).

  • Stuart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment