简体   繁体   中英

Mongoose Relationship Populate Doesn't Return results

    var SecuritySchema = new Mongoose.Schema({
        _bids: [{
            type: Mongoose.Schema.Types.ObjectId,
            ref: 'BuyOrder'
        }],
        _asks: [{
            type: Mongoose.Schema.Types.ObjectId,
            ref: 'SellOrder'
        }]
    });

    var OrdersSchema = new Mongoose.Schema({
        _security: {
            type: Mongoose.Schema.Types.ObjectId,
            ref: 'Security'
        },
        price: {
            type: Number,
            required: true
        },
        quantity: {
            type: Number,
            required: true
        }
    });


    // declare seat covers here too
    var models = {
        Security: Mongoose.model('Security', SecuritySchema),
        BuyOrder: Mongoose.model('BuyOrder', OrdersSchema),
        SellOrder: Mongoose.model('SellOrder', OrdersSchema)
    };
    return models;

And than when I save a new BuyOrder for example:

// I put the 'id' of the security: order.__security = security._id on the client-side
var order = new models.BuyOrder(req.body.order);
    order.save(function(err) {
        if (err) return console.log(err);
    });

And attempt to re-retrieve the associated security:

models.Security.findById(req.params.id).populate({
        path: '_bids'
    }).exec(function(err, security) {
        // the '_bids' array is empty.
    });

I think this is some sort of naming issue, but I'm not sure, I've seen examples here and on the moongoose website that use Number as the Id type: http://mongoosejs.com/docs/populate.html

The ref field should use the singular model name

Also, just do:

models.Security.findById(req.params.id).populate('_bids').exec(...

My main suspicion given your snippet at the moment is your req.body.order has _security as a string instead of an array containing a string.

Also, you don't need an id property. Mongodb itself will automatically do the _id as a real BSON ObjectId, and mongoose will add id as a string representation of the same value, so don't worry about that.

While I don't understand your schema (and the circular nature of it?), this code works:

var order = new models.BuyOrder({ price: 100, quantity: 5});
order.save(function(err, orderDoc) {
    var security = new models.Security();
    security._bids.push(orderDoc);
    security.save(function(err, doc) {
       models.Security.findById({ _id: doc._id })
           .populate("_bids").exec(function(err, security) {
              console.log(security);

           });
    });
});

It:

  1. creates a BuyOrder
  2. save s it
  3. creates a Security
  4. adds to the array of _bids the new orderDoc 's _id
  5. saves it
  6. searches for the match and populates

Note that there's not an automatic method for adding the document to the array of _bids, so I've done that manually.

Results:

{ _id: 5224e73af7c90a2017000002,
  __v: 0,
  _asks: [],
  _bids: [ { price: 100, 
           quantity: 5, 
           _id: 5224e72ef7c90a2017000001, __v: 0 } ] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM