MongoDB "join" (1-to-many and many-to-many manual references?) Help needed. NO ENBEDDING METHODS -
i new mongodb, know quite stuff in mysql. work in robomongo.
let's have 2 collections: instruments:
db.createcollection('instruments'); db.instruments.insert( [{ instrumentid : 1, instrumentname : "squier", instrumentmanufacturerid : 3, instrumentmadein: 'somecountry' },{ ... ])
and manufacturers:
db.createcollection('manufacturers') db.manufacturers.insert([ { manufactid : 1, manufactname : 'fender', manufactcountry : 'united states', manufactfounded : 1946 },{ ... ])
i want "select" (find?) instruments whom manufacturers' country 'united states' ...and want "update" 'fender' instruments' instrumentmadein country 'japan'
i don't want nest manufacturerinformation inside each instrument, becouse repeating same information oven , on again waste of time , space.
i saw code, id field called '_id' , had large object value. had references, not sure tho.
i want simple codes achieve goals, no need large explanations, maybe short, simple ones, that's ask. won't using mongodb ever, assignment. thank you!
mongodb document database, not relational database. not joins. schema depends on "joins" between collections basic operations not fit document database , should denormalized single collection embedded data. when feel icky this, should consider use relational database instead.
when must perform such operation, need on client-side running multiple queries. didn't specify programming language application written in, explain how in mongo shell.
// id of fender var manufacturerid = db.manufacturers.findone({"manufactname" : "fender",}).manufactid; // update instruments manufacturer id made in japan db.instruments.update( { "instrumentmanufacturerid" : manufacturerid, "instrumentmadein": "japan" }, { $set: { "foo":"bar"}}, { multi:true } );
yes, ugly , slow need 2 queries such simple operation, that's when try use screwdriver hammer.
regarding question _id
field: every document gets field automatically, , when set automatically set (practically) globally unique objectid. can use unique id documents, don't have to. when data has unique key, can set _id
value yourself.
Comments
Post a Comment