GithubHelp home page GithubHelp logo

Comments (4)

pgherveou avatar pgherveou commented on June 18, 2024

Hey @kwhitley that should work just fine, let me have a look!

from mongoosefiller.

pgherveou avatar pgherveou commented on June 18, 2024

Try this out that should work

var mongoose = require('mongoose'),
    filler = require('mongoosefiller');

mongoose.set('debug', true);
mongoose.connect('mongodb://localhost/mongoosefiller-test', function (err) {
  if (err) console.error(err);
});

var GearSchema = new mongoose.Schema({
  name: {type: String},
  brand: {_id: Number}
});

var BrandSchema = new mongoose.Schema({
  _id: {type: Number},
  name: { type: String }
});

var Gear = mongoose.model('Gear', GearSchema);
var Brand = mongoose.model('Brand', BrandSchema);

GearSchema.plugin(filler, {
  path: 'brand',
  ref : 'Brand',
  dest: 'Gear'
});

Brand.create([{ name: 'Canon', _id: 1 }, { name: 'Nikon', _id: 2 }], function (err) {
  if (err) console.error(err);
  var gear = new Gear({ name: '5D Mark II', brand: { _id: 1 }});
  gear.save(function () {
    console.log('results:', arguments);
  });
});

this is the output I have

Mongoose: brands.insert({ __v: 0, _id: 1, name: 'Canon' }) {}
Mongoose: brands.insert({ __v: 0, _id: 2, name: 'Nikon' }) {}
Mongoose: brands.findOne({ _id: 1 }) { fields: { __v: 1, name: 1, _id: 1 }, safe: undefined }
Mongoose: gears.insert({ __v: 0, brand: { __v: 0, name: 'Canon', _id: 1 }, _id: ObjectId("5267274a265280b20b000001"), name: '5D Mark II' }) {}
results: { '0': null,
  '1':
   { __v: 0,
     name: '5D Mark II',
     _id: 5267274a265280b20b000001,
     brand: { __v: 0, name: 'Canon', _id: 1 } },
  '2': 1 }

Let me know if something is not clear, and don't hesitate to make a pull request to clarify the doc

from mongoosefiller.

kwhitley avatar kwhitley commented on June 18, 2024

Thanks for the quick response... should have clarified, the _id: 1 was just for a shorthand for an ObjectID...

So each collection would only have name defined, and Gear would have a ref to Brand.

I'll check some more sandboxed cases and see if I can get it to work in an isolated case. Only diff I can see offhand is you manually defined an _id: Number on Brand...

from mongoosefiller.

kwhitley avatar kwhitley commented on June 18, 2024

Must be on my end somewhere... I was able to CURL it and it worked like a champ. Thanks for the help and keep up the good work @pgherveou!

from mongoosefiller.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.