model.js 184 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const Aggregate = require('./aggregate');
  6. const ChangeStream = require('./cursor/changeStream');
  7. const Document = require('./document');
  8. const DocumentNotFoundError = require('./error/notFound');
  9. const EventEmitter = require('events').EventEmitter;
  10. const Kareem = require('kareem');
  11. const MongooseBulkWriteError = require('./error/bulkWriteError');
  12. const MongooseError = require('./error/index');
  13. const ObjectParameterError = require('./error/objectParameter');
  14. const OverwriteModelError = require('./error/overwriteModel');
  15. const Query = require('./query');
  16. const SaveOptions = require('./options/saveOptions');
  17. const Schema = require('./schema');
  18. const ValidationError = require('./error/validation');
  19. const VersionError = require('./error/version');
  20. const ParallelSaveError = require('./error/parallelSave');
  21. const applyDefaultsHelper = require('./helpers/document/applyDefaults');
  22. const applyDefaultsToPOJO = require('./helpers/model/applyDefaultsToPOJO');
  23. const applyEmbeddedDiscriminators = require('./helpers/discriminator/applyEmbeddedDiscriminators');
  24. const applyHooks = require('./helpers/model/applyHooks');
  25. const applyMethods = require('./helpers/model/applyMethods');
  26. const applyProjection = require('./helpers/projection/applyProjection');
  27. const applyReadConcern = require('./helpers/schema/applyReadConcern');
  28. const applySchemaCollation = require('./helpers/indexes/applySchemaCollation');
  29. const applyStaticHooks = require('./helpers/model/applyStaticHooks');
  30. const applyStatics = require('./helpers/model/applyStatics');
  31. const applyTimestampsHelper = require('./helpers/document/applyTimestamps');
  32. const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
  33. const applyVirtualsHelper = require('./helpers/document/applyVirtuals');
  34. const assignVals = require('./helpers/populate/assignVals');
  35. const castBulkWrite = require('./helpers/model/castBulkWrite');
  36. const clone = require('./helpers/clone');
  37. const createPopulateQueryFilter = require('./helpers/populate/createPopulateQueryFilter');
  38. const decorateUpdateWithVersionKey = require('./helpers/update/decorateUpdateWithVersionKey');
  39. const getDefaultBulkwriteResult = require('./helpers/getDefaultBulkwriteResult');
  40. const getSchemaDiscriminatorByValue = require('./helpers/discriminator/getSchemaDiscriminatorByValue');
  41. const discriminator = require('./helpers/model/discriminator');
  42. const each = require('./helpers/each');
  43. const get = require('./helpers/get');
  44. const getConstructorName = require('./helpers/getConstructorName');
  45. const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
  46. const getModelsMapForPopulate = require('./helpers/populate/getModelsMapForPopulate');
  47. const immediate = require('./helpers/immediate');
  48. const internalToObjectOptions = require('./options').internalToObjectOptions;
  49. const isDefaultIdIndex = require('./helpers/indexes/isDefaultIdIndex');
  50. const isIndexEqual = require('./helpers/indexes/isIndexEqual');
  51. const isTimeseriesIndex = require('./helpers/indexes/isTimeseriesIndex');
  52. const {
  53. getRelatedDBIndexes,
  54. getRelatedSchemaIndexes
  55. } = require('./helpers/indexes/getRelatedIndexes');
  56. const decorateDiscriminatorIndexOptions = require('./helpers/indexes/decorateDiscriminatorIndexOptions');
  57. const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedInclusive');
  58. const leanPopulateMap = require('./helpers/populate/leanPopulateMap');
  59. const parallelLimit = require('./helpers/parallelLimit');
  60. const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
  61. const pushNestedArrayPaths = require('./helpers/model/pushNestedArrayPaths');
  62. const removeDeselectedForeignField = require('./helpers/populate/removeDeselectedForeignField');
  63. const setDottedPath = require('./helpers/path/setDottedPath');
  64. const util = require('util');
  65. const utils = require('./utils');
  66. const minimize = require('./helpers/minimize');
  67. const MongooseBulkSaveIncompleteError = require('./error/bulkSaveIncompleteError');
  68. const ObjectExpectedError = require('./error/objectExpected');
  69. const decorateBulkWriteResult = require('./helpers/model/decorateBulkWriteResult');
  70. const modelCollectionSymbol = Symbol('mongoose#Model#collection');
  71. const modelDbSymbol = Symbol('mongoose#Model#db');
  72. const modelSymbol = require('./helpers/symbols').modelSymbol;
  73. const subclassedSymbol = Symbol('mongoose#Model#subclassed');
  74. const { VERSION_INC, VERSION_WHERE, VERSION_ALL } = Document;
  75. const saveToObjectOptions = Object.assign({}, internalToObjectOptions, {
  76. bson: true
  77. });
  78. /**
  79. * A Model is a class that's your primary tool for interacting with MongoDB.
  80. * An instance of a Model is called a [Document](https://mongoosejs.com/docs/api/document.html#Document).
  81. *
  82. * In Mongoose, the term "Model" refers to subclasses of the `mongoose.Model`
  83. * class. You should not use the `mongoose.Model` class directly. The
  84. * [`mongoose.model()`](https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.model()) and
  85. * [`connection.model()`](https://mongoosejs.com/docs/api/connection.html#Connection.prototype.model()) functions
  86. * create subclasses of `mongoose.Model` as shown below.
  87. *
  88. * #### Example:
  89. *
  90. * // `UserModel` is a "Model", a subclass of `mongoose.Model`.
  91. * const UserModel = mongoose.model('User', new Schema({ name: String }));
  92. *
  93. * // You can use a Model to create new documents using `new`:
  94. * const userDoc = new UserModel({ name: 'Foo' });
  95. * await userDoc.save();
  96. *
  97. * // You also use a model to create queries:
  98. * const userFromDb = await UserModel.findOne({ name: 'Foo' });
  99. *
  100. * @param {Object} doc values for initial set
  101. * @param {Object} [fields] optional object containing the fields that were selected in the query which returned this document. You do **not** need to set this parameter to ensure Mongoose handles your [query projection](https://mongoosejs.com/docs/api/query.html#Query.prototype.select()).
  102. * @param {Object} [options] optional object containing the options for the document.
  103. * @param {Boolean} [options.defaults=true] if `false`, skip applying default values to this document.
  104. * @param {Boolean} [options.skipId=false] By default, Mongoose document if one is not provided and the document's schema does not override Mongoose's default `_id`. Set `skipId` to `true` to skip this generation step.
  105. * @inherits Document https://mongoosejs.com/docs/api/document.html
  106. * @event `error`: If listening to this event, 'error' is emitted when a document was saved and an `error` occurred. If not listening, the event bubbles to the connection used to create this Model.
  107. * @event `index`: Emitted after `Model#ensureIndexes` completes. If an error occurred it is passed with the event.
  108. * @event `index-single-start`: Emitted when an individual index starts within `Model#ensureIndexes`. The fields and options being used to build the index are also passed with the event.
  109. * @event `index-single-done`: Emitted when an individual index finishes within `Model#ensureIndexes`. If an error occurred it is passed with the event. The fields, options, and index name are also passed.
  110. * @api public
  111. */
  112. function Model(doc, fields, options) {
  113. if (fields instanceof Schema) {
  114. throw new TypeError('2nd argument to `Model` constructor must be a POJO or string, ' +
  115. '**not** a schema. Make sure you\'re calling `mongoose.model()`, not ' +
  116. '`mongoose.Model()`.');
  117. }
  118. if (typeof doc === 'string') {
  119. throw new TypeError('First argument to `Model` constructor must be an object, ' +
  120. '**not** a string. Make sure you\'re calling `mongoose.model()`, not ' +
  121. '`mongoose.Model()`.');
  122. }
  123. Document.call(this, doc, fields, options);
  124. }
  125. /**
  126. * Inherits from Document.
  127. *
  128. * All Model.prototype features are available on
  129. * top level (non-sub) documents.
  130. * @api private
  131. */
  132. Object.setPrototypeOf(Model.prototype, Document.prototype);
  133. Model.prototype.$isMongooseModelPrototype = true;
  134. /**
  135. * Connection the model uses.
  136. *
  137. * @api public
  138. * @property db
  139. * @memberOf Model
  140. * @instance
  141. */
  142. Model.prototype.db;
  143. /**
  144. * Changes the Connection instance this model uses to make requests to MongoDB.
  145. * This function is most useful for changing the Connection that a Model defined using `mongoose.model()` uses
  146. * after initialization.
  147. *
  148. * #### Example:
  149. *
  150. * await mongoose.connect('mongodb://127.0.0.1:27017/db1');
  151. * const UserModel = mongoose.model('User', mongoose.Schema({ name: String }));
  152. * UserModel.connection === mongoose.connection; // true
  153. *
  154. * const conn2 = await mongoose.createConnection('mongodb://127.0.0.1:27017/db2').asPromise();
  155. * UserModel.useConnection(conn2); // `UserModel` now stores documents in `db2`, not `db1`
  156. *
  157. * UserModel.connection === mongoose.connection; // false
  158. * UserModel.connection === conn2; // true
  159. *
  160. * conn2.model('User') === UserModel; // true
  161. * mongoose.model('User'); // Throws 'MissingSchemaError'
  162. *
  163. * Note: `useConnection()` does **not** apply any [connection-level plugins](https://mongoosejs.com/docs/api/connection.html#Connection.prototype.plugin()) from the new connection.
  164. * If you use `useConnection()` to switch a model's connection, the model will still have the old connection's plugins.
  165. *
  166. * @function useConnection
  167. * @param [Connection] connection The new connection to use
  168. * @return [Model] this
  169. * @api public
  170. */
  171. Model.useConnection = function useConnection(connection) {
  172. if (!connection) {
  173. throw new Error('Please provide a connection.');
  174. }
  175. if (this.db) {
  176. delete this.db.models[this.modelName];
  177. delete this.prototype.db;
  178. delete this.prototype[modelDbSymbol];
  179. delete this.prototype.collection;
  180. delete this.prototype.$collection;
  181. delete this.prototype[modelCollectionSymbol];
  182. }
  183. this.db = connection;
  184. const collection = connection.collection(this.collection.collectionName, connection.options);
  185. this.prototype.collection = collection;
  186. this.prototype.$collection = collection;
  187. this.prototype[modelCollectionSymbol] = collection;
  188. this.prototype.db = connection;
  189. this.prototype[modelDbSymbol] = connection;
  190. this.collection = collection;
  191. this.$__collection = collection;
  192. connection.models[this.modelName] = this;
  193. return this;
  194. };
  195. /**
  196. * The collection instance this model uses.
  197. * A Mongoose collection is a thin wrapper around a [MongoDB Node.js driver collection]([MongoDB Node.js driver collection](https://mongodb.github.io/node-mongodb-native/Next/classes/Collection.html)).
  198. * Using `Model.collection` means you bypass Mongoose middleware, validation, and casting.
  199. *
  200. * This property is read-only. Modifying this property is a no-op.
  201. *
  202. * @api public
  203. * @property collection
  204. * @memberOf Model
  205. * @instance
  206. */
  207. Model.prototype.collection;
  208. /**
  209. * Internal collection the model uses.
  210. *
  211. * This property is read-only. Modifying this property is a no-op.
  212. *
  213. * @api private
  214. * @property collection
  215. * @memberOf Model
  216. * @instance
  217. */
  218. Model.prototype.$__collection;
  219. /**
  220. * The name of the model
  221. *
  222. * @api public
  223. * @property modelName
  224. * @memberOf Model
  225. * @instance
  226. */
  227. Model.prototype.modelName;
  228. /**
  229. * Additional properties to attach to the query when calling `save()` and
  230. * `isNew` is false.
  231. *
  232. * @api public
  233. * @property $where
  234. * @memberOf Model
  235. * @instance
  236. */
  237. Model.prototype.$where;
  238. /**
  239. * If this is a discriminator model, `baseModelName` is the name of
  240. * the base model.
  241. *
  242. * @api public
  243. * @property baseModelName
  244. * @memberOf Model
  245. * @instance
  246. */
  247. Model.prototype.baseModelName;
  248. /**
  249. * Event emitter that reports any errors that occurred. Useful for global error
  250. * handling.
  251. *
  252. * #### Example:
  253. *
  254. * MyModel.events.on('error', err => console.log(err.message));
  255. *
  256. * // Prints a 'CastError' because of the above handler
  257. * await MyModel.findOne({ _id: 'Not a valid ObjectId' }).catch(noop);
  258. *
  259. * @api public
  260. * @property events
  261. * @fires error whenever any query or model function errors
  262. * @memberOf Model
  263. * @static
  264. */
  265. Model.events;
  266. /**
  267. * Compiled middleware for this model. Set in `applyHooks()`.
  268. *
  269. * @api private
  270. * @property _middleware
  271. * @memberOf Model
  272. * @static
  273. */
  274. Model._middleware;
  275. /*!
  276. * ignore
  277. */
  278. function _applyCustomWhere(doc, where) {
  279. if (doc.$where == null) {
  280. return;
  281. }
  282. for (const key of Object.keys(doc.$where)) {
  283. where[key] = doc.$where[key];
  284. }
  285. }
  286. /*!
  287. * ignore
  288. */
  289. function _createSaveOptions(doc, options) {
  290. const saveOptions = {};
  291. applyWriteConcern(doc.$__schema, options);
  292. if (typeof options.writeConcern !== 'undefined') {
  293. saveOptions.writeConcern = {};
  294. if ('w' in options.writeConcern) {
  295. saveOptions.writeConcern.w = options.writeConcern.w;
  296. }
  297. if ('j' in options.writeConcern) {
  298. saveOptions.writeConcern.j = options.writeConcern.j;
  299. }
  300. if ('wtimeout' in options.writeConcern) {
  301. saveOptions.writeConcern.wtimeout = options.writeConcern.wtimeout;
  302. }
  303. } else {
  304. if ('w' in options) {
  305. saveOptions.w = options.w;
  306. }
  307. if ('j' in options) {
  308. saveOptions.j = options.j;
  309. }
  310. if ('wtimeout' in options) {
  311. saveOptions.wtimeout = options.wtimeout;
  312. }
  313. }
  314. if ('checkKeys' in options) {
  315. saveOptions.checkKeys = options.checkKeys;
  316. }
  317. const session = doc.$session();
  318. const asyncLocalStorage = doc[modelDbSymbol].base.transactionAsyncLocalStorage?.getStore();
  319. if (session != null) {
  320. saveOptions.session = session;
  321. } else if (!Object.hasOwn(options, 'session') && asyncLocalStorage?.session != null) {
  322. // Only set session from asyncLocalStorage if `session` option wasn't originally passed in options
  323. saveOptions.session = asyncLocalStorage.session;
  324. }
  325. return saveOptions;
  326. }
  327. /*!
  328. * ignore
  329. */
  330. Model.prototype.$__save = async function $__save(options) {
  331. try {
  332. await this._execDocumentPreHooks('save', options);
  333. } catch (error) {
  334. await this._execDocumentPostHooks('save', error);
  335. return;
  336. }
  337. let result = null;
  338. let where = null;
  339. try {
  340. const saveOptions = _createSaveOptions(this, options);
  341. if (this.$isNew) {
  342. // send entire doc
  343. const obj = this.toObject(saveToObjectOptions);
  344. if ((obj || {})._id === void 0) {
  345. // documents must have an _id else mongoose won't know
  346. // what to update later if more changes are made. the user
  347. // wouldn't know what _id was generated by mongodb either
  348. // nor would the ObjectId generated by mongodb necessarily
  349. // match the schema definition.
  350. throw new MongooseError('document must have an _id before saving');
  351. }
  352. this.$__version(true, obj);
  353. this.$__reset();
  354. _setIsNew(this, false);
  355. // Make it possible to retry the insert
  356. this.$__.inserting = true;
  357. result = await this[modelCollectionSymbol].insertOne(obj, saveOptions).catch(err => {
  358. _setIsNew(this, true);
  359. throw err;
  360. });
  361. } else {
  362. // Make sure we don't treat it as a new object on error,
  363. // since it already exists
  364. this.$__.inserting = false;
  365. const delta = this.$__delta();
  366. if (options.pathsToSave) {
  367. for (const key in delta[1]['$set']) {
  368. if (options.pathsToSave.includes(key)) {
  369. continue;
  370. } else if (options.pathsToSave.some(pathToSave => key.slice(0, pathToSave.length) === pathToSave && key.charAt(pathToSave.length) === '.')) {
  371. continue;
  372. } else {
  373. delete delta[1]['$set'][key];
  374. }
  375. }
  376. }
  377. if (delta) {
  378. where = this.$__where(delta[0]);
  379. _applyCustomWhere(this, where);
  380. const update = delta[1];
  381. if (this.$__schema.options.minimize) {
  382. for (const updateOp of Object.values(update)) {
  383. if (updateOp == null) {
  384. continue;
  385. }
  386. for (const key of Object.keys(updateOp)) {
  387. if (updateOp[key] == null || typeof updateOp[key] !== 'object') {
  388. continue;
  389. }
  390. if (!utils.isPOJO(updateOp[key])) {
  391. continue;
  392. }
  393. minimize(updateOp[key]);
  394. if (utils.hasOwnKeys(updateOp[key]) === false) {
  395. delete updateOp[key];
  396. update.$unset = update.$unset || {};
  397. update.$unset[key] = 1;
  398. }
  399. }
  400. }
  401. }
  402. // store the modified paths before the document is reset
  403. this.$__.modifiedPaths = this.modifiedPaths();
  404. this.$__reset();
  405. _setIsNew(this, false);
  406. result = await this[modelCollectionSymbol].updateOne(where, update, saveOptions).catch(err => {
  407. this.$__undoReset();
  408. throw err;
  409. });
  410. } else {
  411. where = this.$__where();
  412. _applyCustomWhere(this, where);
  413. if (this.$__.version) {
  414. this.$__version(where, delta);
  415. }
  416. applyReadConcern(this.$__schema, saveOptions);
  417. result = await this.constructor.collection.findOne(where, { ...saveOptions, projection: { _id: 1 } })
  418. .then(documentExists => ({ matchedCount: !documentExists ? 0 : 1 }));
  419. }
  420. }
  421. } catch (err) {
  422. const error = this.$__schema._transformDuplicateKeyError(err);
  423. await this._execDocumentPostHooks('save', error);
  424. return;
  425. }
  426. let numAffected = 0;
  427. const writeConcern = options != null ?
  428. options.writeConcern != null ?
  429. options.writeConcern.w :
  430. options.w :
  431. 0;
  432. if (writeConcern !== 0) {
  433. // Skip checking if write succeeded if writeConcern is set to
  434. // unacknowledged writes, because otherwise `numAffected` will always be 0
  435. if (result != null) {
  436. if (Array.isArray(result)) {
  437. numAffected = result.length;
  438. } else if (result.matchedCount != null) {
  439. numAffected = result.matchedCount;
  440. } else {
  441. numAffected = result;
  442. }
  443. }
  444. const versionBump = this.$__.version;
  445. // was this an update that required a version bump?
  446. if (versionBump && !this.$__.inserting) {
  447. const doIncrement = VERSION_INC === (VERSION_INC & this.$__.version);
  448. this.$__.version = undefined;
  449. const key = this.$__schema.options.versionKey;
  450. const version = this.$__getValue(key) || 0;
  451. if (numAffected <= 0) {
  452. // the update failed. pass an error back
  453. this.$__undoReset();
  454. const err = this.$__.$versionError ||
  455. new VersionError(this, version, this.$__.modifiedPaths);
  456. await this._execDocumentPostHooks('save', err);
  457. return;
  458. }
  459. // increment version if was successful
  460. if (doIncrement) {
  461. this.$__setValue(key, version + 1);
  462. }
  463. }
  464. if (result != null && numAffected <= 0) {
  465. this.$__undoReset();
  466. const error = new DocumentNotFoundError(where, this.constructor.modelName, numAffected, result);
  467. await this._execDocumentPostHooks('save', error);
  468. return;
  469. }
  470. }
  471. this.$__.saving = undefined;
  472. this.$__.savedState = {};
  473. this.$emit('save', this, numAffected);
  474. this.constructor.emit('save', this, numAffected);
  475. await this._execDocumentPostHooks('save');
  476. };
  477. /*!
  478. * ignore
  479. */
  480. function generateVersionError(doc, modifiedPaths, defaultPaths) {
  481. const key = doc.$__schema.options.versionKey;
  482. if (!key) {
  483. return null;
  484. }
  485. const version = doc.$__getValue(key) || 0;
  486. return new VersionError(doc, version, modifiedPaths.concat(defaultPaths));
  487. }
  488. /**
  489. * Saves this document by inserting a new document into the database if [document.isNew](https://mongoosejs.com/docs/api/document.html#Document.prototype.isNew) is `true`,
  490. * or sends an [updateOne](https://mongoosejs.com/docs/api/document.html#Document.prototype.updateOne()) operation with just the modified paths if `isNew` is `false`.
  491. *
  492. * #### Example:
  493. *
  494. * product.sold = Date.now();
  495. * product = await product.save();
  496. *
  497. * If save is successful, the returned promise will fulfill with the document
  498. * saved.
  499. *
  500. * #### Example:
  501. *
  502. * const newProduct = await product.save();
  503. * newProduct === product; // true
  504. *
  505. * @param {Object} [options] options optional options
  506. * @param {Session} [options.session=null] the [session](https://www.mongodb.com/docs/manual/reference/server-sessions/) associated with this save operation. If not specified, defaults to the [document's associated session](https://mongoosejs.com/docs/api/document.html#Document.prototype.session()).
  507. * @param {Object} [options.safe] (DEPRECATED) overrides [schema's safe option](https://mongoosejs.com/docs/guide.html#safe). Use the `w` option instead.
  508. * @param {Boolean} [options.validateBeforeSave] set to false to save without validating.
  509. * @param {Boolean} [options.validateModifiedOnly=false] if `true`, Mongoose will only validate modified paths, as opposed to modified paths and `required` paths.
  510. * @param {Number|String} [options.w] set the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/#w-option). Overrides the [schema-level `writeConcern` option](https://mongoosejs.com/docs/guide.html#writeConcern)
  511. * @param {Boolean} [options.j] set to true for MongoDB to wait until this `save()` has been [journaled before resolving the returned promise](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option). Overrides the [schema-level `writeConcern` option](https://mongoosejs.com/docs/guide.html#writeConcern)
  512. * @param {Number} [options.wtimeout] sets a [timeout for the write concern](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout). Overrides the [schema-level `writeConcern` option](https://mongoosejs.com/docs/guide.html#writeConcern).
  513. * @param {Boolean} [options.checkKeys=true] the MongoDB driver prevents you from saving keys that start with '$' or contain '.' by default. Set this option to `false` to skip that check. See [restrictions on field names](https://docs.mongodb.com/manual/reference/limits/#mongodb-limit-Restrictions-on-Field-Names)
  514. * @param {Boolean} [options.timestamps=true] if `false` and [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this `save()`.
  515. * @param {Array} [options.pathsToSave] An array of paths that tell mongoose to only validate and save the paths in `pathsToSave`.
  516. * @throws {DocumentNotFoundError} if this [save updates an existing document](https://mongoosejs.com/docs/api/document.html#Document.prototype.isNew) but the document doesn't exist in the database. For example, you will get this error if the document is [deleted between when you retrieved the document and when you saved it](documents.html#updating).
  517. * @return {Promise}
  518. * @api public
  519. * @see middleware https://mongoosejs.com/docs/middleware.html
  520. */
  521. Model.prototype.save = async function save(options) {
  522. if (typeof options === 'function' || typeof arguments[1] === 'function') {
  523. throw new MongooseError('Model.prototype.save() no longer accepts a callback');
  524. }
  525. let parallelSave;
  526. this.$op = 'save';
  527. if (this.$__.saving) {
  528. parallelSave = new ParallelSaveError(this);
  529. } else {
  530. this.$__.saving = new ParallelSaveError(this);
  531. }
  532. options = new SaveOptions(options);
  533. if (Object.hasOwn(options, 'session')) {
  534. this.$session(options.session);
  535. }
  536. if (this.$__.timestamps != null) {
  537. options.timestamps = this.$__.timestamps;
  538. }
  539. this.$__.$versionError = generateVersionError(
  540. this,
  541. this.modifiedPaths(),
  542. Object.keys(this.$__.activePaths.getStatePaths('default'))
  543. );
  544. if (parallelSave) {
  545. this.$__handleReject(parallelSave);
  546. throw parallelSave;
  547. }
  548. this.$__.saveOptions = options;
  549. try {
  550. await this.$__save(options);
  551. } catch (error) {
  552. this.$__handleReject(error);
  553. throw error;
  554. } finally {
  555. this.$__.saving = null;
  556. this.$__.saveOptions = null;
  557. this.$__.$versionError = null;
  558. this.$op = null;
  559. }
  560. return this;
  561. };
  562. Model.prototype.$save = Model.prototype.save;
  563. /**
  564. * Appends versioning to the where and update clauses.
  565. *
  566. * @api private
  567. * @method $__version
  568. * @memberOf Model
  569. * @instance
  570. */
  571. Model.prototype.$__version = function(where, delta) {
  572. const key = this.$__schema.options.versionKey;
  573. if (where === true) {
  574. // this is an insert
  575. if (key) {
  576. setDottedPath(delta, key, 0);
  577. this.$__setValue(key, 0);
  578. }
  579. return;
  580. }
  581. if (key === false) {
  582. return;
  583. }
  584. // updates
  585. // only apply versioning if our versionKey was selected. else
  586. // there is no way to select the correct version. we could fail
  587. // fast here and force them to include the versionKey but
  588. // thats a bit intrusive. can we do this automatically?
  589. if (!this.$__isSelected(key)) {
  590. return;
  591. }
  592. // $push $addToSet don't need the where clause set
  593. if (VERSION_WHERE === (VERSION_WHERE & this.$__.version)) {
  594. const value = this.$__getValue(key);
  595. if (value != null) where[key] = value;
  596. }
  597. if (VERSION_INC === (VERSION_INC & this.$__.version)) {
  598. if (get(delta.$set, key, null) != null) {
  599. // Version key is getting set, means we'll increment the doc's version
  600. // after a successful save, so we should set the incremented version so
  601. // future saves don't fail (gh-5779)
  602. ++delta.$set[key];
  603. } else {
  604. delta.$inc = delta.$inc || {};
  605. delta.$inc[key] = 1;
  606. }
  607. }
  608. };
  609. /**
  610. * Signal that we desire an increment of this documents version.
  611. *
  612. * #### Example:
  613. *
  614. * const doc = await Model.findById(id);
  615. * doc.increment();
  616. * await doc.save();
  617. *
  618. * @see versionKeys https://mongoosejs.com/docs/guide.html#versionKey
  619. * @memberOf Model
  620. * @method increment
  621. * @api public
  622. */
  623. Model.prototype.increment = function increment() {
  624. this.$__.version = VERSION_ALL;
  625. return this;
  626. };
  627. /**
  628. * Returns a query object
  629. *
  630. * @api private
  631. * @method $__where
  632. * @memberOf Model
  633. * @instance
  634. */
  635. Model.prototype.$__where = function _where(where) {
  636. where || (where = {});
  637. if (!where._id) {
  638. where._id = this._doc._id;
  639. }
  640. if (this._doc._id === void 0) {
  641. throw new MongooseError('No _id found on document!');
  642. }
  643. return where;
  644. };
  645. /**
  646. * Delete this document from the db. Returns a Query instance containing a `deleteOne` operation by this document's `_id`.
  647. *
  648. * #### Example:
  649. *
  650. * await product.deleteOne();
  651. * await Product.findById(product._id); // null
  652. *
  653. * Since `deleteOne()` returns a Query, the `deleteOne()` will **not** execute unless you use either `await`, `.then()`, `.catch()`, or [`.exec()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.exec())
  654. *
  655. * #### Example:
  656. *
  657. * product.deleteOne(); // Doesn't do anything
  658. * product.deleteOne().exec(); // Deletes the document, returns a promise
  659. *
  660. * @return {Query} Query
  661. * @api public
  662. */
  663. Model.prototype.deleteOne = function deleteOne(options) {
  664. if (typeof options === 'function' ||
  665. typeof arguments[1] === 'function') {
  666. throw new MongooseError('Model.prototype.deleteOne() no longer accepts a callback');
  667. }
  668. if (!options) {
  669. options = {};
  670. }
  671. if (Object.hasOwn(options, 'session')) {
  672. this.$session(options.session);
  673. }
  674. const self = this;
  675. const where = this.$__where();
  676. const query = self.constructor.deleteOne();
  677. if (this.$session() != null) {
  678. if (!('session' in query.options)) {
  679. query.options.session = this.$session();
  680. }
  681. }
  682. query.pre(async function queryPreDeleteOne() {
  683. const res = await self.constructor._middleware.execPre('deleteOne', self, [self, options]);
  684. // `self` is passed to pre hooks as argument for backwards compatibility, but that
  685. // isn't the actual arguments passed to the wrapped function.
  686. if (res[0] !== self || res[1] !== options) {
  687. throw new Error('Document deleteOne pre hooks cannot overwrite arguments');
  688. }
  689. query.deleteOne(where, options);
  690. // Apply custom where conditions _after_ document deleteOne middleware for
  691. // consistency with save() - sharding plugin needs to set $where
  692. if (self.$where != null) {
  693. this.where(self.$where);
  694. }
  695. return res;
  696. });
  697. query.pre(function callSubdocPreHooks() {
  698. return Promise.all(self.$getAllSubdocs().map(subdoc => subdoc.constructor._middleware.execPre('deleteOne', subdoc, [subdoc])));
  699. });
  700. query.pre(function skipIfAlreadyDeleted() {
  701. if (self.$__.isDeleted) {
  702. throw new Kareem.skipWrappedFunction();
  703. }
  704. });
  705. query.post(function callSubdocPostHooks() {
  706. return Promise.all(self.$getAllSubdocs().map(subdoc => subdoc.constructor._middleware.execPost('deleteOne', subdoc, [subdoc])));
  707. });
  708. query.post(function queryPostDeleteOne() {
  709. return self.constructor._middleware.execPost('deleteOne', self, [self], {});
  710. });
  711. query.transform(function setIsDeleted(result) {
  712. if (result?.deletedCount > 0) {
  713. self.$isDeleted(true);
  714. }
  715. return result;
  716. });
  717. return query;
  718. };
  719. /**
  720. * Returns the model instance used to create this document if no `name` specified.
  721. * If `name` specified, returns the model with the given `name`.
  722. *
  723. * #### Example:
  724. *
  725. * const doc = new Tank({});
  726. * doc.$model() === Tank; // true
  727. * await doc.$model('User').findById(id);
  728. *
  729. * @param {String} [name] model name
  730. * @method $model
  731. * @api public
  732. * @return {Model}
  733. */
  734. Model.prototype.$model = function $model(name) {
  735. if (arguments.length === 0) {
  736. return this.constructor;
  737. }
  738. return this[modelDbSymbol].model(name);
  739. };
  740. /**
  741. * Returns the model instance used to create this document if no `name` specified.
  742. * If `name` specified, returns the model with the given `name`.
  743. *
  744. * #### Example:
  745. *
  746. * const doc = new Tank({});
  747. * doc.$model() === Tank; // true
  748. * await doc.$model('User').findById(id);
  749. *
  750. * @param {String} [name] model name
  751. * @method model
  752. * @api public
  753. * @return {Model}
  754. */
  755. Model.prototype.model = Model.prototype.$model;
  756. /**
  757. * Returns a document with `_id` only if at least one document exists in the database that matches
  758. * the given `filter`, and `null` otherwise.
  759. *
  760. * Under the hood, `MyModel.exists({ answer: 42 })` is equivalent to
  761. * `MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean()`
  762. *
  763. * #### Example:
  764. *
  765. * await Character.deleteMany({});
  766. * await Character.create({ name: 'Jean-Luc Picard' });
  767. *
  768. * await Character.exists({ name: /picard/i }); // { _id: ... }
  769. * await Character.exists({ name: /riker/i }); // null
  770. *
  771. * This function triggers the following middleware.
  772. *
  773. * - `findOne()`
  774. *
  775. * @param {Object} filter
  776. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  777. * @return {Query}
  778. */
  779. Model.exists = function exists(filter, options) {
  780. _checkContext(this, 'exists');
  781. if (typeof arguments[2] === 'function') {
  782. throw new MongooseError('Model.exists() no longer accepts a callback');
  783. }
  784. const query = this.findOne(filter).
  785. select({ _id: 1 }).
  786. lean().
  787. setOptions(options);
  788. return query;
  789. };
  790. /**
  791. * Adds a discriminator type.
  792. *
  793. * #### Example:
  794. *
  795. * function BaseSchema() {
  796. * Schema.apply(this, arguments);
  797. *
  798. * this.add({
  799. * name: String,
  800. * createdAt: Date
  801. * });
  802. * }
  803. * util.inherits(BaseSchema, Schema);
  804. *
  805. * const PersonSchema = new BaseSchema();
  806. * const BossSchema = new BaseSchema({ department: String });
  807. *
  808. * const Person = mongoose.model('Person', PersonSchema);
  809. * const Boss = Person.discriminator('Boss', BossSchema);
  810. * new Boss().__t; // "Boss". `__t` is the default `discriminatorKey`
  811. *
  812. * const employeeSchema = new Schema({ boss: ObjectId });
  813. * const Employee = Person.discriminator('Employee', employeeSchema, 'staff');
  814. * new Employee().__t; // "staff" because of 3rd argument above
  815. *
  816. * @param {String} name discriminator model name
  817. * @param {Schema} schema discriminator model schema
  818. * @param {Object|String} [options] If string, same as `options.value`.
  819. * @param {String} [options.value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
  820. * @param {Boolean} [options.clone=true] By default, `discriminator()` clones the given `schema`. Set to `false` to skip cloning.
  821. * @param {Boolean} [options.overwriteModels=false] by default, Mongoose does not allow you to define a discriminator with the same name as another discriminator. Set this to allow overwriting discriminators with the same name.
  822. * @param {Boolean} [options.mergeHooks=true] By default, Mongoose merges the base schema's hooks with the discriminator schema's hooks. Set this option to `false` to make Mongoose use the discriminator schema's hooks instead.
  823. * @param {Boolean} [options.mergePlugins=true] By default, Mongoose merges the base schema's plugins with the discriminator schema's plugins. Set this option to `false` to make Mongoose use the discriminator schema's plugins instead.
  824. * @return {Model} The newly created discriminator model
  825. * @api public
  826. */
  827. Model.discriminator = function(name, schema, options) {
  828. let model;
  829. if (typeof name === 'function') {
  830. model = name;
  831. name = utils.getFunctionName(model);
  832. if (!(model.prototype instanceof Model)) {
  833. throw new MongooseError('The provided class ' + name + ' must extend Model');
  834. }
  835. }
  836. options = options || {};
  837. const value = utils.isPOJO(options) ? options.value : options;
  838. const clone = typeof options.clone === 'boolean' ? options.clone : true;
  839. const mergePlugins = typeof options.mergePlugins === 'boolean' ? options.mergePlugins : true;
  840. const overwriteModels = typeof options.overwriteModels === 'boolean' ? options.overwriteModels : false;
  841. _checkContext(this, 'discriminator');
  842. if (utils.isObject(schema) && !schema.instanceOfSchema) {
  843. schema = new Schema(schema);
  844. }
  845. if (schema instanceof Schema && clone) {
  846. schema = schema.clone();
  847. }
  848. schema = discriminator(this, name, schema, value, mergePlugins, options.mergeHooks, overwriteModels);
  849. if (this.db.models[name] && !schema.options.overwriteModels && !overwriteModels) {
  850. throw new OverwriteModelError(name);
  851. }
  852. schema.$isRootDiscriminator = true;
  853. schema.$globalPluginsApplied = true;
  854. model = this.db.model(model || name, schema, this.$__collection.name);
  855. this.discriminators[name] = model;
  856. const d = this.discriminators[name];
  857. Object.setPrototypeOf(d.prototype, this.prototype);
  858. Object.defineProperty(d, 'baseModelName', {
  859. value: this.modelName,
  860. configurable: true,
  861. writable: false
  862. });
  863. // apply methods and statics
  864. applyMethods(d, schema);
  865. applyStatics(d, schema);
  866. if (this[subclassedSymbol] != null) {
  867. for (const submodel of this[subclassedSymbol]) {
  868. submodel.discriminators = submodel.discriminators || {};
  869. submodel.discriminators[name] =
  870. model.__subclass(model.db, schema, submodel.collection.name);
  871. }
  872. }
  873. return d;
  874. };
  875. /**
  876. * Make sure `this` is a model
  877. * @api private
  878. */
  879. function _checkContext(ctx, fnName) {
  880. // Check context, because it is easy to mistakenly type
  881. // `new Model.discriminator()` and get an incomprehensible error
  882. if (ctx == null || ctx === global) {
  883. throw new MongooseError('`Model.' + fnName + '()` cannot run without a ' +
  884. 'model as `this`. Make sure you are calling `MyModel.' + fnName + '()` ' +
  885. 'where `MyModel` is a Mongoose model.');
  886. } else if (ctx[modelSymbol] == null) {
  887. throw new MongooseError('`Model.' + fnName + '()` cannot run without a ' +
  888. 'model as `this`. Make sure you are not calling ' +
  889. '`new Model.' + fnName + '()`');
  890. }
  891. }
  892. // Model (class) features
  893. /*!
  894. * Give the constructor the ability to emit events.
  895. */
  896. for (const i in EventEmitter.prototype) {
  897. Model[i] = EventEmitter.prototype[i];
  898. }
  899. /**
  900. * This function is responsible for initializing the underlying connection in MongoDB based on schema options.
  901. * This function performs the following operations:
  902. *
  903. * - `createCollection()` unless [`autoCreate`](https://mongoosejs.com/docs/guide.html#autoCreate) option is turned off
  904. * - `ensureIndexes()` unless [`autoIndex`](https://mongoosejs.com/docs/guide.html#autoIndex) option is turned off
  905. * - `createSearchIndex()` on all schema search indexes if `autoSearchIndex` is enabled.
  906. *
  907. * Mongoose calls this function automatically when a model is a created using
  908. * [`mongoose.model()`](https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.model()) or
  909. * [`connection.model()`](https://mongoosejs.com/docs/api/connection.html#Connection.prototype.model()), so you
  910. * don't need to call `init()` to trigger index builds.
  911. *
  912. * However, you _may_ need to call `init()` to get back a promise that will resolve when your indexes are finished.
  913. * Calling `await Model.init()` is helpful if you need to wait for indexes to build before continuing.
  914. * For example, if you want to wait for unique indexes to build before continuing with a test case.
  915. *
  916. * #### Example:
  917. *
  918. * const eventSchema = new Schema({ thing: { type: 'string', unique: true } })
  919. * // This calls `Event.init()` implicitly, so you don't need to call
  920. * // `Event.init()` on your own.
  921. * const Event = mongoose.model('Event', eventSchema);
  922. *
  923. * await Event.init();
  924. * console.log('Indexes are done building!');
  925. *
  926. * @api public
  927. * @returns {Promise}
  928. */
  929. Model.init = function init() {
  930. _checkContext(this, 'init');
  931. if (typeof arguments[0] === 'function') {
  932. throw new MongooseError('Model.init() no longer accepts a callback');
  933. }
  934. this.schema.emit('init', this);
  935. if (this.$init != null) {
  936. return this.$init;
  937. }
  938. const conn = this.db;
  939. const _ensureIndexes = async() => {
  940. const autoIndex = utils.getOption(
  941. 'autoIndex',
  942. this.schema.options,
  943. conn.config,
  944. conn.base.options
  945. );
  946. if (!autoIndex) {
  947. return;
  948. }
  949. return await this.ensureIndexes({ _automatic: true });
  950. };
  951. const _createSearchIndexes = async() => {
  952. const autoSearchIndex = utils.getOption(
  953. 'autoSearchIndex',
  954. this.schema.options,
  955. conn.config,
  956. conn.base.options
  957. );
  958. if (!autoSearchIndex) {
  959. return;
  960. }
  961. return await this.createSearchIndexes();
  962. };
  963. const _createCollection = async() => {
  964. let autoCreate = utils.getOption(
  965. 'autoCreate',
  966. this.schema.options,
  967. conn.config
  968. // No base.options here because we don't want to take the base value if the connection hasn't
  969. // set it yet
  970. );
  971. if (autoCreate == null) {
  972. // `autoCreate` may later be set when the connection is opened, so wait for connect before checking
  973. await conn._waitForConnect(true);
  974. autoCreate = utils.getOption(
  975. 'autoCreate',
  976. this.schema.options,
  977. conn.config,
  978. conn.base.options
  979. );
  980. }
  981. if (!autoCreate) {
  982. return;
  983. }
  984. return await this.createCollection();
  985. };
  986. this.$init = _createCollection().
  987. then(() => _ensureIndexes()).
  988. then(() => _createSearchIndexes());
  989. const _catch = this.$init.catch;
  990. const _this = this;
  991. this.$init.catch = function() {
  992. _this.$caught = true;
  993. return _catch.apply(_this.$init, arguments);
  994. };
  995. return this.$init;
  996. };
  997. /**
  998. * Create the collection for this model. By default, if no indexes are specified,
  999. * mongoose will not create the collection for the model until any documents are
  1000. * created. Use this method to create the collection explicitly.
  1001. *
  1002. * Note 1: You may need to call this before starting a transaction
  1003. * See https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-operations
  1004. *
  1005. * Note 2: You don't have to call this if your schema contains index or unique field.
  1006. * In that case, just use `Model.init()`
  1007. *
  1008. * #### Example:
  1009. *
  1010. * const userSchema = new Schema({ name: String })
  1011. * const User = mongoose.model('User', userSchema);
  1012. *
  1013. * User.createCollection().then(function(collection) {
  1014. * console.log('Collection is created!');
  1015. * });
  1016. *
  1017. * @api public
  1018. * @param {Object} [options] see [MongoDB driver docs](https://mongodb.github.io/node-mongodb-native/4.9/classes/Db.html#createCollection)
  1019. * @returns {Promise}
  1020. */
  1021. Model.createCollection = async function createCollection(options) {
  1022. _checkContext(this, 'createCollection');
  1023. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {
  1024. throw new MongooseError('Model.createCollection() no longer accepts a callback');
  1025. }
  1026. [options] = await this.hooks.execPre('createCollection', this, [options]).catch(err => {
  1027. if (err instanceof Kareem.skipWrappedFunction) {
  1028. return [err];
  1029. }
  1030. throw err;
  1031. });
  1032. const collectionOptions = this?.schema?.options?.collectionOptions;
  1033. if (collectionOptions != null) {
  1034. options = Object.assign({}, collectionOptions, options);
  1035. }
  1036. const schemaCollation = this?.schema?.options?.collation;
  1037. if (schemaCollation != null) {
  1038. options = Object.assign({ collation: schemaCollation }, options);
  1039. }
  1040. const capped = this?.schema?.options?.capped;
  1041. if (capped != null) {
  1042. if (typeof capped === 'number') {
  1043. options = Object.assign({ capped: true, size: capped }, options);
  1044. } else if (typeof capped === 'object') {
  1045. options = Object.assign({ capped: true }, capped, options);
  1046. }
  1047. }
  1048. const timeseries = this?.schema?.options?.timeseries;
  1049. if (timeseries != null) {
  1050. options = Object.assign({ timeseries }, options);
  1051. if (options.expireAfterSeconds != null) {
  1052. // do nothing
  1053. } else if (options.expires != null) {
  1054. utils.expires(options);
  1055. } else if (this.schema.options.expireAfterSeconds != null) {
  1056. options.expireAfterSeconds = this.schema.options.expireAfterSeconds;
  1057. } else if (this.schema.options.expires != null) {
  1058. options.expires = this.schema.options.expires;
  1059. utils.expires(options);
  1060. }
  1061. }
  1062. const clusteredIndex = this?.schema?.options?.clusteredIndex;
  1063. if (clusteredIndex != null) {
  1064. options = Object.assign({ clusteredIndex: { ...clusteredIndex, unique: true } }, options);
  1065. }
  1066. try {
  1067. if (!(options instanceof Kareem.skipWrappedFunction)) {
  1068. await this.db.createCollection(this.$__collection.collectionName, options);
  1069. }
  1070. } catch (err) {
  1071. if (err != null && (err.name !== 'MongoServerError' || err.code !== 48)) {
  1072. await this.hooks.execPost('createCollection', this, [null], { error: err });
  1073. }
  1074. }
  1075. await this.hooks.execPost('createCollection', this, [this.$__collection]);
  1076. return this.$__collection;
  1077. };
  1078. /**
  1079. * Makes the indexes in MongoDB match the indexes defined in this model's
  1080. * schema. This function will drop any indexes that are not defined in
  1081. * the model's schema except the `_id` index, and build any indexes that
  1082. * are in your schema but not in MongoDB.
  1083. *
  1084. * See the [introductory blog post](https://thecodebarbarian.com/whats-new-in-mongoose-5-2-syncindexes)
  1085. * for more information.
  1086. *
  1087. * #### Example:
  1088. *
  1089. * const schema = new Schema({ name: { type: String, unique: true } });
  1090. * const Customer = mongoose.model('Customer', schema);
  1091. * await Customer.collection.createIndex({ age: 1 }); // Index is not in schema
  1092. * // Will drop the 'age' index and create an index on `name`
  1093. * await Customer.syncIndexes();
  1094. *
  1095. * You should be careful about running `syncIndexes()` on production applications under heavy load,
  1096. * because index builds are expensive operations, and unexpected index drops can lead to degraded
  1097. * performance. Before running `syncIndexes()`, you can use the [`diffIndexes()` function](#Model.diffIndexes())
  1098. * to check what indexes `syncIndexes()` will drop and create.
  1099. *
  1100. * #### Example:
  1101. *
  1102. * const { toDrop, toCreate } = await Model.diffIndexes();
  1103. * toDrop; // Array of strings containing names of indexes that `syncIndexes()` will drop
  1104. * toCreate; // Array of strings containing names of indexes that `syncIndexes()` will create
  1105. *
  1106. * @param {Object} [options] options to pass to `ensureIndexes()`
  1107. * @param {Boolean} [options.hideIndexes=false] set to `true` to hide indexes instead of dropping. Requires MongoDB server 4.4 or higher
  1108. * @return {Promise}
  1109. * @api public
  1110. */
  1111. Model.syncIndexes = async function syncIndexes(options) {
  1112. _checkContext(this, 'syncIndexes');
  1113. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {
  1114. throw new MongooseError('Model.syncIndexes() no longer accepts a callback');
  1115. }
  1116. const autoCreate = options?.autoCreate ??
  1117. this.schema.options?.autoCreate ??
  1118. this.db.config.autoCreate ??
  1119. this.db.base?.options?.autoCreate ??
  1120. true;
  1121. if (autoCreate) {
  1122. try {
  1123. await this.createCollection();
  1124. } catch (err) {
  1125. if (err != null && (err.name !== 'MongoServerError' || err.code !== 48)) {
  1126. throw err;
  1127. }
  1128. }
  1129. }
  1130. const diffIndexesResult = await this.diffIndexes({ indexOptionsToCreate: true });
  1131. const dropped = await this.cleanIndexes({ ...options, toDrop: diffIndexesResult.toDrop });
  1132. await this.createIndexes({ ...options, toCreate: diffIndexesResult.toCreate });
  1133. return dropped;
  1134. };
  1135. /**
  1136. * Create an [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/).
  1137. * This function only works when connected to MongoDB Atlas.
  1138. *
  1139. * #### Example:
  1140. *
  1141. * const schema = new Schema({ name: { type: String, unique: true } });
  1142. * const Customer = mongoose.model('Customer', schema);
  1143. * await Customer.createSearchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });
  1144. *
  1145. * @param {Object} description index options, including `name` and `definition`
  1146. * @param {String} description.name
  1147. * @param {Object} description.definition
  1148. * @return {Promise}
  1149. * @api public
  1150. */
  1151. Model.createSearchIndex = async function createSearchIndex(description) {
  1152. _checkContext(this, 'createSearchIndex');
  1153. return await this.$__collection.createSearchIndex(description);
  1154. };
  1155. /**
  1156. * Update an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/).
  1157. * This function only works when connected to MongoDB Atlas.
  1158. *
  1159. * #### Example:
  1160. *
  1161. * const schema = new Schema({ name: { type: String, unique: true } });
  1162. * const Customer = mongoose.model('Customer', schema);
  1163. * await Customer.updateSearchIndex('test', { mappings: { dynamic: true } });
  1164. *
  1165. * @param {String} name
  1166. * @param {Object} definition
  1167. * @return {Promise}
  1168. * @api public
  1169. */
  1170. Model.updateSearchIndex = async function updateSearchIndex(name, definition) {
  1171. _checkContext(this, 'updateSearchIndex');
  1172. return await this.$__collection.updateSearchIndex(name, definition);
  1173. };
  1174. /**
  1175. * Delete an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) by name.
  1176. * This function only works when connected to MongoDB Atlas.
  1177. *
  1178. * #### Example:
  1179. *
  1180. * const schema = new Schema({ name: { type: String, unique: true } });
  1181. * const Customer = mongoose.model('Customer', schema);
  1182. * await Customer.dropSearchIndex('test');
  1183. *
  1184. * @param {String} name
  1185. * @return {Promise}
  1186. * @api public
  1187. */
  1188. Model.dropSearchIndex = async function dropSearchIndex(name) {
  1189. _checkContext(this, 'dropSearchIndex');
  1190. return await this.$__collection.dropSearchIndex(name);
  1191. };
  1192. /**
  1193. * List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
  1194. * This function only works when connected to MongoDB Atlas.
  1195. *
  1196. * #### Example:
  1197. *
  1198. * const schema = new Schema({ name: { type: String, unique: true } });
  1199. * const Customer = mongoose.model('Customer', schema);
  1200. *
  1201. * await Customer.createSearchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });
  1202. * const res = await Customer.listSearchIndexes(); // Includes `[{ name: 'test' }]`
  1203. *
  1204. * @param {Object} [options]
  1205. * @return {Promise<Array>}
  1206. * @api public
  1207. */
  1208. Model.listSearchIndexes = async function listSearchIndexes(options) {
  1209. _checkContext(this, 'listSearchIndexes');
  1210. const cursor = await this.$__collection.listSearchIndexes(options);
  1211. return await cursor.toArray();
  1212. };
  1213. /**
  1214. * Does a dry-run of `Model.syncIndexes()`, returning the indexes that `syncIndexes()` would drop and create if you were to run `syncIndexes()`.
  1215. *
  1216. * #### Example:
  1217. *
  1218. * const { toDrop, toCreate } = await Model.diffIndexes();
  1219. * toDrop; // Array of strings containing names of indexes that `syncIndexes()` will drop
  1220. * toCreate; // Array of index specs containing the keys of indexes that `syncIndexes()` will create
  1221. *
  1222. * @param {Object} [options]
  1223. * @param {Boolean} [options.indexOptionsToCreate=false] If true, `toCreate` will include both the index spec and the index options, not just the index spec
  1224. * @return {Promise<Object>} contains the indexes that would be dropped in MongoDB and indexes that would be created in MongoDB as `{ toDrop: string[], toCreate: string[] }`.
  1225. */
  1226. Model.diffIndexes = async function diffIndexes(options) {
  1227. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {
  1228. throw new MongooseError('Model.syncIndexes() no longer accepts a callback');
  1229. }
  1230. const model = this;
  1231. let dbIndexes = await model.listIndexes().catch(err => {
  1232. if (err.codeName == 'NamespaceNotFound') {
  1233. return undefined;
  1234. }
  1235. throw err;
  1236. });
  1237. if (dbIndexes === undefined) {
  1238. dbIndexes = [];
  1239. }
  1240. dbIndexes = getRelatedDBIndexes(model, dbIndexes);
  1241. const schema = model.schema;
  1242. const schemaIndexes = getRelatedSchemaIndexes(model, schema.indexes());
  1243. const toDrop = getIndexesToDrop(schema, schemaIndexes, dbIndexes);
  1244. const toCreate = getIndexesToCreate(schema, schemaIndexes, dbIndexes, toDrop, options);
  1245. return { toDrop, toCreate };
  1246. };
  1247. function getIndexesToCreate(schema, schemaIndexes, dbIndexes, toDrop, options) {
  1248. const toCreate = [];
  1249. const indexOptionsToCreate = options?.indexOptionsToCreate ?? false;
  1250. for (const [schemaIndexKeysObject, schemaIndexOptions] of schemaIndexes) {
  1251. let found = false;
  1252. const options = decorateDiscriminatorIndexOptions(schema, clone(schemaIndexOptions));
  1253. for (const index of dbIndexes) {
  1254. if (isDefaultIdIndex(index)) {
  1255. continue;
  1256. }
  1257. if (
  1258. isIndexEqual(schemaIndexKeysObject, options, index) &&
  1259. !toDrop.includes(index.name)
  1260. ) {
  1261. found = true;
  1262. break;
  1263. }
  1264. }
  1265. if (!found) {
  1266. if (indexOptionsToCreate) {
  1267. toCreate.push([schemaIndexKeysObject, schemaIndexOptions]);
  1268. } else {
  1269. toCreate.push(schemaIndexKeysObject);
  1270. }
  1271. }
  1272. }
  1273. return toCreate;
  1274. }
  1275. function getIndexesToDrop(schema, schemaIndexes, dbIndexes) {
  1276. const toDrop = [];
  1277. for (const dbIndex of dbIndexes) {
  1278. let found = false;
  1279. // Never try to drop `_id` index, MongoDB server doesn't allow it
  1280. if (isDefaultIdIndex(dbIndex)) {
  1281. continue;
  1282. }
  1283. // Timeseries collections have a default index on { timeField: 1, metaField: 1 }.
  1284. if (isTimeseriesIndex(dbIndex, schema.options)) {
  1285. continue;
  1286. }
  1287. for (const [schemaIndexKeysObject, schemaIndexOptions] of schemaIndexes) {
  1288. const options = decorateDiscriminatorIndexOptions(schema, clone(schemaIndexOptions));
  1289. applySchemaCollation(schemaIndexKeysObject, options, schema.options);
  1290. if (isIndexEqual(schemaIndexKeysObject, options, dbIndex)) {
  1291. found = true;
  1292. break;
  1293. }
  1294. }
  1295. if (found) {
  1296. continue;
  1297. }
  1298. toDrop.push(dbIndex.name);
  1299. }
  1300. return toDrop;
  1301. }
  1302. /**
  1303. * Deletes all indexes that aren't defined in this model's schema. Used by
  1304. * `syncIndexes()`.
  1305. *
  1306. * The returned promise resolves to a list of the dropped indexes' names as an array
  1307. *
  1308. * @param {Object} [options]
  1309. * @param {Array<String>} [options.toDrop] if specified, contains a list of index names to drop
  1310. * @param {Boolean} [options.hideIndexes=false] set to `true` to hide indexes instead of dropping. Requires MongoDB server 4.4 or higher
  1311. * @return {Promise<Array<String>>} list of dropped or hidden index names
  1312. * @api public
  1313. */
  1314. Model.cleanIndexes = async function cleanIndexes(options) {
  1315. _checkContext(this, 'cleanIndexes');
  1316. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {
  1317. throw new MongooseError('Model.cleanIndexes() no longer accepts a callback');
  1318. }
  1319. const model = this;
  1320. if (Array.isArray(options?.toDrop)) {
  1321. const res = await _dropIndexes(options.toDrop, model, options);
  1322. return res;
  1323. }
  1324. const res = await model.diffIndexes();
  1325. return await _dropIndexes(res.toDrop, model, options);
  1326. };
  1327. async function _dropIndexes(toDrop, model, options) {
  1328. if (toDrop.length === 0) {
  1329. return [];
  1330. }
  1331. const collection = model.$__collection;
  1332. if (options?.hideIndexes) {
  1333. await Promise.all(toDrop.map(indexName => {
  1334. return model.db.db.command({
  1335. collMod: collection.collectionName,
  1336. index: { name: indexName, hidden: true }
  1337. });
  1338. }));
  1339. } else {
  1340. await Promise.all(toDrop.map(indexName => collection.dropIndex(indexName)));
  1341. }
  1342. return toDrop;
  1343. }
  1344. /**
  1345. * Lists the indexes currently defined in MongoDB. This may or may not be
  1346. * the same as the indexes defined in your schema depending on whether you
  1347. * use the [`autoIndex` option](https://mongoosejs.com/docs/guide.html#autoIndex) and if you
  1348. * build indexes manually.
  1349. *
  1350. * @return {Promise}
  1351. * @api public
  1352. */
  1353. Model.listIndexes = async function listIndexes() {
  1354. _checkContext(this, 'listIndexes');
  1355. if (typeof arguments[0] === 'function') {
  1356. throw new MongooseError('Model.listIndexes() no longer accepts a callback');
  1357. }
  1358. if (this.$__collection.buffer) {
  1359. await new Promise(resolve => {
  1360. this.$__collection.addQueue(resolve);
  1361. });
  1362. }
  1363. return this.$__collection.listIndexes().toArray();
  1364. };
  1365. /**
  1366. * Sends `createIndex` commands to mongo for each index declared in the schema.
  1367. * The `createIndex` commands are sent in series.
  1368. *
  1369. * #### Example:
  1370. *
  1371. * await Event.ensureIndexes();
  1372. *
  1373. * After completion, an `index` event is emitted on this `Model` passing an error if one occurred.
  1374. *
  1375. * #### Example:
  1376. *
  1377. * const eventSchema = new Schema({ thing: { type: 'string', unique: true } })
  1378. * const Event = mongoose.model('Event', eventSchema);
  1379. *
  1380. * Event.on('index', function (err) {
  1381. * if (err) console.error(err); // error occurred during index creation
  1382. * });
  1383. *
  1384. * _NOTE: It is not recommended that you run this in production. Index creation may impact database performance depending on your load. Use with caution._
  1385. *
  1386. * @param {Object} [options] internal options
  1387. * @return {Promise}
  1388. * @api public
  1389. */
  1390. Model.ensureIndexes = async function ensureIndexes(options) {
  1391. _checkContext(this, 'ensureIndexes');
  1392. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {
  1393. throw new MongooseError('Model.ensureIndexes() no longer accepts a callback');
  1394. }
  1395. await new Promise((resolve, reject) => {
  1396. _ensureIndexes(this, options, (err) => {
  1397. if (err != null) {
  1398. return reject(err);
  1399. }
  1400. resolve();
  1401. });
  1402. });
  1403. };
  1404. /**
  1405. * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/4.9/classes/Db.html#createIndex)
  1406. * function.
  1407. *
  1408. * @param {Object} [options] internal options
  1409. * @return {Promise}
  1410. * @api public
  1411. */
  1412. Model.createIndexes = async function createIndexes(options) {
  1413. _checkContext(this, 'createIndexes');
  1414. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {
  1415. throw new MongooseError('Model.createIndexes() no longer accepts a callback');
  1416. }
  1417. return this.ensureIndexes(options);
  1418. };
  1419. /*!
  1420. * ignore
  1421. */
  1422. function _ensureIndexes(model, options, callback) {
  1423. const indexes = Array.isArray(options?.toCreate) ? options.toCreate : model.schema.indexes();
  1424. let indexError;
  1425. options = options || {};
  1426. const done = function(err) {
  1427. if (err && !model.$caught) {
  1428. model.emit('error', err);
  1429. }
  1430. model.emit('index', err || indexError);
  1431. callback && callback(err || indexError);
  1432. };
  1433. for (const index of indexes) {
  1434. if (isDefaultIdIndex(index)) {
  1435. utils.warn('mongoose: Cannot specify a custom index on `_id` for ' +
  1436. 'model name "' + model.modelName + '", ' +
  1437. 'MongoDB does not allow overwriting the default `_id` index. See ' +
  1438. 'https://bit.ly/mongodb-id-index');
  1439. }
  1440. }
  1441. if (!indexes.length) {
  1442. immediate(function() {
  1443. done();
  1444. });
  1445. return;
  1446. }
  1447. // Indexes are created one-by-one
  1448. const indexSingleDone = function(err, fields, options, name) {
  1449. model.emit('index-single-done', err, fields, options, name);
  1450. };
  1451. const indexSingleStart = function(fields, options) {
  1452. model.emit('index-single-start', fields, options);
  1453. };
  1454. const baseSchema = model.schema._baseSchema;
  1455. const baseSchemaIndexes = baseSchema ? baseSchema.indexes() : [];
  1456. immediate(function() {
  1457. // If buffering is off, do this manually.
  1458. if (options._automatic && !model.collection.collection) {
  1459. model.collection.addQueue(create, []);
  1460. } else {
  1461. create();
  1462. }
  1463. });
  1464. function create() {
  1465. if (options._automatic) {
  1466. if (model.schema.options.autoIndex === false ||
  1467. (model.schema.options.autoIndex == null && model.db.config.autoIndex === false)) {
  1468. return done();
  1469. }
  1470. }
  1471. const index = indexes.shift();
  1472. if (!index) {
  1473. return done();
  1474. }
  1475. if (options._automatic && index[1]._autoIndex === false) {
  1476. return create();
  1477. }
  1478. if (baseSchemaIndexes.find(i => utils.deepEqual(i, index))) {
  1479. return create();
  1480. }
  1481. const indexFields = clone(index[0]);
  1482. const indexOptions = clone(index[1]);
  1483. delete indexOptions._autoIndex;
  1484. decorateDiscriminatorIndexOptions(model.schema, indexOptions);
  1485. applyWriteConcern(model.schema, indexOptions);
  1486. applySchemaCollation(indexFields, indexOptions, model.schema.options);
  1487. indexSingleStart(indexFields, options);
  1488. // Just in case `createIndex()` throws a sync error
  1489. let promise = null;
  1490. try {
  1491. promise = model.collection.createIndex(indexFields, indexOptions);
  1492. } catch (err) {
  1493. if (!indexError) {
  1494. indexError = err;
  1495. }
  1496. if (!model.$caught) {
  1497. model.emit('error', err);
  1498. }
  1499. indexSingleDone(err, indexFields, indexOptions);
  1500. create();
  1501. return;
  1502. }
  1503. promise.then(
  1504. name => {
  1505. indexSingleDone(null, indexFields, indexOptions, name);
  1506. create();
  1507. },
  1508. err => {
  1509. if (!indexError) {
  1510. indexError = err;
  1511. }
  1512. if (!model.$caught) {
  1513. model.emit('error', err);
  1514. }
  1515. indexSingleDone(err, indexFields, indexOptions);
  1516. create();
  1517. }
  1518. );
  1519. }
  1520. }
  1521. /**
  1522. * Creates all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) defined in this model's schema.
  1523. * This function only works when connected to MongoDB Atlas.
  1524. *
  1525. * #### Example:
  1526. *
  1527. * const schema = new Schema({
  1528. * name: String,
  1529. * description: String
  1530. * });
  1531. * schema.searchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });
  1532. * const Product = mongoose.model('Product', schema);
  1533. *
  1534. * // Creates the search index defined in the schema
  1535. * await Product.createSearchIndexes();
  1536. *
  1537. * @api public
  1538. * @return {Promise} resolves to the results of creating the search indexes
  1539. */
  1540. Model.createSearchIndexes = async function createSearchIndexes() {
  1541. _checkContext(this, 'createSearchIndexes');
  1542. const results = [];
  1543. for (const searchIndex of this.schema._searchIndexes) {
  1544. results.push(await this.createSearchIndex(searchIndex));
  1545. }
  1546. return results;
  1547. };
  1548. /**
  1549. * Schema the model uses.
  1550. *
  1551. * @property schema
  1552. * @static
  1553. * @api public
  1554. * @memberOf Model
  1555. */
  1556. Model.schema;
  1557. /**
  1558. * Connection instance the model uses.
  1559. *
  1560. * @property db
  1561. * @static
  1562. * @api public
  1563. * @memberOf Model
  1564. */
  1565. Model.db;
  1566. /**
  1567. * Collection the model uses.
  1568. *
  1569. * @property collection
  1570. * @api public
  1571. * @memberOf Model
  1572. */
  1573. Model.collection;
  1574. /**
  1575. * Internal collection the model uses.
  1576. *
  1577. * @property collection
  1578. * @api private
  1579. * @memberOf Model
  1580. */
  1581. Model.$__collection;
  1582. /**
  1583. * Base Mongoose instance the model uses.
  1584. *
  1585. * @property base
  1586. * @api public
  1587. * @memberOf Model
  1588. */
  1589. Model.base;
  1590. /**
  1591. * Registered discriminators for this model.
  1592. *
  1593. * @property discriminators
  1594. * @api public
  1595. * @memberOf Model
  1596. */
  1597. Model.discriminators;
  1598. /**
  1599. * Translate any aliases fields/conditions so the final query or document object is pure
  1600. *
  1601. * #### Example:
  1602. *
  1603. * await Character.find(Character.translateAliases({
  1604. * '名': 'Eddard Stark' // Alias for 'name'
  1605. * });
  1606. *
  1607. * By default, `translateAliases()` overwrites raw fields with aliased fields.
  1608. * So if `n` is an alias for `name`, `{ n: 'alias', name: 'raw' }` will resolve to `{ name: 'alias' }`.
  1609. * However, you can set the `errorOnDuplicates` option to throw an error if there are potentially conflicting paths.
  1610. * The `translateAliases` option for queries uses `errorOnDuplicates`.
  1611. *
  1612. * #### Note:
  1613. *
  1614. * Only translate arguments of object type anything else is returned raw
  1615. *
  1616. * @param {Object} fields fields/conditions that may contain aliased keys
  1617. * @param {Boolean} [errorOnDuplicates] if true, throw an error if there's both a key and an alias for that key in `fields`
  1618. * @return {Object} the translated 'pure' fields/conditions
  1619. */
  1620. Model.translateAliases = function translateAliases(fields, errorOnDuplicates) {
  1621. _checkContext(this, 'translateAliases');
  1622. const translate = (key, value) => {
  1623. let alias;
  1624. const translated = [];
  1625. const fieldKeys = key.split('.');
  1626. let currentSchema = this.schema;
  1627. for (const i in fieldKeys) {
  1628. const name = fieldKeys[i];
  1629. if (currentSchema?.aliases[name]) {
  1630. alias = currentSchema.aliases[name];
  1631. if (errorOnDuplicates && alias in fields) {
  1632. throw new MongooseError(`Provided object has both field "${name}" and its alias "${alias}"`);
  1633. }
  1634. // Alias found,
  1635. translated.push(alias);
  1636. } else {
  1637. alias = name;
  1638. // Alias not found, so treat as un-aliased key
  1639. translated.push(name);
  1640. }
  1641. // Check if aliased path is a schema
  1642. if (currentSchema?.paths[alias]) {
  1643. currentSchema = currentSchema.paths[alias].schema;
  1644. }
  1645. else
  1646. currentSchema = null;
  1647. }
  1648. const translatedKey = translated.join('.');
  1649. if (fields instanceof Map)
  1650. fields.set(translatedKey, value);
  1651. else
  1652. fields[translatedKey] = value;
  1653. if (translatedKey !== key) {
  1654. // We'll be using the translated key instead
  1655. if (fields instanceof Map) {
  1656. // Delete from map
  1657. fields.delete(key);
  1658. } else {
  1659. // Delete from object
  1660. delete fields[key]; // We'll be using the translated key instead
  1661. }
  1662. }
  1663. return fields;
  1664. };
  1665. if (typeof fields === 'object') {
  1666. // Fields is an object (query conditions or document fields)
  1667. if (fields instanceof Map) {
  1668. // A Map was supplied
  1669. for (const field of new Map(fields)) {
  1670. fields = translate(field[0], field[1]);
  1671. }
  1672. } else {
  1673. // Infer a regular object was supplied
  1674. for (const key of Object.keys(fields)) {
  1675. fields = translate(key, fields[key]);
  1676. if (key[0] === '$') {
  1677. if (Array.isArray(fields[key])) {
  1678. for (const i in fields[key]) {
  1679. // Recursively translate nested queries
  1680. fields[key][i] = this.translateAliases(fields[key][i]);
  1681. }
  1682. } else {
  1683. this.translateAliases(fields[key]);
  1684. }
  1685. }
  1686. }
  1687. }
  1688. return fields;
  1689. } else {
  1690. // Don't know typeof fields
  1691. return fields;
  1692. }
  1693. };
  1694. /**
  1695. * Deletes the first document that matches `conditions` from the collection.
  1696. * It returns an object with the property `deletedCount` indicating how many documents were deleted.
  1697. *
  1698. * #### Example:
  1699. *
  1700. * await Character.deleteOne({ name: 'Eddard Stark' }); // returns {deletedCount: 1}
  1701. *
  1702. * #### Note:
  1703. *
  1704. * This function triggers `deleteOne` query hooks. Read the
  1705. * [middleware docs](https://mongoosejs.com/docs/middleware.html#naming) to learn more.
  1706. *
  1707. * @param {Object} conditions
  1708. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  1709. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  1710. * @return {Query}
  1711. * @api public
  1712. */
  1713. Model.deleteOne = function deleteOne(conditions, options) {
  1714. _checkContext(this, 'deleteOne');
  1715. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
  1716. throw new MongooseError('Model.prototype.deleteOne() no longer accepts a callback');
  1717. }
  1718. const mq = new this.Query({}, {}, this, this.$__collection);
  1719. mq.setOptions(options);
  1720. return mq.deleteOne(conditions);
  1721. };
  1722. /**
  1723. * Deletes all of the documents that match `conditions` from the collection.
  1724. * It returns an object with the property `deletedCount` containing the number of documents deleted.
  1725. *
  1726. * #### Example:
  1727. *
  1728. * await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }); // returns {deletedCount: x} where x is the number of documents deleted.
  1729. *
  1730. * #### Note:
  1731. *
  1732. * This function triggers `deleteMany` query hooks. Read the
  1733. * [middleware docs](https://mongoosejs.com/docs/middleware.html#naming) to learn more.
  1734. *
  1735. * @param {Object} conditions
  1736. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  1737. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  1738. * @return {Query}
  1739. * @api public
  1740. */
  1741. Model.deleteMany = function deleteMany(conditions, options) {
  1742. _checkContext(this, 'deleteMany');
  1743. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
  1744. throw new MongooseError('Model.deleteMany() no longer accepts a callback');
  1745. }
  1746. const mq = new this.Query({}, {}, this, this.$__collection);
  1747. mq.setOptions(options);
  1748. return mq.deleteMany(conditions);
  1749. };
  1750. /**
  1751. * Finds documents.
  1752. *
  1753. * Mongoose casts the `filter` to match the model's schema before the command is sent.
  1754. * See our [query casting tutorial](https://mongoosejs.com/docs/tutorials/query_casting.html) for
  1755. * more information on how Mongoose casts `filter`.
  1756. *
  1757. * #### Example:
  1758. *
  1759. * // find all documents
  1760. * await MyModel.find({});
  1761. *
  1762. * // find all documents named john and at least 18
  1763. * await MyModel.find({ name: 'john', age: { $gte: 18 } }).exec();
  1764. *
  1765. * // executes, name LIKE john and only selecting the "name" and "friends" fields
  1766. * await MyModel.find({ name: /john/i }, 'name friends').exec();
  1767. *
  1768. * // passing options
  1769. * await MyModel.find({ name: /john/i }, null, { skip: 10 }).exec();
  1770. *
  1771. * @param {Object|ObjectId} filter
  1772. * @param {Object|String|String[]} [projection] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
  1773. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  1774. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  1775. * @return {Query}
  1776. * @see field selection https://mongoosejs.com/docs/api/query.html#Query.prototype.select()
  1777. * @see query casting https://mongoosejs.com/docs/tutorials/query_casting.html
  1778. * @api public
  1779. */
  1780. Model.find = function find(conditions, projection, options) {
  1781. _checkContext(this, 'find');
  1782. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function' || typeof arguments[3] === 'function') {
  1783. throw new MongooseError('Model.find() no longer accepts a callback');
  1784. }
  1785. const mq = new this.Query({}, {}, this, this.$__collection);
  1786. mq.select(projection);
  1787. mq.setOptions(options);
  1788. return mq.find(conditions);
  1789. };
  1790. /**
  1791. * Finds a single document by its _id field. `findById(id)` is equivalent to `findOne({ _id: id })`.
  1792. *
  1793. * The `id` is cast based on the Schema before sending the command.
  1794. *
  1795. * This function triggers the following middleware.
  1796. *
  1797. * - `findOne()`
  1798. *
  1799. * #### Example:
  1800. *
  1801. * // Find the adventure with the given `id`, or `null` if not found
  1802. * await Adventure.findById(id).exec();
  1803. *
  1804. * // select only the adventures name and length
  1805. * await Adventure.findById(id, 'name length').exec();
  1806. *
  1807. * @param {Any} id value of `_id` to query by
  1808. * @param {Object|String|String[]} [projection] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
  1809. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  1810. * @return {Query}
  1811. * @see field selection https://mongoosejs.com/docs/api/query.html#Query.prototype.select()
  1812. * @see lean queries https://mongoosejs.com/docs/tutorials/lean.html
  1813. * @see findById in Mongoose https://masteringjs.io/tutorials/mongoose/find-by-id
  1814. * @api public
  1815. */
  1816. Model.findById = function findById(id, projection, options) {
  1817. _checkContext(this, 'findById');
  1818. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
  1819. throw new MongooseError('Model.findById() no longer accepts a callback');
  1820. }
  1821. return this.findOne({ _id: id }, projection, options);
  1822. };
  1823. /**
  1824. * Finds one document.
  1825. *
  1826. * The `conditions` are cast to their respective SchemaTypes before the command is sent.
  1827. *
  1828. * *Note:* `conditions` is optional, and if `conditions` is null or undefined,
  1829. * mongoose will send an empty `findOne` command to MongoDB, which will return
  1830. * an arbitrary document. If you're querying by `_id`, use `findById()` instead.
  1831. *
  1832. * #### Example:
  1833. *
  1834. * // Find one adventure whose `country` is 'Croatia', otherwise `null`
  1835. * await Adventure.findOne({ country: 'Croatia' }).exec();
  1836. *
  1837. * // Model.findOne() no longer accepts a callback
  1838. *
  1839. * // Select only the adventures name and length
  1840. * await Adventure.findOne({ country: 'Croatia' }, 'name length').exec();
  1841. *
  1842. * @param {Object} [conditions]
  1843. * @param {Object|String|String[]} [projection] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
  1844. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  1845. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  1846. * @return {Query}
  1847. * @see field selection https://mongoosejs.com/docs/api/query.html#Query.prototype.select()
  1848. * @see lean queries https://mongoosejs.com/docs/tutorials/lean.html
  1849. * @api public
  1850. */
  1851. Model.findOne = function findOne(conditions, projection, options) {
  1852. _checkContext(this, 'findOne');
  1853. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
  1854. throw new MongooseError('Model.findOne() no longer accepts a callback');
  1855. }
  1856. const mq = new this.Query({}, {}, this, this.$__collection);
  1857. mq.select(projection);
  1858. mq.setOptions(options);
  1859. return mq.findOne(conditions);
  1860. };
  1861. /**
  1862. * Estimates the number of documents in the MongoDB collection. Faster than
  1863. * using `countDocuments()` for large collections because
  1864. * `estimatedDocumentCount()` uses collection metadata rather than scanning
  1865. * the entire collection.
  1866. *
  1867. * #### Example:
  1868. *
  1869. * const numAdventures = await Adventure.estimatedDocumentCount();
  1870. *
  1871. * @param {Object} [options]
  1872. * @return {Query}
  1873. * @api public
  1874. */
  1875. Model.estimatedDocumentCount = function estimatedDocumentCount(options) {
  1876. _checkContext(this, 'estimatedDocumentCount');
  1877. const mq = new this.Query({}, {}, this, this.$__collection);
  1878. return mq.estimatedDocumentCount(options);
  1879. };
  1880. /**
  1881. * Counts number of documents matching `filter` in a database collection.
  1882. *
  1883. * #### Example:
  1884. *
  1885. * const count = await Adventure.countDocuments({ type: 'jungle' });
  1886. * console.log('there are %d jungle adventures', count);
  1887. *
  1888. * If you want to count all documents in a large collection,
  1889. * use the [`estimatedDocumentCount()` function](https://mongoosejs.com/docs/api/model.html#Model.estimatedDocumentCount())
  1890. * instead. If you call `countDocuments({})`, MongoDB will always execute
  1891. * a full collection scan and **not** use any indexes.
  1892. *
  1893. * The `countDocuments()` function is similar to `count()`, but there are a
  1894. * [few operators that `countDocuments()` does not support](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#countDocuments).
  1895. * Below are the operators that `count()` supports but `countDocuments()` does not,
  1896. * and the suggested replacement:
  1897. *
  1898. * - `$where`: [`$expr`](https://www.mongodb.com/docs/manual/reference/operator/query/expr/)
  1899. * - `$near`: [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/) with [`$center`](https://www.mongodb.com/docs/manual/reference/operator/query/center/#op._S_center)
  1900. * - `$nearSphere`: [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/) with [`$centerSphere`](https://www.mongodb.com/docs/manual/reference/operator/query/centerSphere/#op._S_centerSphere)
  1901. *
  1902. * @param {Object} filter
  1903. * @return {Query}
  1904. * @api public
  1905. */
  1906. Model.countDocuments = function countDocuments(conditions, options) {
  1907. _checkContext(this, 'countDocuments');
  1908. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
  1909. throw new MongooseError('Model.countDocuments() no longer accepts a callback');
  1910. }
  1911. const mq = new this.Query({}, {}, this, this.$__collection);
  1912. if (options != null) {
  1913. mq.setOptions(options);
  1914. }
  1915. return mq.countDocuments(conditions);
  1916. };
  1917. /**
  1918. * Creates a Query for a `distinct` operation.
  1919. *
  1920. * #### Example:
  1921. *
  1922. * const query = Link.distinct('url');
  1923. * query.exec();
  1924. *
  1925. * @param {String} field
  1926. * @param {Object} [conditions] optional
  1927. * @param {Object} [options] optional
  1928. * @return {Query}
  1929. * @api public
  1930. */
  1931. Model.distinct = function distinct(field, conditions, options) {
  1932. _checkContext(this, 'distinct');
  1933. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
  1934. throw new MongooseError('Model.distinct() no longer accepts a callback');
  1935. }
  1936. const mq = new this.Query({}, {}, this, this.$__collection);
  1937. if (options != null) {
  1938. mq.setOptions(options);
  1939. }
  1940. return mq.distinct(field, conditions);
  1941. };
  1942. /**
  1943. * Creates a Query, applies the passed conditions, and returns the Query.
  1944. *
  1945. * For example, instead of writing:
  1946. *
  1947. * User.find({ age: { $gte: 21, $lte: 65 } });
  1948. *
  1949. * we can instead write:
  1950. *
  1951. * User.where('age').gte(21).lte(65).exec();
  1952. *
  1953. * Since the Query class also supports `where` you can continue chaining
  1954. *
  1955. * User
  1956. * .where('age').gte(21).lte(65)
  1957. * .where('name', /^b/i)
  1958. * ... etc
  1959. *
  1960. * @param {String} path
  1961. * @param {Object} [val] optional value
  1962. * @return {Query}
  1963. * @api public
  1964. */
  1965. Model.where = function where(path, val) {
  1966. _checkContext(this, 'where');
  1967. void val; // eslint
  1968. const mq = new this.Query({}, {}, this, this.$__collection).find({});
  1969. return mq.where.apply(mq, arguments);
  1970. };
  1971. /**
  1972. * Creates a `Query` and specifies a `$where` condition.
  1973. *
  1974. * Sometimes you need to query for things in mongodb using a JavaScript expression. You can do so via `find({ $where: javascript })`, or you can use the mongoose shortcut method $where via a Query chain or from your mongoose Model.
  1975. *
  1976. * Blog.$where('this.username.indexOf("val") !== -1').exec(function (err, docs) {});
  1977. *
  1978. * @param {String|Function} argument is a javascript string or anonymous function
  1979. * @method $where
  1980. * @memberOf Model
  1981. * @return {Query}
  1982. * @see Query.$where https://mongoosejs.com/docs/api/query.html#Query.prototype.$where
  1983. * @api public
  1984. */
  1985. Model.$where = function $where() {
  1986. _checkContext(this, '$where');
  1987. const mq = new this.Query({}, {}, this, this.$__collection).find({});
  1988. return mq.$where.apply(mq, arguments);
  1989. };
  1990. /**
  1991. * Issues a mongodb findOneAndUpdate command.
  1992. *
  1993. * Finds a matching document, updates it according to the `update` arg, passing any `options`. A Query object is returned.
  1994. *
  1995. * #### Example:
  1996. *
  1997. * A.findOneAndUpdate(filter, update, options); // returns Query
  1998. * A.findOneAndUpdate(filter, update); // returns Query
  1999. * A.findOneAndUpdate(filter); // returns Query
  2000. * A.findOneAndUpdate(); // returns Query
  2001. *
  2002. * // Other supported syntaxes
  2003. * // Note that calling `Query#findOneAndUpdate()` with 1 arg will treat the arg as `update`, NOT `filter`
  2004. * A.find(filter).findOneAndUpdate(update);
  2005. *
  2006. * #### Note:
  2007. *
  2008. * All top level update keys which are not `atomic` operation names are treated as set operations:
  2009. *
  2010. * #### Example:
  2011. *
  2012. * const query = { name: 'borne' };
  2013. * Model.findOneAndUpdate(query, { name: 'jason bourne' }, options);
  2014. *
  2015. * // is sent as
  2016. * Model.findOneAndUpdate(query, { $set: { name: 'jason bourne' }}, options);
  2017. *
  2018. * #### Note:
  2019. *
  2020. * `findOneAndX` and `findByIdAndX` functions support limited validation that
  2021. * you can enable by setting the `runValidators` option.
  2022. *
  2023. * If you need full-fledged validation, use the traditional approach of first
  2024. * retrieving the document.
  2025. *
  2026. * const doc = await Model.findById(id);
  2027. * doc.name = 'jason bourne';
  2028. * await doc.save();
  2029. *
  2030. * @param {Object} [conditions]
  2031. * @param {Object} [update]
  2032. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  2033. * @param {String} [options.returnDocument='before'] Has two possible values, `'before'` and `'after'`. By default, it will return the document before the update was applied.
  2034. * @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.lean()) and [the Mongoose lean tutorial](https://mongoosejs.com/docs/tutorials/lean.html).
  2035. * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
  2036. * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
  2037. * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
  2038. * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
  2039. * @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
  2040. * @param {Boolean} [options.new=false] if true, return the modified document rather than the original
  2041. * @param {Object|String} [options.fields] Field selection. Equivalent to `.select(fields).findOneAndUpdate()`
  2042. * @param {Number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
  2043. * @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
  2044. * @param {Boolean} [options.runValidators] if true, runs [update validators](https://mongoosejs.com/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema
  2045. * @param {Boolean} [options.setDefaultsOnInsert=true] If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created
  2046. * @param {Boolean} [options.includeResultMetadata] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ModifyResult.html)
  2047. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  2048. * @param {Boolean} [options.overwriteDiscriminatorKey=false] Mongoose removes discriminator key updates from `update` by default, set `overwriteDiscriminatorKey` to `true` to allow updating the discriminator key
  2049. * @return {Query}
  2050. * @see Tutorial https://mongoosejs.com/docs/tutorials/findoneandupdate.html
  2051. * @see mongodb https://www.mongodb.com/docs/manual/reference/command/findAndModify/
  2052. * @api public
  2053. */
  2054. Model.findOneAndUpdate = function(conditions, update, options) {
  2055. _checkContext(this, 'findOneAndUpdate');
  2056. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function' || typeof arguments[3] === 'function') {
  2057. throw new MongooseError('Model.findOneAndUpdate() no longer accepts a callback');
  2058. }
  2059. let fields;
  2060. if (options) {
  2061. fields = options.fields || options.projection;
  2062. }
  2063. update = clone(update, {
  2064. depopulate: true,
  2065. _isNested: true
  2066. });
  2067. decorateUpdateWithVersionKey(update, options, this.schema.options.versionKey);
  2068. const mq = new this.Query({}, {}, this, this.$__collection);
  2069. mq.select(fields);
  2070. return mq.findOneAndUpdate(conditions, update, options);
  2071. };
  2072. /**
  2073. * Issues a mongodb findOneAndUpdate command by a document's _id field.
  2074. * `findByIdAndUpdate(id, ...)` is equivalent to `findOneAndUpdate({ _id: id }, ...)`.
  2075. *
  2076. * Finds a matching document, updates it according to the `update` arg,
  2077. * passing any `options`, and returns the found document (if any).
  2078. *
  2079. * This function triggers the following middleware.
  2080. *
  2081. * - `findOneAndUpdate()`
  2082. *
  2083. * #### Example:
  2084. *
  2085. * A.findByIdAndUpdate(id, update, options) // returns Query
  2086. * A.findByIdAndUpdate(id, update) // returns Query
  2087. * A.findByIdAndUpdate() // returns Query
  2088. *
  2089. * #### Note:
  2090. *
  2091. * All top level update keys which are not `atomic` operation names are treated as set operations:
  2092. *
  2093. * #### Example:
  2094. *
  2095. * Model.findByIdAndUpdate(id, { name: 'jason bourne' }, options)
  2096. *
  2097. * // is sent as
  2098. * Model.findByIdAndUpdate(id, { $set: { name: 'jason bourne' }}, options)
  2099. *
  2100. * #### Note:
  2101. *
  2102. * `findOneAndX` and `findByIdAndX` functions support limited validation. You can
  2103. * enable validation by setting the `runValidators` option.
  2104. *
  2105. * If you need full-fledged validation, use the traditional approach of first
  2106. * retrieving the document.
  2107. *
  2108. * const doc = await Model.findById(id)
  2109. * doc.name = 'jason bourne';
  2110. * await doc.save();
  2111. *
  2112. * @param {Object|Number|String} id value of `_id` to query by
  2113. * @param {Object} [update]
  2114. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  2115. * @param {String} [options.returnDocument='before'] Has two possible values, `'before'` and `'after'`. By default, it will return the document before the update was applied.
  2116. * @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.lean()) and [the Mongoose lean tutorial](https://mongoosejs.com/docs/tutorials/lean.html).
  2117. * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
  2118. * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
  2119. * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
  2120. * @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
  2121. * @param {Boolean} [options.runValidators] if true, runs [update validators](https://mongoosejs.com/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema
  2122. * @param {Boolean} [options.setDefaultsOnInsert=true] If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created
  2123. * @param {Boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ModifyResult.html) rather than just the document
  2124. * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
  2125. * @param {Boolean} [options.new=false] if true, return the modified document rather than the original
  2126. * @param {Object|String} [options.select] sets the document fields to return.
  2127. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  2128. * @param {Boolean} [options.overwriteDiscriminatorKey=false] Mongoose removes discriminator key updates from `update` by default, set `overwriteDiscriminatorKey` to `true` to allow updating the discriminator key
  2129. * @return {Query}
  2130. * @see Model.findOneAndUpdate https://mongoosejs.com/docs/api/model.html#Model.findOneAndUpdate()
  2131. * @see mongodb https://www.mongodb.com/docs/manual/reference/command/findAndModify/
  2132. * @api public
  2133. */
  2134. Model.findByIdAndUpdate = function(id, update, options) {
  2135. _checkContext(this, 'findByIdAndUpdate');
  2136. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function' || typeof arguments[3] === 'function') {
  2137. throw new MongooseError('Model.findByIdAndUpdate() no longer accepts a callback');
  2138. }
  2139. // if a model is passed in instead of an id
  2140. if (id instanceof Document) {
  2141. id = id._doc._id;
  2142. }
  2143. return this.findOneAndUpdate.call(this, { _id: id }, update, options);
  2144. };
  2145. /**
  2146. * Issue a MongoDB `findOneAndDelete()` command.
  2147. *
  2148. * Finds a matching document, removes it, and returns the found document (if any).
  2149. *
  2150. * This function triggers the following middleware.
  2151. *
  2152. * - `findOneAndDelete()`
  2153. *
  2154. * #### Example:
  2155. *
  2156. * A.findOneAndDelete(conditions, options) // return Query
  2157. * A.findOneAndDelete(conditions) // returns Query
  2158. * A.findOneAndDelete() // returns Query
  2159. *
  2160. * `findOneAndX` and `findByIdAndX` functions support limited validation. You can
  2161. * enable validation by setting the `runValidators` option.
  2162. *
  2163. * If you need full-fledged validation, use the traditional approach of first
  2164. * retrieving the document.
  2165. *
  2166. * const doc = await Model.findById(id)
  2167. * doc.name = 'jason bourne';
  2168. * await doc.save();
  2169. *
  2170. * @param {Object} conditions
  2171. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  2172. * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
  2173. * @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
  2174. * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
  2175. * @param {Boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ModifyResult.html) rather than just the document
  2176. * @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
  2177. * @param {Object|String} [options.select] sets the document fields to return.
  2178. * @param {Number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
  2179. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  2180. * @return {Query}
  2181. * @api public
  2182. */
  2183. Model.findOneAndDelete = function(conditions, options) {
  2184. _checkContext(this, 'findOneAndDelete');
  2185. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
  2186. throw new MongooseError('Model.findOneAndDelete() no longer accepts a callback');
  2187. }
  2188. let fields;
  2189. if (options) {
  2190. fields = options.select;
  2191. options.select = undefined;
  2192. }
  2193. const mq = new this.Query({}, {}, this, this.$__collection);
  2194. mq.select(fields);
  2195. return mq.findOneAndDelete(conditions, options);
  2196. };
  2197. /**
  2198. * Issue a MongoDB `findOneAndDelete()` command by a document's _id field.
  2199. * In other words, `findByIdAndDelete(id)` is a shorthand for
  2200. * `findOneAndDelete({ _id: id })`.
  2201. *
  2202. * This function triggers the following middleware.
  2203. *
  2204. * - `findOneAndDelete()`
  2205. *
  2206. * @param {Object|Number|String} id value of `_id` to query by
  2207. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  2208. * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
  2209. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  2210. * @return {Query}
  2211. * @see Model.findOneAndDelete https://mongoosejs.com/docs/api/model.html#Model.findOneAndDelete()
  2212. * @see mongodb https://www.mongodb.com/docs/manual/reference/command/findAndModify/
  2213. */
  2214. Model.findByIdAndDelete = function(id, options) {
  2215. _checkContext(this, 'findByIdAndDelete');
  2216. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {
  2217. throw new MongooseError('Model.findByIdAndDelete() no longer accepts a callback');
  2218. }
  2219. return this.findOneAndDelete({ _id: id }, options);
  2220. };
  2221. /**
  2222. * Issue a MongoDB `findOneAndReplace()` command.
  2223. *
  2224. * Finds a matching document, replaces it with the provided doc, and returns the document.
  2225. *
  2226. * This function triggers the following query middleware.
  2227. *
  2228. * - `findOneAndReplace()`
  2229. *
  2230. * #### Example:
  2231. *
  2232. * A.findOneAndReplace(filter, replacement, options) // return Query
  2233. * A.findOneAndReplace(filter, replacement) // returns Query
  2234. * A.findOneAndReplace() // returns Query
  2235. *
  2236. * @param {Object} filter Replace the first document that matches this filter
  2237. * @param {Object} [replacement] Replace with this document
  2238. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  2239. * @param {String} [options.returnDocument='before'] Has two possible values, `'before'` and `'after'`. By default, it will return the document before the update was applied.
  2240. * @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.lean()) and [the Mongoose lean tutorial](https://mongoosejs.com/docs/tutorials/lean.html).
  2241. * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
  2242. * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
  2243. * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
  2244. * @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())
  2245. * @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
  2246. * @param {Boolean} [options.includeResultMetadata] if true, returns the full [ModifyResult from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ModifyResult.html) rather than just the document
  2247. * @param {Object|String} [options.select] sets the document fields to return.
  2248. * @param {Number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
  2249. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  2250. * @return {Query}
  2251. * @api public
  2252. */
  2253. Model.findOneAndReplace = function(filter, replacement, options) {
  2254. _checkContext(this, 'findOneAndReplace');
  2255. if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function' || typeof arguments[3] === 'function') {
  2256. throw new MongooseError('Model.findOneAndReplace() no longer accepts a callback');
  2257. }
  2258. let fields;
  2259. if (options) {
  2260. fields = options.select;
  2261. options.select = undefined;
  2262. }
  2263. const mq = new this.Query({}, {}, this, this.$__collection);
  2264. mq.select(fields);
  2265. return mq.findOneAndReplace(filter, replacement, options);
  2266. };
  2267. /**
  2268. * Shortcut for saving one or more documents to the database.
  2269. * `MyModel.create(docs)` does `new MyModel(doc).save()` for every doc in
  2270. * docs.
  2271. *
  2272. * This function triggers the following middleware.
  2273. *
  2274. * - `save()`
  2275. *
  2276. * #### Example:
  2277. *
  2278. * // Insert one new `Character` document
  2279. * await Character.create({ name: 'Jean-Luc Picard' });
  2280. *
  2281. * // Insert multiple new `Character` documents
  2282. * await Character.create([{ name: 'Will Riker' }, { name: 'Geordi LaForge' }]);
  2283. *
  2284. * // Create a new character within a transaction. Note that you **must**
  2285. * // pass an array as the first parameter to `create()` if you want to
  2286. * // specify options.
  2287. * await Character.create([{ name: 'Jean-Luc Picard' }], { session });
  2288. *
  2289. * @param {Array|Object} docs Documents to insert, as a spread or array
  2290. * @param {Object} [options] Options passed down to `save()`. To specify `options`, `docs` **must** be an array, not a spread. See [Model.save](https://mongoosejs.com/docs/api/model.html#Model.prototype.save()) for available options.
  2291. * @param {Boolean} [options.ordered] saves the docs in series rather than parallel.
  2292. * @param {Boolean} [options.aggregateErrors] Aggregate Errors instead of throwing the first one that occurs. Default: false
  2293. * @return {Promise}
  2294. * @api public
  2295. */
  2296. Model.create = async function create(doc, options) {
  2297. if (typeof options === 'function' ||
  2298. typeof arguments[2] === 'function') {
  2299. throw new MongooseError('Model.create() no longer accepts a callback');
  2300. }
  2301. _checkContext(this, 'create');
  2302. let args;
  2303. const discriminatorKey = this.schema.options.discriminatorKey;
  2304. if (Array.isArray(doc)) {
  2305. args = doc;
  2306. options = options != null && typeof options === 'object' ? options : {};
  2307. } else {
  2308. const last = arguments[arguments.length - 1];
  2309. options = {};
  2310. const hasCallback = typeof last === 'function' ||
  2311. typeof options === 'function' ||
  2312. typeof arguments[2] === 'function';
  2313. if (hasCallback) {
  2314. throw new MongooseError('Model.create() no longer accepts a callback');
  2315. } else {
  2316. args = [...arguments];
  2317. // For backwards compatibility with 6.x, because of gh-5061 Mongoose 6.x and
  2318. // older would treat a falsy last arg as a callback. We don't want to throw
  2319. // an error here, because it would look strange if `Test.create({}, void 0)`
  2320. // threw a callback error. But we also don't want to create an unnecessary document.
  2321. if (args.length > 1 && !last) {
  2322. args.pop();
  2323. }
  2324. }
  2325. if (args.length === 2 &&
  2326. args[0] != null &&
  2327. args[1] != null &&
  2328. args[0].session == null &&
  2329. last &&
  2330. getConstructorName(last.session) === 'ClientSession' &&
  2331. !this.schema.path('session')) {
  2332. // Probably means the user is running into the common mistake of trying
  2333. // to use a spread to specify options, see gh-7535
  2334. utils.warn('WARNING: to pass a `session` to `Model.create()` in ' +
  2335. 'Mongoose, you **must** pass an array as the first argument. See: ' +
  2336. 'https://mongoosejs.com/docs/api/model.html#Model.create()');
  2337. }
  2338. }
  2339. if (args.length === 0) {
  2340. return Array.isArray(doc) ? [] : null;
  2341. }
  2342. let res = [];
  2343. const immediateError = typeof options.aggregateErrors === 'boolean' ? !options.aggregateErrors : true;
  2344. delete options.aggregateErrors; // dont pass on the option to "$save"
  2345. if (options.session && !options.ordered && args.length > 1) {
  2346. throw new MongooseError('Cannot call `create()` with a session and multiple documents unless `ordered: true` is set');
  2347. }
  2348. if (options.ordered) {
  2349. for (let i = 0; i < args.length; i++) {
  2350. try {
  2351. const doc = args[i];
  2352. const Model = this.discriminators && doc[discriminatorKey] != null ?
  2353. this.discriminators[doc[discriminatorKey]] || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
  2354. this;
  2355. if (Model == null) {
  2356. throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
  2357. `found for model "${this.modelName}"`);
  2358. }
  2359. let toSave = doc;
  2360. if (!(toSave instanceof Model)) {
  2361. toSave = new Model(toSave);
  2362. }
  2363. await toSave.$save(options);
  2364. res.push(toSave);
  2365. } catch (err) {
  2366. if (!immediateError) {
  2367. res.push(err);
  2368. } else {
  2369. throw err;
  2370. }
  2371. }
  2372. }
  2373. return res;
  2374. } else if (!immediateError) {
  2375. res = await Promise.allSettled(args.map(async doc => {
  2376. const Model = this.discriminators && doc[discriminatorKey] != null ?
  2377. this.discriminators[doc[discriminatorKey]] || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
  2378. this;
  2379. if (Model == null) {
  2380. throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
  2381. `found for model "${this.modelName}"`);
  2382. }
  2383. let toSave = doc;
  2384. if (!(toSave instanceof Model)) {
  2385. toSave = new Model(toSave);
  2386. }
  2387. await toSave.$save(options);
  2388. return toSave;
  2389. }));
  2390. res = res.map(result => result.status === 'fulfilled' ? result.value : result.reason);
  2391. } else {
  2392. let firstError = null;
  2393. res = await Promise.all(args.map(async doc => {
  2394. const Model = this.discriminators && doc[discriminatorKey] != null ?
  2395. this.discriminators[doc[discriminatorKey]] || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
  2396. this;
  2397. if (Model == null) {
  2398. throw new MongooseError(`Discriminator "${doc[discriminatorKey]}" not ` +
  2399. `found for model "${this.modelName}"`);
  2400. }
  2401. try {
  2402. let toSave = doc;
  2403. if (!(toSave instanceof Model)) {
  2404. toSave = new Model(toSave);
  2405. }
  2406. await toSave.$save(options);
  2407. return toSave;
  2408. } catch (err) {
  2409. if (!firstError) {
  2410. firstError = err;
  2411. }
  2412. }
  2413. }));
  2414. if (firstError) {
  2415. throw firstError;
  2416. }
  2417. }
  2418. if (!Array.isArray(doc) && args.length === 1) {
  2419. return res[0];
  2420. }
  2421. return res;
  2422. };
  2423. /**
  2424. * Shortcut for saving one document to the database.
  2425. * `MyModel.insertOne(obj, options)` is almost equivalent to `new MyModel(obj).save(options)`.
  2426. * The difference is that `insertOne()` checks if `obj` is already a document, and checks for discriminators.
  2427. *
  2428. * This function triggers the following middleware.
  2429. *
  2430. * - `save()`
  2431. *
  2432. * #### Example:
  2433. *
  2434. * // Insert one new `Character` document
  2435. * const character = await Character.insertOne({ name: 'Jean-Luc Picard' });
  2436. * character.name; // 'Jean-Luc Picard'
  2437. *
  2438. * // Create a new character within a transaction.
  2439. * await Character.insertOne({ name: 'Jean-Luc Picard' }, { session });
  2440. *
  2441. * @param {Object|Document} doc Document to insert, as a POJO or Mongoose document
  2442. * @param {Object} [options] Options passed down to `save()`.
  2443. * @return {Promise<Document>} resolves to the saved document
  2444. * @api public
  2445. */
  2446. Model.insertOne = async function insertOne(doc, options) {
  2447. _checkContext(this, 'insertOne');
  2448. const discriminatorKey = this.schema.options.discriminatorKey;
  2449. const Model = this.discriminators && doc[discriminatorKey] != null ?
  2450. this.discriminators[doc[discriminatorKey]] || getDiscriminatorByValue(this.discriminators, doc[discriminatorKey]) :
  2451. this;
  2452. if (Model == null) {
  2453. throw new MongooseError(
  2454. `Discriminator "${doc[discriminatorKey]}" not found for model "${this.modelName}"`
  2455. );
  2456. }
  2457. if (!(doc instanceof Model)) {
  2458. doc = new Model(doc);
  2459. }
  2460. return await doc.$save(options);
  2461. };
  2462. /**
  2463. * _Requires a replica set running MongoDB >= 3.6.0._ Watches the
  2464. * underlying collection for changes using
  2465. * [MongoDB change streams](https://www.mongodb.com/docs/manual/changeStreams/).
  2466. *
  2467. * This function does **not** trigger any middleware. In particular, it
  2468. * does **not** trigger aggregate middleware.
  2469. *
  2470. * The ChangeStream object is an event emitter that emits the following events:
  2471. *
  2472. * - 'change': A change occurred, see below example
  2473. * - 'error': An unrecoverable error occurred. In particular, change streams currently error out if they lose connection to the replica set primary. Follow [this GitHub issue](https://github.com/Automattic/mongoose/issues/6799) for updates.
  2474. * - 'end': Emitted if the underlying stream is closed
  2475. * - 'close': Emitted if the underlying stream is closed
  2476. *
  2477. * #### Example:
  2478. *
  2479. * const doc = await Person.create({ name: 'Ned Stark' });
  2480. * const changeStream = Person.watch().on('change', change => console.log(change));
  2481. * // Will print from the above `console.log()`:
  2482. * // { _id: { _data: ... },
  2483. * // operationType: 'delete',
  2484. * // ns: { db: 'mydb', coll: 'Person' },
  2485. * // documentKey: { _id: 5a51b125c5500f5aa094c7bd } }
  2486. * await doc.deleteOne();
  2487. *
  2488. * @param {Array} [pipeline]
  2489. * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#watch)
  2490. * @param {Boolean} [options.hydrate=false] if true and `fullDocument: 'updateLookup'` is set, Mongoose will automatically hydrate `fullDocument` into a fully fledged Mongoose document
  2491. * @return {ChangeStream} mongoose-specific change stream wrapper, inherits from EventEmitter
  2492. * @api public
  2493. */
  2494. Model.watch = function(pipeline, options) {
  2495. _checkContext(this, 'watch');
  2496. options = options || {};
  2497. const watchOptions = options?.hydrate !== undefined ?
  2498. utils.omit(options, ['hydrate']) :
  2499. { ...options };
  2500. options.model = this;
  2501. const changeStreamThunk = cb => {
  2502. pipeline = pipeline || [];
  2503. prepareDiscriminatorPipeline(pipeline, this.schema, 'fullDocument');
  2504. if (this.$__collection.buffer) {
  2505. this.$__collection.addQueue(() => {
  2506. if (this.closed) {
  2507. return;
  2508. }
  2509. const driverChangeStream = this.$__collection.watch(pipeline, watchOptions);
  2510. cb(null, driverChangeStream);
  2511. });
  2512. } else {
  2513. const driverChangeStream = this.$__collection.watch(pipeline, watchOptions);
  2514. cb(null, driverChangeStream);
  2515. }
  2516. };
  2517. return new ChangeStream(changeStreamThunk, pipeline, options);
  2518. };
  2519. /**
  2520. * _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://www.mongodb.com/docs/manual/release-notes/3.6/#client-sessions)
  2521. * for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/),
  2522. * and [transactions](https://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
  2523. *
  2524. * Calling `MyModel.startSession()` is equivalent to calling `MyModel.db.startSession()`.
  2525. *
  2526. * This function does not trigger any middleware.
  2527. *
  2528. * #### Example:
  2529. *
  2530. * const session = await Person.startSession();
  2531. * let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
  2532. * await doc.deleteOne();
  2533. * // `doc` will always be null, even if reading from a replica set
  2534. * // secondary. Without causal consistency, it is possible to
  2535. * // get a doc back from the below query if the query reads from a
  2536. * // secondary that is experiencing replication lag.
  2537. * doc = await Person.findOne({ name: 'Ned Stark' }, null, { session, readPreference: 'secondary' });
  2538. *
  2539. * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html#startSession)
  2540. * @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
  2541. * @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
  2542. * @api public
  2543. */
  2544. Model.startSession = function() {
  2545. _checkContext(this, 'startSession');
  2546. return this.db.startSession.apply(this.db, arguments);
  2547. };
  2548. /**
  2549. * Shortcut for validating an array of documents and inserting them into
  2550. * MongoDB if they're all valid. This function is faster than `.create()`
  2551. * because it only sends one operation to the server, rather than one for each
  2552. * document.
  2553. *
  2554. * Mongoose always validates each document **before** sending `insertMany`
  2555. * to MongoDB. So if one document has a validation error, no documents will
  2556. * be saved, unless you set
  2557. * [the `ordered` option to false](https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/#error-handling).
  2558. *
  2559. * This function does **not** trigger save middleware.
  2560. *
  2561. * This function triggers the following middleware.
  2562. *
  2563. * - `insertMany()`
  2564. *
  2565. * #### Example:
  2566. *
  2567. * const docs = await Movies.insertMany([
  2568. * { name: 'Star Wars' },
  2569. * { name: 'The Empire Strikes Back' }
  2570. * ]);
  2571. * docs[0].name; // 'Star Wars'
  2572. *
  2573. * // Return raw result from MongoDB
  2574. * const result = await Movies.insertMany([
  2575. * { name: 'Star Wars' },
  2576. * { name: 'The Empire Strikes Back' }
  2577. * ], { rawResult: true });
  2578. *
  2579. * @param {Array|Object|*} doc(s)
  2580. * @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#insertMany)
  2581. * @param {Boolean} [options.ordered=true] if true, will fail fast on the first error encountered. If false, will insert all the documents it can and report errors later. An `insertMany()` with `ordered = false` is called an "unordered" `insertMany()`.
  2582. * @param {Boolean} [options.rawResult=false] if false, the returned promise resolves to the documents that passed mongoose document validation. If `true`, will return the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/InsertManyResult.html) with a `mongoose` property that contains `validationErrors` and `results` if this is an unordered `insertMany`.
  2583. * @param {Boolean} [options.lean=false] if `true`, skips hydrating the documents. This means Mongoose will **not** cast, validate, or apply defaults to any of the documents passed to `insertMany()`. This option is useful if you need the extra performance, but comes with data integrity risk. Consider using with [`castObject()`](https://mongoosejs.com/docs/api/model.html#Model.castObject()) and [`applyDefaults()`](https://mongoosejs.com/docs/api/model.html#Model.applyDefaults()).
  2584. * @param {Number} [options.limit=null] this limits the number of documents being processed (validation/casting) by mongoose in parallel, this does **NOT** send the documents in batches to MongoDB. Use this option if you're processing a large number of documents and your app is running out of memory.
  2585. * @param {String|Object|Array} [options.populate=null] populates the result documents. This option is a no-op if `rawResult` is set.
  2586. * @param {Boolean} [options.throwOnValidationError=false] If true and `ordered: false`, throw an error if one of the operations failed validation, but all valid operations completed successfully.
  2587. * @return {Promise} resolving to the raw result from the MongoDB driver if `options.rawResult` was `true`, or the documents that passed validation, otherwise
  2588. * @api public
  2589. */
  2590. Model.insertMany = async function insertMany(arr, options) {
  2591. _checkContext(this, 'insertMany');
  2592. if (typeof options === 'function' ||
  2593. typeof arguments[2] === 'function') {
  2594. throw new MongooseError('Model.insertMany() no longer accepts a callback');
  2595. }
  2596. try {
  2597. [arr] = await this._middleware.execPre('insertMany', this, [arr]);
  2598. } catch (error) {
  2599. await this._middleware.execPost('insertMany', this, [arr], { error });
  2600. }
  2601. options = options || {};
  2602. const ThisModel = this;
  2603. const limit = options.limit || 1000;
  2604. const rawResult = !!options.rawResult;
  2605. const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
  2606. const throwOnValidationError = typeof options.throwOnValidationError === 'boolean' ? options.throwOnValidationError : false;
  2607. const lean = !!options.lean;
  2608. const asyncLocalStorage = this.db.base.transactionAsyncLocalStorage?.getStore();
  2609. if ((!options || !Object.hasOwn(options, 'session')) && asyncLocalStorage?.session != null) {
  2610. options = { ...options, session: asyncLocalStorage.session };
  2611. }
  2612. if (!Array.isArray(arr)) {
  2613. arr = [arr];
  2614. }
  2615. const validationErrors = [];
  2616. const validationErrorsToOriginalOrder = new Map();
  2617. const results = ordered ? null : new Array(arr.length);
  2618. async function validateDoc(doc, index) {
  2619. // If option `lean` is set to true bypass validation and hydration
  2620. if (lean) {
  2621. return doc;
  2622. }
  2623. let createdNewDoc = false;
  2624. if (!(doc instanceof ThisModel)) {
  2625. if (doc != null && typeof doc !== 'object') {
  2626. throw new ObjectParameterError(doc, 'arr.' + index, 'insertMany');
  2627. }
  2628. doc = new ThisModel(doc);
  2629. createdNewDoc = true;
  2630. }
  2631. if (options.session != null) {
  2632. doc.$session(options.session);
  2633. }
  2634. return doc.$validate(createdNewDoc ? { _skipParallelValidateCheck: true } : null)
  2635. .then(() => doc)
  2636. .catch(error => {
  2637. if (ordered === false) {
  2638. error.index = index;
  2639. validationErrors.push(error);
  2640. validationErrorsToOriginalOrder.set(error, index);
  2641. results[index] = error;
  2642. return;
  2643. }
  2644. throw error;
  2645. });
  2646. }
  2647. const docs = await parallelLimit(arr, validateDoc, limit);
  2648. const originalDocIndex = new Map();
  2649. const validDocIndexToOriginalIndex = new Map();
  2650. for (let i = 0; i < docs.length; ++i) {
  2651. originalDocIndex.set(docs[i], i);
  2652. }
  2653. // We filter all failed pre-validations by removing nulls
  2654. const docAttributes = docs.filter(function(doc) {
  2655. return doc != null;
  2656. });
  2657. for (let i = 0; i < docAttributes.length; ++i) {
  2658. validDocIndexToOriginalIndex.set(i, originalDocIndex.get(docAttributes[i]));
  2659. }
  2660. // Make sure validation errors are in the same order as the
  2661. // original documents, so if both doc1 and doc2 both fail validation,
  2662. // `Model.insertMany([doc1, doc2])` will always have doc1's validation
  2663. // error before doc2's. Re: gh-12791.
  2664. if (validationErrors.length > 0) {
  2665. validationErrors.sort((err1, err2) => {
  2666. return validationErrorsToOriginalOrder.get(err1) - validationErrorsToOriginalOrder.get(err2);
  2667. });
  2668. }
  2669. // Quickly escape while there aren't any valid docAttributes
  2670. if (docAttributes.length === 0) {
  2671. if (throwOnValidationError) {
  2672. throw new MongooseBulkWriteError(
  2673. validationErrors,
  2674. results,
  2675. null,
  2676. 'insertMany'
  2677. );
  2678. }
  2679. if (rawResult) {
  2680. const res = {
  2681. acknowledged: true,
  2682. insertedCount: 0,
  2683. insertedIds: {}
  2684. };
  2685. decorateBulkWriteResult(res, validationErrors, validationErrors);
  2686. return res;
  2687. }
  2688. return [];
  2689. }
  2690. const docObjects = lean ? docAttributes : docAttributes.map(function(doc) {
  2691. if (doc.$__schema.options.versionKey) {
  2692. doc[doc.$__schema.options.versionKey] = 0;
  2693. }
  2694. const shouldSetTimestamps = (!options || options.timestamps !== false) && doc.initializeTimestamps && (!doc.$__ || doc.$__.timestamps !== false);
  2695. if (shouldSetTimestamps) {
  2696. doc.initializeTimestamps();
  2697. }
  2698. if (doc.$__hasOnlyPrimitiveValues()) {
  2699. return doc.$__toObjectShallow();
  2700. }
  2701. return doc.toObject(internalToObjectOptions);
  2702. });
  2703. let res;
  2704. try {
  2705. res = await this.$__collection.insertMany(docObjects, options);
  2706. } catch (error) {
  2707. // `writeErrors` is a property reported by the MongoDB driver,
  2708. // just not if there's only 1 error.
  2709. if (error.writeErrors == null &&
  2710. error.result?.result?.writeErrors != null) {
  2711. error.writeErrors = error.result.result.writeErrors;
  2712. }
  2713. // `insertedDocs` is a Mongoose-specific property
  2714. const hasWriteErrors = error?.writeErrors;
  2715. const erroredIndexes = new Set((error?.writeErrors || []).map(err => err.index));
  2716. if (error.writeErrors != null) {
  2717. for (let i = 0; i < error.writeErrors.length; ++i) {
  2718. const originalIndex = validDocIndexToOriginalIndex.get(error.writeErrors[i].index);
  2719. error.writeErrors[i] = { ...error.writeErrors[i], index: originalIndex };
  2720. if (!ordered) {
  2721. results[originalIndex] = error.writeErrors[i];
  2722. }
  2723. }
  2724. }
  2725. if (!ordered) {
  2726. for (let i = 0; i < results.length; ++i) {
  2727. if (results[i] === void 0) {
  2728. results[i] = docs[i];
  2729. }
  2730. }
  2731. error.results = results;
  2732. }
  2733. let firstErroredIndex = -1;
  2734. error.insertedDocs = docAttributes.
  2735. filter((doc, i) => {
  2736. const isErrored = !hasWriteErrors || erroredIndexes.has(i);
  2737. if (ordered) {
  2738. if (firstErroredIndex > -1) {
  2739. return i < firstErroredIndex;
  2740. }
  2741. if (isErrored) {
  2742. firstErroredIndex = i;
  2743. }
  2744. }
  2745. return !isErrored;
  2746. }).
  2747. map(function setIsNewForInsertedDoc(doc) {
  2748. if (lean) {
  2749. return doc;
  2750. }
  2751. doc.$__reset();
  2752. _setIsNew(doc, false);
  2753. return doc;
  2754. });
  2755. if (rawResult && ordered === false) {
  2756. decorateBulkWriteResult(error, validationErrors, results);
  2757. }
  2758. await this._middleware.execPost('insertMany', this, [arr], { error });
  2759. }
  2760. if (!lean) {
  2761. for (const attribute of docAttributes) {
  2762. attribute.$__reset();
  2763. _setIsNew(attribute, false);
  2764. }
  2765. }
  2766. if (ordered === false && throwOnValidationError && validationErrors.length > 0) {
  2767. for (let i = 0; i < results.length; ++i) {
  2768. if (results[i] === void 0) {
  2769. results[i] = docs[i];
  2770. }
  2771. }
  2772. throw new MongooseBulkWriteError(
  2773. validationErrors,
  2774. results,
  2775. res,
  2776. 'insertMany'
  2777. );
  2778. }
  2779. if (rawResult) {
  2780. if (ordered === false) {
  2781. for (let i = 0; i < results.length; ++i) {
  2782. if (results[i] === void 0) {
  2783. results[i] = docs[i];
  2784. }
  2785. }
  2786. // Decorate with mongoose validation errors in case of unordered,
  2787. // because then still do `insertMany()`
  2788. decorateBulkWriteResult(res, validationErrors, results);
  2789. }
  2790. return res;
  2791. }
  2792. if (options.populate != null) {
  2793. return this.populate(docAttributes, options.populate).catch(err => {
  2794. if (err != null) {
  2795. err.insertedDocs = docAttributes;
  2796. }
  2797. throw err;
  2798. });
  2799. }
  2800. return await this._middleware.execPost('insertMany', this, [docAttributes]).then(res => res[0]);
  2801. };
  2802. /*!
  2803. * ignore
  2804. */
  2805. function _setIsNew(doc, val) {
  2806. doc.$isNew = val;
  2807. doc.$emit('isNew', val);
  2808. doc.constructor.emit('isNew', val);
  2809. const subdocs = doc.$getAllSubdocs({ useCache: true });
  2810. for (const subdoc of subdocs) {
  2811. subdoc.$isNew = val;
  2812. subdoc.$emit('isNew', val);
  2813. }
  2814. }
  2815. /**
  2816. * Sends multiple `insertOne`, `updateOne`, `updateMany`, `replaceOne`,
  2817. * `deleteOne`, and/or `deleteMany` operations to the MongoDB server in one
  2818. * command. This is faster than sending multiple independent operations (e.g.
  2819. * if you use `create()`) because with `bulkWrite()` there is only one round
  2820. * trip to MongoDB.
  2821. *
  2822. * Mongoose will perform casting on all operations you provide.
  2823. * The only exception is [setting the `update` operator for `updateOne` or `updateMany` to a pipeline](https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite/#updateone-and-updatemany): Mongoose does **not** cast update pipelines.
  2824. *
  2825. * This function does **not** trigger any middleware, neither `save()`, nor `update()`.
  2826. * If you need to trigger
  2827. * `save()` middleware for every document use [`create()`](https://mongoosejs.com/docs/api/model.html#Model.create()) instead.
  2828. *
  2829. * #### Example:
  2830. *
  2831. * Character.bulkWrite([
  2832. * {
  2833. * insertOne: {
  2834. * document: {
  2835. * name: 'Eddard Stark',
  2836. * title: 'Warden of the North'
  2837. * }
  2838. * }
  2839. * },
  2840. * {
  2841. * updateOne: {
  2842. * filter: { name: 'Eddard Stark' },
  2843. * // If you were using the MongoDB driver directly, you'd need to do
  2844. * // `update: { $set: { title: ... } }` but mongoose adds $set for
  2845. * // you.
  2846. * update: { title: 'Hand of the King' }
  2847. * }
  2848. * },
  2849. * {
  2850. * deleteOne: {
  2851. * filter: { name: 'Eddard Stark' }
  2852. * }
  2853. * }
  2854. * ]).then(res => {
  2855. * // Prints "1 1 1"
  2856. * console.log(res.insertedCount, res.modifiedCount, res.deletedCount);
  2857. * });
  2858. *
  2859. * // Mongoose does **not** cast update pipelines, so no casting for the `update` option below.
  2860. * // Mongoose does still cast `filter`
  2861. * await Character.bulkWrite([{
  2862. * updateOne: {
  2863. * filter: { name: 'Annika Hansen' },
  2864. * update: [{ $set: { name: 7 } }] // Array means update pipeline, so Mongoose skips casting
  2865. * }
  2866. * }]);
  2867. *
  2868. * The [supported operations](https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite/#db.collection.bulkWrite) are:
  2869. *
  2870. * - `insertOne`
  2871. * - `updateOne`
  2872. * - `updateMany`
  2873. * - `deleteOne`
  2874. * - `deleteMany`
  2875. * - `replaceOne`
  2876. *
  2877. * @param {Array} ops
  2878. * @param {Object} [ops.insertOne.document] The document to insert
  2879. * @param {Object} [ops.insertOne.timestamps=true] If false, do not apply [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) to the operation
  2880. * @param {Object} [ops.updateOne.filter] Update the first document that matches this filter
  2881. * @param {Object} [ops.updateOne.update] An object containing [update operators](https://www.mongodb.com/docs/manual/reference/operator/update/)
  2882. * @param {Boolean} [ops.updateOne.upsert=false] If true, insert a doc if none match
  2883. * @param {Boolean} [ops.updateOne.timestamps=true] If false, do not apply [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) to the operation
  2884. * @param {Boolean} [ops.updateOne.overwriteImmutable=false] Mongoose removes updated immutable properties from `update` by default (excluding $setOnInsert). Set `overwriteImmutable` to `true` to allow updating immutable properties using other update operators.
  2885. * @param {Object} [ops.updateOne.collation] The [MongoDB collation](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-collations) to use
  2886. * @param {Array} [ops.updateOne.arrayFilters] The [array filters](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-36-array-filters.html) used in `update`
  2887. * @param {Object} [ops.updateMany.filter] Update all the documents that match this filter
  2888. * @param {Object} [ops.updateMany.update] An object containing [update operators](https://www.mongodb.com/docs/manual/reference/operator/update/)
  2889. * @param {Boolean} [ops.updateMany.upsert=false] If true, insert a doc if no documents match `filter`
  2890. * @param {Boolean} [ops.updateMany.timestamps=true] If false, do not apply [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) to the operation
  2891. * @param {Boolean} [ops.updateMany.overwriteImmutable=false] Mongoose removes updated immutable properties from `update` by default (excluding $setOnInsert). Set `overwriteImmutable` to `true` to allow updating immutable properties using other update operators.
  2892. * @param {Object} [ops.updateMany.collation] The [MongoDB collation](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-collations) to use
  2893. * @param {Array} [ops.updateMany.arrayFilters] The [array filters](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-36-array-filters.html) used in `update`
  2894. * @param {Object} [ops.deleteOne.filter] Delete the first document that matches this filter
  2895. * @param {Object} [ops.deleteMany.filter] Delete all documents that match this filter
  2896. * @param {Object} [ops.replaceOne.filter] Replace the first document that matches this filter
  2897. * @param {Object} [ops.replaceOne.replacement] The replacement document
  2898. * @param {Boolean} [ops.replaceOne.upsert=false] If true, insert a doc if no documents match `filter`
  2899. * @param {Object} [ops.replaceOne.timestamps=true] If false, do not apply [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) to the operation
  2900. * @param {Object} [options]
  2901. * @param {Boolean} [options.ordered=true] If true, execute writes in order and stop at the first error. If false, execute writes in parallel and continue until all writes have either succeeded or errored.
  2902. * @param {Boolean} [options.timestamps=true] If false, do not apply [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) to any operations. Can be overridden at the operation-level.
  2903. * @param {ClientSession} [options.session=null] The session associated with this bulk write. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
  2904. * @param {String|number} [options.w=1] The [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/). See [`Query#w()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.w()) for more information.
  2905. * @param {number} [options.wtimeout=null] The [write concern timeout](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout).
  2906. * @param {Boolean} [options.j=true] If false, disable [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option)
  2907. * @param {Boolean} [options.skipValidation=false] Set to true to skip Mongoose schema validation on bulk write operations. Mongoose currently runs validation on `insertOne` and `replaceOne` operations by default.
  2908. * @param {Boolean} [options.bypassDocumentValidation=false] If true, disable [MongoDB server-side schema validation](https://www.mongodb.com/docs/manual/core/schema-validation/) for all writes in this bulk.
  2909. * @param {Boolean} [options.throwOnValidationError=false] If true and `ordered: false`, throw an error if one of the operations failed validation, but all valid operations completed successfully. Note that Mongoose will still send all valid operations to the MongoDB server.
  2910. * @param {Boolean|"throw"} [options.strict=null] Overwrites the [`strict` option](https://mongoosejs.com/docs/guide.html#strict) on schema. If false, allows filtering and writing fields not defined in the schema for all writes in this bulk.
  2911. * @return {Promise} resolves to a [`BulkWriteOpResult`](https://mongodb.github.io/node-mongodb-native/4.9/classes/BulkWriteResult.html) if the operation succeeds
  2912. * @api public
  2913. */
  2914. Model.bulkWrite = async function bulkWrite(ops, options) {
  2915. _checkContext(this, 'bulkWrite');
  2916. if (typeof options === 'function' ||
  2917. typeof arguments[2] === 'function') {
  2918. throw new MongooseError('Model.bulkWrite() no longer accepts a callback');
  2919. }
  2920. options = options || {};
  2921. try {
  2922. [ops, options] = await this.hooks.execPre('bulkWrite', this, [ops, options]);
  2923. } catch (err) {
  2924. if (err instanceof Kareem.skipWrappedFunction) {
  2925. ops = err;
  2926. } else {
  2927. await this.hooks.execPost('bulkWrite', this, [null], { error: err });
  2928. }
  2929. }
  2930. if (ops instanceof Kareem.skipWrappedFunction) {
  2931. return ops.args[0];
  2932. }
  2933. const ordered = options.ordered == null ? true : options.ordered;
  2934. if (ops.length === 0) {
  2935. const BulkWriteResult = this.base.driver.get().BulkWriteResult;
  2936. const bulkWriteResult = new BulkWriteResult(getDefaultBulkwriteResult(), false);
  2937. bulkWriteResult.n = 0;
  2938. decorateBulkWriteResult(bulkWriteResult, [], []);
  2939. return bulkWriteResult;
  2940. }
  2941. const validations = options?._skipCastBulkWrite ? [] : ops.map(op => castBulkWrite(this, op, options));
  2942. const asyncLocalStorage = this.db.base.transactionAsyncLocalStorage?.getStore();
  2943. if ((!options || !Object.hasOwn(options, 'session')) && asyncLocalStorage?.session != null) {
  2944. options = { ...options, session: asyncLocalStorage.session };
  2945. }
  2946. let res = null;
  2947. if (ordered) {
  2948. await new Promise((resolve, reject) => {
  2949. each(validations, (fn, cb) => fn(cb), error => {
  2950. if (error) {
  2951. return reject(error);
  2952. }
  2953. resolve();
  2954. });
  2955. });
  2956. try {
  2957. res = await this.$__collection.bulkWrite(ops, options);
  2958. } catch (error) {
  2959. await this.hooks.execPost('bulkWrite', this, [null], { error });
  2960. }
  2961. } else {
  2962. let validOpIndexes = [];
  2963. let validationErrors = [];
  2964. const results = [];
  2965. if (validations.length > 0) {
  2966. validOpIndexes = await Promise.all(ops.map((op, i) => {
  2967. if (i >= validations.length) {
  2968. return i;
  2969. }
  2970. return new Promise((resolve) => {
  2971. validations[i]((err) => {
  2972. if (err == null) {
  2973. resolve(i);
  2974. } else {
  2975. validationErrors.push({ index: i, error: err });
  2976. results[i] = err;
  2977. }
  2978. resolve();
  2979. });
  2980. });
  2981. }));
  2982. validOpIndexes = validOpIndexes.filter(index => index != null);
  2983. } else {
  2984. validOpIndexes = ops.map((op, i) => i);
  2985. }
  2986. validationErrors = validationErrors.
  2987. sort((v1, v2) => v1.index - v2.index).
  2988. map(v => v.error);
  2989. const validOps = validOpIndexes.sort().map(index => ops[index]);
  2990. if (validOps.length === 0) {
  2991. if (options.throwOnValidationError && validationErrors.length) {
  2992. throw new MongooseBulkWriteError(
  2993. validationErrors,
  2994. results,
  2995. res,
  2996. 'bulkWrite'
  2997. );
  2998. }
  2999. const BulkWriteResult = this.base.driver.get().BulkWriteResult;
  3000. const bulkWriteResult = new BulkWriteResult(getDefaultBulkwriteResult(), false);
  3001. bulkWriteResult.result = getDefaultBulkwriteResult();
  3002. decorateBulkWriteResult(bulkWriteResult, validationErrors, results);
  3003. return bulkWriteResult;
  3004. }
  3005. let error;
  3006. [res, error] = await this.$__collection.bulkWrite(validOps, options).
  3007. then(res => ([res, null])).
  3008. catch(error => ([null, error]));
  3009. const writeErrorsByIndex = {};
  3010. if (error?.writeErrors) {
  3011. for (const writeError of error.writeErrors) {
  3012. writeErrorsByIndex[writeError.err.index] = writeError;
  3013. }
  3014. }
  3015. for (let i = 0; i < validOpIndexes.length; ++i) {
  3016. results[validOpIndexes[i]] = writeErrorsByIndex[i] ?? null;
  3017. }
  3018. if (error) {
  3019. if (validationErrors.length > 0) {
  3020. decorateBulkWriteResult(error, validationErrors, results);
  3021. }
  3022. await this.hooks.execPost('bulkWrite', this, [null], { error });
  3023. }
  3024. if (validationErrors.length > 0) {
  3025. if (options.throwOnValidationError) {
  3026. throw new MongooseBulkWriteError(
  3027. validationErrors,
  3028. results,
  3029. res,
  3030. 'bulkWrite'
  3031. );
  3032. } else {
  3033. decorateBulkWriteResult(res, validationErrors, results);
  3034. }
  3035. }
  3036. }
  3037. await this.hooks.execPost('bulkWrite', this, [res]);
  3038. return res;
  3039. };
  3040. /**
  3041. * Takes an array of documents, gets the changes and inserts/updates documents in the database
  3042. * according to whether or not the document is new, or whether it has changes or not.
  3043. *
  3044. * `bulkSave` uses `bulkWrite` under the hood, so it's mostly useful when dealing with many documents (10K+)
  3045. *
  3046. * `bulkSave()` throws errors under the following conditions:
  3047. *
  3048. * - one of the provided documents fails validation. In this case, `bulkSave()` does not send a `bulkWrite()`, and throws the first validation error.
  3049. * - `bulkWrite()` fails (for example, due to being unable to connect to MongoDB or due to duplicate key error)
  3050. * - `bulkWrite()` did not insert or update **any** documents. In this case, `bulkSave()` will throw a DocumentNotFound error.
  3051. *
  3052. * Note that `bulkSave()` will **not** throw an error if only some of the `save()` calls succeeded.
  3053. *
  3054. * @param {Array<Document>} documents
  3055. * @param {Object} [options] options passed to the underlying `bulkWrite()`
  3056. * @param {Boolean} [options.timestamps] defaults to `null`, when set to false, mongoose will not add/update timestamps to the documents.
  3057. * @param {ClientSession} [options.session=null] The session associated with this bulk write. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
  3058. * @param {String|number} [options.w=1] The [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/). See [`Query#w()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.w()) for more information.
  3059. * @param {number} [options.wtimeout=null] The [write concern timeout](https://www.mongodb.com/docs/manual/reference/write-concern/#wtimeout).
  3060. * @param {Boolean} [options.j=true] If false, disable [journal acknowledgement](https://www.mongodb.com/docs/manual/reference/write-concern/#j-option)
  3061. * @param {Boolean} [options.validateBeforeSave=true] set to `false` to skip Mongoose validation on all documents
  3062. * @return {BulkWriteResult} the return value from `bulkWrite()`
  3063. */
  3064. Model.bulkSave = async function bulkSave(documents, options) {
  3065. options = options || {};
  3066. if (options.timestamps != null) {
  3067. for (const document of documents) {
  3068. document.$__.saveOptions = document.$__.saveOptions || {};
  3069. document.$__.saveOptions.timestamps = options.timestamps;
  3070. }
  3071. } else {
  3072. for (const document of documents) {
  3073. if (document.$__.timestamps != null) {
  3074. document.$__.saveOptions = document.$__.saveOptions || {};
  3075. document.$__.saveOptions.timestamps = document.$__.timestamps;
  3076. }
  3077. }
  3078. }
  3079. await Promise.all(documents.map(doc => buildPreSavePromise(doc, options)));
  3080. const writeOperations = this.buildBulkWriteOperations(documents, options);
  3081. const opts = { skipValidation: true, _skipCastBulkWrite: true, ...options };
  3082. const { bulkWriteResult, bulkWriteError } = await this.bulkWrite(writeOperations, opts).then(
  3083. (res) => ({ bulkWriteResult: res, bulkWriteError: null }),
  3084. (err) => ({ bulkWriteResult: null, bulkWriteError: err })
  3085. );
  3086. // If not a MongoBulkWriteError, treat this as all documents failed to save.
  3087. if (bulkWriteError != null && bulkWriteError.name !== 'MongoBulkWriteError') {
  3088. throw bulkWriteError;
  3089. }
  3090. const matchedCount = bulkWriteResult?.matchedCount ?? 0;
  3091. const insertedCount = bulkWriteResult?.insertedCount ?? 0;
  3092. if (writeOperations.length > 0 && matchedCount + insertedCount < writeOperations.length && !bulkWriteError) {
  3093. throw new MongooseBulkSaveIncompleteError(
  3094. this.modelName,
  3095. documents,
  3096. bulkWriteResult
  3097. );
  3098. }
  3099. const successfulDocuments = [];
  3100. for (let i = 0; i < documents.length; i++) {
  3101. const document = documents[i];
  3102. const documentError = bulkWriteError?.writeErrors.find(writeError => {
  3103. const writeErrorDocumentId = writeError.err.op._id || writeError.err.op.q._id;
  3104. return writeErrorDocumentId.toString() === document._doc._id.toString();
  3105. });
  3106. if (documentError == null) {
  3107. successfulDocuments.push(document);
  3108. }
  3109. }
  3110. await Promise.all(successfulDocuments.map(document => handleSuccessfulWrite(document)));
  3111. if (bulkWriteError != null) {
  3112. throw bulkWriteError;
  3113. }
  3114. return bulkWriteResult;
  3115. };
  3116. async function buildPreSavePromise(document, options) {
  3117. const [newOptions] = await document.schema.s.hooks.execPre('save', document, [options]);
  3118. if (newOptions !== options) {
  3119. throw new Error('Cannot overwrite options in pre("save") hook on bulkSave()');
  3120. }
  3121. }
  3122. async function handleSuccessfulWrite(document) {
  3123. if (document.$isNew) {
  3124. _setIsNew(document, false);
  3125. }
  3126. document.$__reset();
  3127. document._applyVersionIncrement();
  3128. return document.schema.s.hooks.execPost('save', document, [document]);
  3129. }
  3130. /**
  3131. * Apply defaults to the given document or POJO.
  3132. *
  3133. * @param {Object|Document} obj object or document to apply defaults on
  3134. * @returns {Object|Document}
  3135. * @api public
  3136. */
  3137. Model.applyDefaults = function applyDefaults(doc) {
  3138. if (doc == null) {
  3139. return doc;
  3140. }
  3141. if (doc.$__ != null) {
  3142. applyDefaultsHelper(doc, doc.$__.fields, doc.$__.exclude);
  3143. for (const subdoc of doc.$getAllSubdocs()) {
  3144. applyDefaults(subdoc, subdoc.$__.fields, subdoc.$__.exclude);
  3145. }
  3146. return doc;
  3147. }
  3148. applyDefaultsToPOJO(doc, this.schema);
  3149. return doc;
  3150. };
  3151. /**
  3152. * Apply this model's virtuals to a given POJO. Virtuals execute with the POJO as the context `this`.
  3153. *
  3154. * #### Example:
  3155. *
  3156. * const userSchema = new Schema({ name: String });
  3157. * userSchema.virtual('upper').get(function() { return this.name.toUpperCase(); });
  3158. * const User = mongoose.model('User', userSchema);
  3159. *
  3160. * const obj = { name: 'John' };
  3161. * User.applyVirtuals(obj);
  3162. * obj.name; // 'John'
  3163. * obj.upper; // 'JOHN', Mongoose applied the return value of the virtual to the given object
  3164. *
  3165. * @param {Object} obj object or document to apply virtuals on
  3166. * @param {Array<string>} [virtualsToApply] optional whitelist of virtuals to apply
  3167. * @returns {Object} obj
  3168. * @api public
  3169. */
  3170. Model.applyVirtuals = function applyVirtuals(obj, virtualsToApply) {
  3171. if (obj == null) {
  3172. return obj;
  3173. }
  3174. // Nothing to do if this is already a hydrated document - it should already have virtuals
  3175. if (obj.$__ != null) {
  3176. return obj;
  3177. }
  3178. applyVirtualsHelper(this.schema, obj, virtualsToApply);
  3179. return obj;
  3180. };
  3181. /**
  3182. * Apply this model's timestamps to a given POJO, including subdocument timestamps
  3183. *
  3184. * #### Example:
  3185. *
  3186. * const userSchema = new Schema({ name: String }, { timestamps: true });
  3187. * const User = mongoose.model('User', userSchema);
  3188. *
  3189. * const obj = { name: 'John' };
  3190. * User.applyTimestamps(obj);
  3191. * obj.createdAt; // 2024-06-01T18:00:00.000Z
  3192. * obj.updatedAt; // 2024-06-01T18:00:00.000Z
  3193. *
  3194. * @param {Object} obj object or document to apply virtuals on
  3195. * @param {Object} [options]
  3196. * @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
  3197. * @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
  3198. * @returns {Object} obj
  3199. * @api public
  3200. */
  3201. Model.applyTimestamps = function applyTimestamps(obj, options) {
  3202. if (obj == null) {
  3203. return obj;
  3204. }
  3205. // Nothing to do if this is already a hydrated document - it should already have timestamps
  3206. if (obj.$__ != null) {
  3207. return obj;
  3208. }
  3209. applyTimestampsHelper(this.schema, obj, options);
  3210. return obj;
  3211. };
  3212. /**
  3213. * Cast the given POJO to the model's schema
  3214. *
  3215. * #### Example:
  3216. *
  3217. * const Test = mongoose.model('Test', Schema({ num: Number }));
  3218. *
  3219. * const obj = Test.castObject({ num: '42' });
  3220. * obj.num; // 42 as a number
  3221. *
  3222. * Test.castObject({ num: 'not a number' }); // Throws a ValidationError
  3223. *
  3224. * @param {Object} obj object or document to cast
  3225. * @param {Object} options options passed to castObject
  3226. * @param {Boolean} options.ignoreCastErrors If set to `true` will not throw a ValidationError and only return values that were successfully cast.
  3227. * @returns {Object} POJO casted to the model's schema
  3228. * @throws {ValidationError} if casting failed for at least one path
  3229. * @api public
  3230. */
  3231. Model.castObject = function castObject(obj, options) {
  3232. options = options || {};
  3233. const ret = {};
  3234. let schema = this.schema;
  3235. const discriminatorKey = schema.options.discriminatorKey;
  3236. if (schema.discriminators != null && obj != null && obj[discriminatorKey] != null) {
  3237. schema = getSchemaDiscriminatorByValue(schema, obj[discriminatorKey]) || schema;
  3238. }
  3239. const paths = Object.keys(schema.paths);
  3240. for (const path of paths) {
  3241. const schemaType = schema.path(path);
  3242. if (!schemaType?.$isMongooseArray) {
  3243. continue;
  3244. }
  3245. const val = get(obj, path);
  3246. pushNestedArrayPaths(paths, val, path);
  3247. }
  3248. let error = null;
  3249. for (const path of paths) {
  3250. const schemaType = schema.path(path);
  3251. if (schemaType == null) {
  3252. continue;
  3253. }
  3254. let val = get(obj, path, void 0);
  3255. if (val == null) {
  3256. continue;
  3257. }
  3258. const pieces = path.indexOf('.') === -1 ? [path] : path.split('.');
  3259. let cur = ret;
  3260. for (let i = 0; i < pieces.length - 1; ++i) {
  3261. if (cur[pieces[i]] == null) {
  3262. cur[pieces[i]] = isNaN(pieces[i + 1]) ? {} : [];
  3263. }
  3264. cur = cur[pieces[i]];
  3265. }
  3266. if (schemaType.$isMongooseDocumentArray) {
  3267. const castNonArraysOption = schemaType.options?.castNonArrays ?? schemaType.constructor.options.castNonArrays;
  3268. if (!Array.isArray(val)) {
  3269. if (!castNonArraysOption) {
  3270. if (!options.ignoreCastErrors) {
  3271. error = error || new ValidationError();
  3272. error.addError(path, new ObjectExpectedError(path, val));
  3273. }
  3274. } else {
  3275. cur[pieces[pieces.length - 1]] = [
  3276. Model.castObject.call(schemaType.Constructor, val)
  3277. ];
  3278. }
  3279. continue;
  3280. }
  3281. }
  3282. if (schemaType.$isSingleNested || schemaType.$isMongooseDocumentArrayElement) {
  3283. try {
  3284. val = Model.castObject.call(schemaType.Constructor, val);
  3285. } catch (err) {
  3286. if (!options.ignoreCastErrors) {
  3287. error = error || new ValidationError();
  3288. error.addError(path, err);
  3289. }
  3290. continue;
  3291. }
  3292. cur[pieces[pieces.length - 1]] = val;
  3293. continue;
  3294. }
  3295. try {
  3296. val = schemaType.cast(val);
  3297. cur[pieces[pieces.length - 1]] = val;
  3298. } catch (err) {
  3299. if (!options.ignoreCastErrors) {
  3300. error = error || new ValidationError();
  3301. error.addError(path, err);
  3302. }
  3303. continue;
  3304. }
  3305. }
  3306. if (error != null) {
  3307. throw error;
  3308. }
  3309. return ret;
  3310. };
  3311. /**
  3312. * Build bulk write operations for `bulkSave()`.
  3313. *
  3314. * @param {Array<Document>} documents The array of documents to build write operations of
  3315. * @param {Object} options
  3316. * @param {Boolean} options.skipValidation defaults to `false`, when set to true, building the write operations will bypass validating the documents.
  3317. * @param {Boolean} options.timestamps defaults to `null`, when set to false, mongoose will not add/update timestamps to the documents.
  3318. * @return {Array<Promise>} Returns a array of all Promises the function executes to be awaited.
  3319. * @api private
  3320. */
  3321. Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, options) {
  3322. if (!Array.isArray(documents)) {
  3323. throw new Error(`bulkSave expects an array of documents to be passed, received \`${documents}\` instead`);
  3324. }
  3325. setDefaultOptions();
  3326. const writeOperations = documents.map((document, i) => {
  3327. if (!options.skipValidation) {
  3328. if (!(document instanceof Document)) {
  3329. throw new Error(`documents.${i} was not a mongoose document, documents must be an array of mongoose documents (instanceof mongoose.Document).`);
  3330. }
  3331. if (options.validateBeforeSave == null || options.validateBeforeSave) {
  3332. const err = document.validateSync();
  3333. if (err != null) {
  3334. throw err;
  3335. }
  3336. }
  3337. }
  3338. const isANewDocument = document.isNew;
  3339. if (isANewDocument) {
  3340. const writeOperation = { insertOne: { document } };
  3341. utils.injectTimestampsOption(writeOperation.insertOne, options.timestamps);
  3342. return writeOperation;
  3343. }
  3344. const delta = document.$__delta();
  3345. const isDocumentWithChanges = delta != null && !utils.isEmptyObject(delta[0]);
  3346. if (isDocumentWithChanges) {
  3347. const where = document.$__where(delta[0]);
  3348. const changes = delta[1];
  3349. _applyCustomWhere(document, where);
  3350. // If shard key is set, add shard keys to _filter_ condition to right shard is targeted
  3351. const shardKey = this.schema.options.shardKey;
  3352. if (shardKey) {
  3353. const paths = Object.keys(shardKey);
  3354. const len = paths.length;
  3355. for (let i = 0; i < len; ++i) {
  3356. where[paths[i]] = document[paths[i]];
  3357. }
  3358. }
  3359. document.$__version(where, delta);
  3360. const writeOperation = { updateOne: { filter: where, update: changes } };
  3361. utils.injectTimestampsOption(writeOperation.updateOne, options.timestamps);
  3362. return writeOperation;
  3363. }
  3364. return null;
  3365. }).filter(op => op !== null);
  3366. return writeOperations;
  3367. function setDefaultOptions() {
  3368. options = options || {};
  3369. if (options.skipValidation == null) {
  3370. options.skipValidation = false;
  3371. }
  3372. }
  3373. };
  3374. /**
  3375. * Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
  3376. * The document returned has no paths marked as modified initially.
  3377. *
  3378. * #### Example:
  3379. *
  3380. * // hydrate previous data into a Mongoose document
  3381. * const mongooseCandy = Candy.hydrate({ _id: '54108337212ffb6d459f854c', type: 'jelly bean' });
  3382. *
  3383. * @param {Object} obj
  3384. * @param {Object|String|String[]} [projection] optional projection containing which fields should be selected for this document
  3385. * @param {Object} [options] optional options
  3386. * @param {Boolean} [options.setters=false] if true, apply schema setters when hydrating
  3387. * @param {Boolean} [options.hydratedPopulatedDocs=false] if true, populates the docs if passing pre-populated data
  3388. * @param {Boolean} [options.virtuals=false] if true, sets any virtuals present on `obj`
  3389. * @return {Document} document instance
  3390. * @api public
  3391. */
  3392. Model.hydrate = function(obj, projection, options) {
  3393. _checkContext(this, 'hydrate');
  3394. if (options?.virtuals && options?.hydratedPopulatedDocs === false) {
  3395. throw new MongooseError('Cannot set `hydratedPopulatedDocs` option to false if `virtuals` option is truthy because `virtuals: true` also sets populated virtuals');
  3396. }
  3397. if (projection != null) {
  3398. if (obj?.$__ != null) {
  3399. obj = obj.toObject(internalToObjectOptions);
  3400. }
  3401. obj = applyProjection(obj, projection);
  3402. }
  3403. const document = require('./queryHelpers').createModel(this, obj, projection);
  3404. document.$init(obj, options);
  3405. return document;
  3406. };
  3407. /**
  3408. * Same as `updateOne()`, except MongoDB will update _all_ documents that match
  3409. * `filter` (as opposed to just the first one) regardless of the value of
  3410. * the `multi` option.
  3411. *
  3412. * **Note** updateMany will _not_ fire update middleware. Use `pre('updateMany')`
  3413. * and `post('updateMany')` instead.
  3414. *
  3415. * #### Example:
  3416. *
  3417. * const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
  3418. * res.matchedCount; // Number of documents matched
  3419. * res.modifiedCount; // Number of documents modified
  3420. * res.acknowledged; // Boolean indicating the MongoDB server received the operation. This may be false if Mongoose did not send an update to the server because the update was empty.
  3421. * res.upsertedId; // null or an id containing a document that had to be upserted.
  3422. * res.upsertedCount; // Number indicating how many documents had to be upserted. Will either be 0 or 1.
  3423. *
  3424. * // Other supported syntaxes
  3425. * await Person.find({ name: /Stark$/ }).updateMany({ isDeleted: true }); // Using chaining syntax
  3426. * await Person.find().updateMany({ isDeleted: true }); // Set `isDeleted` on _all_ Person documents
  3427. *
  3428. * This function triggers the following middleware.
  3429. *
  3430. * - `updateMany()`
  3431. *
  3432. * @param {Object} filter
  3433. * @param {Object|Array} update. If array, this update will be treated as an update pipeline and not casted.
  3434. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  3435. * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
  3436. * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
  3437. * @param {Object} [options.writeConcern=null] sets the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](https://mongoosejs.com/docs/guide.html#writeConcern)
  3438. * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
  3439. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  3440. * @param {Boolean} [options.overwriteDiscriminatorKey=false] Mongoose removes discriminator key updates from `update` by default, set `overwriteDiscriminatorKey` to `true` to allow updating the discriminator key
  3441. * @return {Query}
  3442. * @see Query docs https://mongoosejs.com/docs/queries.html
  3443. * @see MongoDB docs https://www.mongodb.com/docs/manual/reference/command/update/#update-command-output
  3444. * @see UpdateResult https://mongodb.github.io/node-mongodb-native/4.9/interfaces/UpdateResult.html
  3445. * @api public
  3446. */
  3447. Model.updateMany = function updateMany(conditions, update, options) {
  3448. _checkContext(this, 'updateMany');
  3449. if (update == null) {
  3450. throw new MongooseError('updateMany `update` parameter cannot be nullish');
  3451. }
  3452. return _update(this, 'updateMany', conditions, update, options);
  3453. };
  3454. /**
  3455. * Update _only_ the first document that matches `filter`.
  3456. *
  3457. * - Use `replaceOne()` if you want to overwrite an entire document rather than using atomic operators like `$set`.
  3458. *
  3459. * #### Example:
  3460. *
  3461. * const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
  3462. * res.matchedCount; // Number of documents matched
  3463. * res.modifiedCount; // Number of documents modified
  3464. * res.acknowledged; // Boolean indicating the MongoDB server received the operation. This may be false if Mongoose did not send an update to the server because the update was empty.
  3465. * res.upsertedId; // null or an id containing a document that had to be upserted.
  3466. * res.upsertedCount; // Number indicating how many documents had to be upserted. Will either be 0 or 1.
  3467. *
  3468. * // Other supported syntaxes
  3469. * await Person.findOne({ name: 'Jean-Luc Picard' }).updateOne({ ship: 'USS Enterprise' }); // Using chaining syntax
  3470. * await Person.updateOne({ ship: 'USS Enterprise' }); // Updates first doc's `ship` property
  3471. *
  3472. * This function triggers the following middleware.
  3473. *
  3474. * - `updateOne()`
  3475. *
  3476. * @param {Object} filter
  3477. * @param {Object|Array} update. If array, this update will be treated as an update pipeline and not casted.
  3478. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  3479. * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
  3480. * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
  3481. * @param {Object} [options.writeConcern=null] sets the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](https://mongoosejs.com/docs/guide.html#writeConcern)
  3482. * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
  3483. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  3484. * @param {Boolean} [options.overwriteDiscriminatorKey=false] Mongoose removes discriminator key updates from `update` by default, set `overwriteDiscriminatorKey` to `true` to allow updating the discriminator key
  3485. * @return {Query}
  3486. * @see Query docs https://mongoosejs.com/docs/queries.html
  3487. * @see MongoDB docs https://www.mongodb.com/docs/manual/reference/command/update/#update-command-output
  3488. * @see UpdateResult https://mongodb.github.io/node-mongodb-native/4.9/interfaces/UpdateResult.html
  3489. * @api public
  3490. */
  3491. Model.updateOne = function updateOne(conditions, doc, options) {
  3492. _checkContext(this, 'updateOne');
  3493. return _update(this, 'updateOne', conditions, doc, options);
  3494. };
  3495. /**
  3496. * Replace the existing document with the given document (no atomic operators like `$set`).
  3497. *
  3498. * #### Example:
  3499. *
  3500. * const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
  3501. * res.matchedCount; // Number of documents matched
  3502. * res.modifiedCount; // Number of documents modified
  3503. * res.acknowledged; // Boolean indicating the MongoDB server received the operation.
  3504. * res.upsertedId; // null or an id containing a document that had to be upserted.
  3505. * res.upsertedCount; // Number indicating how many documents had to be upserted. Will either be 0 or 1.
  3506. *
  3507. * This function triggers the following middleware.
  3508. *
  3509. * - `replaceOne()`
  3510. *
  3511. * @param {Object} filter
  3512. * @param {Object} doc
  3513. * @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())
  3514. * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
  3515. * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
  3516. * @param {Object} [options.writeConcern=null] sets the [write concern](https://www.mongodb.com/docs/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](https://mongoosejs.com/docs/guide.html#writeConcern)
  3517. * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](https://mongoosejs.com/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
  3518. * @param {Boolean} [options.translateAliases=null] If set to `true`, translates any schema-defined aliases in `filter`, `projection`, `update`, and `distinct`. Throws an error if there are any conflicts where both alias and raw property are defined on the same object.
  3519. * @return {Query}
  3520. * @see Query docs https://mongoosejs.com/docs/queries.html
  3521. * @see UpdateResult https://mongodb.github.io/node-mongodb-native/4.9/interfaces/UpdateResult.html
  3522. * @return {Query}
  3523. * @api public
  3524. */
  3525. Model.replaceOne = function replaceOne(conditions, doc, options) {
  3526. _checkContext(this, 'replaceOne');
  3527. const versionKey = this?.schema?.options?.versionKey || null;
  3528. if (versionKey && !doc[versionKey]) {
  3529. doc[versionKey] = 0;
  3530. }
  3531. return _update(this, 'replaceOne', conditions, doc, options);
  3532. };
  3533. /**
  3534. * Common code for `updateOne()`, `updateMany()`, `replaceOne()`, and `update()`
  3535. * because they need to do the same thing
  3536. * @api private
  3537. */
  3538. function _update(model, op, conditions, doc, options) {
  3539. const mq = new model.Query({}, {}, model, model.collection);
  3540. // gh-2406
  3541. // make local deep copy of conditions
  3542. if (conditions instanceof Document) {
  3543. conditions = conditions.toObject();
  3544. } else {
  3545. conditions = clone(conditions);
  3546. }
  3547. options = typeof options === 'function' ? options : clone(options);
  3548. const versionKey = model?.schema?.options?.versionKey || null;
  3549. decorateUpdateWithVersionKey(doc, options, versionKey);
  3550. return mq[op](conditions, doc, options);
  3551. }
  3552. /**
  3553. * Performs [aggregations](https://www.mongodb.com/docs/manual/aggregation/) on the models collection.
  3554. *
  3555. * The `aggregate` itself is returned.
  3556. *
  3557. * This function triggers the following middleware.
  3558. *
  3559. * - `aggregate()`
  3560. *
  3561. * #### Example:
  3562. *
  3563. * // Find the max balance of all accounts
  3564. * const res = await Users.aggregate([
  3565. * { $group: { _id: null, maxBalance: { $max: '$balance' }}},
  3566. * { $project: { _id: 0, maxBalance: 1 }}
  3567. * ]);
  3568. *
  3569. * console.log(res); // [ { maxBalance: 98000 } ]
  3570. *
  3571. * // Or use the aggregation pipeline builder.
  3572. * const res = await Users.aggregate().
  3573. * group({ _id: null, maxBalance: { $max: '$balance' } }).
  3574. * project('-id maxBalance').
  3575. * exec();
  3576. * console.log(res); // [ { maxBalance: 98 } ]
  3577. *
  3578. * #### Note:
  3579. *
  3580. * - Mongoose does **not** cast aggregation pipelines to the model's schema because `$project` and `$group` operators allow redefining the "shape" of the documents at any stage of the pipeline, which may leave documents in an incompatible format. You can use the [mongoose-cast-aggregation plugin](https://github.com/AbdelrahmanHafez/mongoose-cast-aggregation) to enable minimal casting for aggregation pipelines.
  3581. * - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
  3582. *
  3583. * #### More About Aggregations:
  3584. *
  3585. * - [Mongoose `Aggregate`](https://mongoosejs.com/docs/api/aggregate.html)
  3586. * - [An Introduction to Mongoose Aggregate](https://masteringjs.io/tutorials/mongoose/aggregate)
  3587. * - [MongoDB Aggregation docs](https://www.mongodb.com/docs/manual/applications/aggregation/)
  3588. *
  3589. * @see Aggregate https://mongoosejs.com/docs/api/aggregate.html#Aggregate()
  3590. * @see MongoDB https://www.mongodb.com/docs/manual/applications/aggregation/
  3591. * @param {Array} [pipeline] aggregation pipeline as an array of objects
  3592. * @param {Object} [options] aggregation options
  3593. * @return {Aggregate}
  3594. * @api public
  3595. */
  3596. Model.aggregate = function aggregate(pipeline, options) {
  3597. _checkContext(this, 'aggregate');
  3598. if (typeof options === 'function' || typeof arguments[2] === 'function') {
  3599. throw new MongooseError('Model.aggregate() no longer accepts a callback');
  3600. }
  3601. const aggregate = new Aggregate(pipeline || []);
  3602. aggregate.model(this);
  3603. if (options != null) {
  3604. aggregate.option(options);
  3605. }
  3606. return aggregate;
  3607. };
  3608. /**
  3609. * Casts and validates the given object against this model's schema, passing the
  3610. * given `context` to custom validators.
  3611. *
  3612. * #### Example:
  3613. *
  3614. * const Model = mongoose.model('Test', Schema({
  3615. * name: { type: String, required: true },
  3616. * age: { type: Number, required: true }
  3617. * });
  3618. *
  3619. * try {
  3620. * await Model.validate({ name: null }, ['name'])
  3621. * } catch (err) {
  3622. * err instanceof mongoose.Error.ValidationError; // true
  3623. * Object.keys(err.errors); // ['name']
  3624. * }
  3625. *
  3626. * @param {Object} obj
  3627. * @param {Object|Array|String} pathsOrOptions
  3628. * @param {Object} [context]
  3629. * @return {Promise<Object>} casted and validated copy of `obj` if validation succeeded
  3630. * @api public
  3631. */
  3632. Model.validate = async function validate(obj, pathsOrOptions, context) {
  3633. if ((arguments.length < 3) || (arguments.length === 3 && typeof arguments[2] === 'function')) {
  3634. // For convenience, if we're validating a document or an object, make `context` default to
  3635. // the model so users don't have to always pass `context`, re: gh-10132, gh-10346
  3636. context = obj;
  3637. }
  3638. if (typeof context === 'function' || typeof arguments[3] === 'function') {
  3639. throw new MongooseError('Model.validate() no longer accepts a callback');
  3640. }
  3641. let schema = this.schema;
  3642. const discriminatorKey = schema.options.discriminatorKey;
  3643. if (schema.discriminators != null && obj != null && obj[discriminatorKey] != null) {
  3644. schema = getSchemaDiscriminatorByValue(schema, obj[discriminatorKey]) || schema;
  3645. }
  3646. let paths = Object.keys(schema.paths);
  3647. if (pathsOrOptions != null) {
  3648. const _pathsToValidate = typeof pathsOrOptions === 'string' ? new Set(pathsOrOptions.split(' ')) : Array.isArray(pathsOrOptions) ? new Set(pathsOrOptions) : new Set(paths);
  3649. paths = paths.filter(p => {
  3650. if (pathsOrOptions.pathsToSkip) {
  3651. if (Array.isArray(pathsOrOptions.pathsToSkip)) {
  3652. if (pathsOrOptions.pathsToSkip.find(x => x == p)) {
  3653. return false;
  3654. }
  3655. } else if (typeof pathsOrOptions.pathsToSkip == 'string') {
  3656. if (pathsOrOptions.pathsToSkip.includes(p)) {
  3657. return false;
  3658. }
  3659. }
  3660. }
  3661. const pieces = p.split('.');
  3662. let cur = pieces[0];
  3663. for (const piece of pieces) {
  3664. if (_pathsToValidate.has(cur)) {
  3665. return true;
  3666. }
  3667. cur += '.' + piece;
  3668. }
  3669. return _pathsToValidate.has(p);
  3670. });
  3671. }
  3672. for (const path of paths) {
  3673. const schemaType = schema.path(path);
  3674. if (!schemaType?.$isMongooseArray || schemaType.$isMongooseDocumentArray) {
  3675. continue;
  3676. }
  3677. const val = get(obj, path);
  3678. pushNestedArrayPaths(paths, val, path);
  3679. }
  3680. let error = null;
  3681. paths = new Set(paths);
  3682. try {
  3683. obj = this.castObject(obj);
  3684. } catch (err) {
  3685. error = err;
  3686. for (const key of Object.keys(error.errors || {})) {
  3687. paths.delete(key);
  3688. }
  3689. }
  3690. const promises = [];
  3691. for (const path of paths) {
  3692. const schemaType = schema.path(path);
  3693. if (schemaType == null) {
  3694. continue;
  3695. }
  3696. const pieces = path.indexOf('.') === -1 ? [path] : path.split('.');
  3697. let cur = obj;
  3698. for (let i = 0; i < pieces.length - 1; ++i) {
  3699. cur = cur[pieces[i]];
  3700. }
  3701. const val = get(obj, path, void 0);
  3702. promises.push(
  3703. schemaType.doValidate(val, context, { path: path }).catch(err => {
  3704. error = error || new ValidationError();
  3705. error.addError(path, err);
  3706. })
  3707. );
  3708. }
  3709. await Promise.all(promises);
  3710. if (error != null) {
  3711. throw error;
  3712. }
  3713. return obj;
  3714. };
  3715. /**
  3716. * Populates document references.
  3717. *
  3718. * Changed in Mongoose 6: the model you call `populate()` on should be the
  3719. * "local field" model, **not** the "foreign field" model.
  3720. *
  3721. * #### Available top-level options:
  3722. *
  3723. * - path: space delimited path(s) to populate
  3724. * - select: optional fields to select
  3725. * - match: optional query conditions to match
  3726. * - model: optional name of the model to use for population
  3727. * - options: optional query options like sort, limit, etc
  3728. * - justOne: optional boolean, if true Mongoose will always set `path` to a document, or `null` if no document was found. If false, Mongoose will always set `path` to an array, which will be empty if no documents are found. Inferred from schema by default.
  3729. * - strictPopulate: optional boolean, set to `false` to allow populating paths that aren't in the schema.
  3730. * - forceRepopulate: optional boolean, defaults to `true`. Set to `false` to prevent Mongoose from repopulating paths that are already populated
  3731. *
  3732. * #### Example:
  3733. *
  3734. * const Dog = mongoose.model('Dog', new Schema({ name: String, breed: String }));
  3735. * const Person = mongoose.model('Person', new Schema({
  3736. * name: String,
  3737. * pet: { type: mongoose.ObjectId, ref: 'Dog' }
  3738. * }));
  3739. *
  3740. * const pets = await Pet.create([
  3741. * { name: 'Daisy', breed: 'Beagle' },
  3742. * { name: 'Einstein', breed: 'Catalan Sheepdog' }
  3743. * ]);
  3744. *
  3745. * // populate many plain objects
  3746. * const users = [
  3747. * { name: 'John Wick', dog: pets[0]._id },
  3748. * { name: 'Doc Brown', dog: pets[1]._id }
  3749. * ];
  3750. * await User.populate(users, { path: 'dog', select: 'name' });
  3751. * users[0].dog.name; // 'Daisy'
  3752. * users[0].dog.breed; // undefined because of `select`
  3753. *
  3754. * @param {Document|Array} docs Either a single document or array of documents to populate.
  3755. * @param {Object|String} options Either the paths to populate or an object specifying all parameters
  3756. * @param {string} [options.path=null] The path to populate.
  3757. * @param {string|PopulateOptions} [options.populate=null] Recursively populate paths in the populated documents. See [deep populate docs](https://mongoosejs.com/docs/populate.html#deep-populate).
  3758. * @param {boolean} [options.retainNullValues=false] By default, Mongoose removes null and undefined values from populated arrays. Use this option to make `populate()` retain `null` and `undefined` array entries.
  3759. * @param {boolean} [options.getters=false] If true, Mongoose will call any getters defined on the `localField`. By default, Mongoose gets the raw value of `localField`. For example, you would need to set this option to `true` if you wanted to [add a `lowercase` getter to your `localField`](https://mongoosejs.com/docs/schematypes.html#schematype-options).
  3760. * @param {boolean} [options.clone=false] When you do `BlogPost.find().populate('author')`, blog posts with the same author will share 1 copy of an `author` doc. Enable this option to make Mongoose clone populated docs before assigning them.
  3761. * @param {Object|Function} [options.match=null] Add an additional filter to the populate query. Can be a filter object containing [MongoDB query syntax](https://www.mongodb.com/docs/manual/tutorial/query-documents/), or a function that returns a filter object.
  3762. * @param {Boolean} [options.skipInvalidIds=false] By default, Mongoose throws a cast error if `localField` and `foreignField` schemas don't line up. If you enable this option, Mongoose will instead filter out any `localField` properties that cannot be casted to `foreignField`'s schema type.
  3763. * @param {Number} [options.perDocumentLimit=null] For legacy reasons, `limit` with `populate()` may give incorrect results because it only executes a single query for every document being populated. If you set `perDocumentLimit`, Mongoose will ensure correct `limit` per document by executing a separate query for each document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })` will execute 2 additional queries if `.find()` returns 2 documents.
  3764. * @param {Boolean} [options.strictPopulate=true] Set to false to allow populating paths that aren't defined in the given model's schema.
  3765. * @param {Object} [options.options=null] Additional options like `limit` and `lean`.
  3766. * @param {Function} [options.transform=null] Function that Mongoose will call on every populated document that allows you to transform the populated document.
  3767. * @param {Boolean} [options.forceRepopulate=true] Set to `false` to prevent Mongoose from repopulating paths that are already populated
  3768. * @param {Boolean} [options.ordered=false] Set to `true` to execute any populate queries one at a time, as opposed to in parallel. Set this option to `true` if populating multiple paths or paths with multiple models in transactions.
  3769. * @return {Promise}
  3770. * @api public
  3771. */
  3772. Model.populate = async function populate(docs, paths) {
  3773. _checkContext(this, 'populate');
  3774. if (typeof paths === 'function' || typeof arguments[2] === 'function') {
  3775. throw new MongooseError('Model.populate() no longer accepts a callback');
  3776. }
  3777. // normalized paths
  3778. paths = utils.populate(paths);
  3779. if (paths.length === 0) {
  3780. return docs;
  3781. }
  3782. // each path has its own query options and must be executed separately
  3783. if (paths.find(p => p.ordered)) {
  3784. // Populate in series, primarily for transactions because MongoDB doesn't support multiple operations on
  3785. // one transaction in parallel.
  3786. // Note that if _any_ path has `ordered`, we make the top-level populate `ordered` as well.
  3787. for (const path of paths) {
  3788. await _populatePath(this, docs, path);
  3789. }
  3790. } else {
  3791. // By default, populate in parallel
  3792. const promises = [];
  3793. for (const path of paths) {
  3794. promises.push(_populatePath(this, docs, path));
  3795. }
  3796. await Promise.all(promises);
  3797. }
  3798. return docs;
  3799. };
  3800. /*!
  3801. * Populates `docs` for a single `populateOptions` instance.
  3802. */
  3803. const excludeIdReg = /\s?-_id\s?/;
  3804. const excludeIdRegGlobal = /\s?-_id\s?/g;
  3805. async function _populatePath(model, docs, populateOptions) {
  3806. if (populateOptions.strictPopulate == null) {
  3807. if (populateOptions._localModel?.schema._userProvidedOptions.strictPopulate != null) {
  3808. populateOptions.strictPopulate = populateOptions._localModel.schema._userProvidedOptions.strictPopulate;
  3809. } else if (populateOptions._localModel != null && model.base.options.strictPopulate != null) {
  3810. populateOptions.strictPopulate = model.base.options.strictPopulate;
  3811. } else if (model.base.options.strictPopulate != null) {
  3812. populateOptions.strictPopulate = model.base.options.strictPopulate;
  3813. }
  3814. }
  3815. // normalize single / multiple docs passed
  3816. if (!Array.isArray(docs)) {
  3817. docs = [docs];
  3818. }
  3819. if (docs.length === 0 || docs.every(utils.isNullOrUndefined)) {
  3820. return;
  3821. }
  3822. const modelsMap = getModelsMapForPopulate(model, docs, populateOptions);
  3823. if (modelsMap instanceof MongooseError) {
  3824. throw modelsMap;
  3825. }
  3826. const len = modelsMap.length;
  3827. let vals = [];
  3828. function flatten(item) {
  3829. // no need to include undefined values in our query
  3830. return undefined !== item;
  3831. }
  3832. let hasOne = false;
  3833. const params = [];
  3834. for (let i = 0; i < len; ++i) {
  3835. const mod = modelsMap[i];
  3836. let select = mod.options.select;
  3837. let ids = utils.array.flatten(mod.ids, flatten);
  3838. ids = utils.array.unique(ids);
  3839. const assignmentOpts = {};
  3840. assignmentOpts.sort = mod &&
  3841. mod.options &&
  3842. mod.options.options &&
  3843. mod.options.options.sort || void 0;
  3844. assignmentOpts.excludeId = excludeIdReg.test(select) || (select && select._id === 0);
  3845. // Lean transform may delete `_id`, which would cause assignment
  3846. // to fail. So delay running lean transform until _after_
  3847. // `_assign()`
  3848. if (mod.options &&
  3849. mod.options.options &&
  3850. mod.options.options.lean &&
  3851. mod.options.options.lean.transform) {
  3852. mod.options.options._leanTransform = mod.options.options.lean.transform;
  3853. mod.options.options.lean = true;
  3854. }
  3855. if (ids.length === 0 || ids.every(utils.isNullOrUndefined)) {
  3856. // Ensure that we set to 0 or empty array even
  3857. // if we don't actually execute a query to make sure there's a value
  3858. // and we know this path was populated for future sets. See gh-7731, gh-8230
  3859. _assign(model, [], mod, assignmentOpts);
  3860. continue;
  3861. }
  3862. hasOne = true;
  3863. if (typeof populateOptions.foreignField === 'string') {
  3864. mod.foreignField.clear();
  3865. mod.foreignField.add(populateOptions.foreignField);
  3866. }
  3867. const match = createPopulateQueryFilter(ids, mod.match, mod.foreignField, mod.model, mod.options.skipInvalidIds);
  3868. if (assignmentOpts.excludeId) {
  3869. // override the exclusion from the query so we can use the _id
  3870. // for document matching during assignment. we'll delete the
  3871. // _id back off before returning the result.
  3872. if (typeof select === 'string') {
  3873. select = select.replace(excludeIdRegGlobal, ' ');
  3874. } else if (Array.isArray(select)) {
  3875. select = select.filter(field => field !== '-_id');
  3876. } else {
  3877. // preserve original select conditions by copying
  3878. select = { ...select };
  3879. delete select._id;
  3880. }
  3881. }
  3882. if (mod.options.options?.limit != null) {
  3883. assignmentOpts.originalLimit = mod.options.options.limit;
  3884. } else if (mod.options.limit != null) {
  3885. assignmentOpts.originalLimit = mod.options.limit;
  3886. }
  3887. params.push([mod, match, select, assignmentOpts]);
  3888. }
  3889. if (!hasOne) {
  3890. // If models but no docs, skip further deep populate.
  3891. if (modelsMap.length !== 0) {
  3892. return;
  3893. }
  3894. // If no models and no docs to populate but we have a nested populate,
  3895. // probably a case of unnecessarily populating a non-ref path re: gh-8946
  3896. if (populateOptions.populate != null) {
  3897. const opts = utils.populate(populateOptions.populate).map(pop => Object.assign({}, pop, {
  3898. path: populateOptions.path + '.' + pop.path
  3899. }));
  3900. return model.populate(docs, opts);
  3901. }
  3902. return;
  3903. }
  3904. if (populateOptions.ordered) {
  3905. // Populate in series, primarily for transactions because MongoDB doesn't support multiple operations on
  3906. // one transaction in parallel.
  3907. for (const arr of params) {
  3908. await _execPopulateQuery.apply(null, arr).then(valsFromDb => { vals = vals.concat(valsFromDb); });
  3909. }
  3910. } else {
  3911. // By default, populate in parallel
  3912. const promises = [];
  3913. for (const arr of params) {
  3914. promises.push(_execPopulateQuery.apply(null, arr).then(valsFromDb => { vals = vals.concat(valsFromDb); }));
  3915. }
  3916. await Promise.all(promises);
  3917. }
  3918. for (const arr of params) {
  3919. const mod = arr[0];
  3920. const assignmentOpts = arr[3];
  3921. for (const val of vals) {
  3922. mod.options._childDocs.push(val);
  3923. }
  3924. _assign(model, vals, mod, assignmentOpts);
  3925. }
  3926. for (const arr of params) {
  3927. removeDeselectedForeignField(arr[0].foreignField, arr[0].options, vals);
  3928. }
  3929. for (const arr of params) {
  3930. const mod = arr[0];
  3931. if (mod.options?.options?._leanTransform) {
  3932. for (const doc of vals) {
  3933. mod.options.options._leanTransform(doc);
  3934. }
  3935. }
  3936. }
  3937. }
  3938. /*!
  3939. * ignore
  3940. */
  3941. function _execPopulateQuery(mod, match, select) {
  3942. let subPopulate = clone(mod.options.populate);
  3943. const queryOptions = {};
  3944. if (mod.options.skip !== undefined) {
  3945. queryOptions.skip = mod.options.skip;
  3946. }
  3947. if (mod.options.limit !== undefined) {
  3948. queryOptions.limit = mod.options.limit;
  3949. }
  3950. if (mod.options.perDocumentLimit !== undefined) {
  3951. queryOptions.perDocumentLimit = mod.options.perDocumentLimit;
  3952. }
  3953. Object.assign(queryOptions, mod.options.options);
  3954. if (mod.count) {
  3955. delete queryOptions.skip;
  3956. }
  3957. if (queryOptions.perDocumentLimit != null) {
  3958. queryOptions.limit = queryOptions.perDocumentLimit;
  3959. delete queryOptions.perDocumentLimit;
  3960. } else if (queryOptions.limit != null) {
  3961. queryOptions.limit = queryOptions.limit * mod.ids.length;
  3962. }
  3963. const query = mod.model.find(match, select, queryOptions);
  3964. // If we're doing virtual populate and projection is inclusive and foreign
  3965. // field is not selected, automatically select it because mongoose needs it.
  3966. // If projection is exclusive and client explicitly unselected the foreign
  3967. // field, that's the client's fault.
  3968. for (const foreignField of mod.foreignField) {
  3969. if (foreignField !== '_id' &&
  3970. query.selectedInclusively() &&
  3971. !isPathSelectedInclusive(query._fields, foreignField)) {
  3972. query.select(foreignField);
  3973. }
  3974. }
  3975. // If using count, still need the `foreignField` so we can match counts
  3976. // to documents, otherwise we would need a separate `count()` for every doc.
  3977. if (mod.count) {
  3978. for (const foreignField of mod.foreignField) {
  3979. query.select(foreignField);
  3980. }
  3981. }
  3982. // If we need to sub-populate, call populate recursively
  3983. if (subPopulate) {
  3984. // If subpopulating on a discriminator, skip check for non-existent
  3985. // paths. Because the discriminator may not have the path defined.
  3986. if (mod.model.baseModelName != null) {
  3987. if (Array.isArray(subPopulate)) {
  3988. subPopulate.forEach(pop => { pop.strictPopulate = false; });
  3989. } else if (typeof subPopulate === 'string') {
  3990. subPopulate = { path: subPopulate, strictPopulate: false };
  3991. } else {
  3992. subPopulate.strictPopulate = false;
  3993. }
  3994. }
  3995. const basePath = mod.options._fullPath || mod.options.path;
  3996. if (Array.isArray(subPopulate)) {
  3997. for (const pop of subPopulate) {
  3998. pop._fullPath = basePath + '.' + pop.path;
  3999. }
  4000. } else if (typeof subPopulate === 'object') {
  4001. subPopulate._fullPath = basePath + '.' + subPopulate.path;
  4002. }
  4003. query.populate(subPopulate);
  4004. }
  4005. return query.exec().then(
  4006. docs => {
  4007. for (const val of docs) {
  4008. leanPopulateMap.set(val, mod.model);
  4009. }
  4010. return docs;
  4011. }
  4012. );
  4013. }
  4014. /*!
  4015. * ignore
  4016. */
  4017. function _assign(model, vals, mod, assignmentOpts) {
  4018. const options = mod.options;
  4019. const isVirtual = mod.isVirtual;
  4020. const justOne = mod.justOne;
  4021. let _val;
  4022. const lean = options &&
  4023. options.options &&
  4024. options.options.lean || false;
  4025. const len = vals.length;
  4026. const rawOrder = {};
  4027. const rawDocs = {};
  4028. let key;
  4029. let val;
  4030. // Clone because `assignRawDocsToIdStructure` will mutate the array
  4031. const allIds = clone(mod.allIds);
  4032. // optimization:
  4033. // record the document positions as returned by
  4034. // the query result.
  4035. for (let i = 0; i < len; i++) {
  4036. val = vals[i];
  4037. if (val == null) {
  4038. continue;
  4039. }
  4040. for (const foreignField of mod.foreignField) {
  4041. _val = utils.getValue(foreignField, val);
  4042. if (Array.isArray(_val)) {
  4043. _val = utils.array.unique(utils.array.flatten(_val));
  4044. for (let __val of _val) {
  4045. if (__val instanceof Document) {
  4046. __val = __val._doc._id;
  4047. }
  4048. key = String(__val);
  4049. if (rawDocs[key]) {
  4050. if (Array.isArray(rawDocs[key])) {
  4051. rawDocs[key].push(val);
  4052. rawOrder[key].push(i);
  4053. } else {
  4054. rawDocs[key] = [rawDocs[key], val];
  4055. rawOrder[key] = [rawOrder[key], i];
  4056. }
  4057. } else {
  4058. if (isVirtual && !justOne) {
  4059. rawDocs[key] = [val];
  4060. rawOrder[key] = [i];
  4061. } else {
  4062. rawDocs[key] = val;
  4063. rawOrder[key] = i;
  4064. }
  4065. }
  4066. }
  4067. } else {
  4068. if (_val instanceof Document) {
  4069. _val = _val._doc._id;
  4070. }
  4071. key = String(_val);
  4072. if (rawDocs[key]) {
  4073. if (Array.isArray(rawDocs[key])) {
  4074. rawDocs[key].push(val);
  4075. rawOrder[key].push(i);
  4076. } else if (isVirtual ||
  4077. rawDocs[key].constructor !== val.constructor ||
  4078. (rawDocs[key] instanceof Document ? String(rawDocs[key]._doc._id) : String(rawDocs[key]._id)) !== (val instanceof Document ? String(val._doc._id) : String(val._id))) {
  4079. // May need to store multiple docs with the same id if there's multiple models
  4080. // if we have discriminators or a ref function. But avoid converting to an array
  4081. // if we have multiple queries on the same model because of `perDocumentLimit` re: gh-9906
  4082. rawDocs[key] = [rawDocs[key], val];
  4083. rawOrder[key] = [rawOrder[key], i];
  4084. }
  4085. } else {
  4086. rawDocs[key] = val;
  4087. rawOrder[key] = i;
  4088. }
  4089. }
  4090. // flag each as result of population
  4091. if (!lean) {
  4092. val.$__.wasPopulated = val.$__.wasPopulated || { value: _val };
  4093. }
  4094. }
  4095. }
  4096. assignVals({
  4097. originalModel: model,
  4098. // If virtual, make sure to not mutate original field
  4099. rawIds: mod.isVirtual ? allIds : mod.allIds,
  4100. allIds: allIds,
  4101. unpopulatedValues: mod.unpopulatedValues,
  4102. foreignField: mod.foreignField,
  4103. rawDocs: rawDocs,
  4104. rawOrder: rawOrder,
  4105. docs: mod.docs,
  4106. path: options.path,
  4107. options: assignmentOpts,
  4108. justOne: mod.justOne,
  4109. isVirtual: mod.isVirtual,
  4110. allOptions: mod,
  4111. populatedModel: mod.model,
  4112. lean: lean,
  4113. virtual: mod.virtual,
  4114. count: mod.count,
  4115. match: mod.match
  4116. });
  4117. }
  4118. /**
  4119. * Compiler utility.
  4120. *
  4121. * @param {String|Function} name model name or class extending Model
  4122. * @param {Schema} schema
  4123. * @param {String} collectionName
  4124. * @param {Connection} connection
  4125. * @param {Mongoose} base mongoose instance
  4126. * @api private
  4127. */
  4128. Model.compile = function compile(name, schema, collectionName, connection, base) {
  4129. const versioningEnabled = schema.options.versionKey !== false;
  4130. if (versioningEnabled && !schema.paths[schema.options.versionKey]) {
  4131. // add versioning to top level documents only
  4132. const o = {};
  4133. o[schema.options.versionKey] = Number;
  4134. schema.add(o);
  4135. }
  4136. let model;
  4137. if (typeof name === 'function' && name.prototype instanceof Model) {
  4138. model = name;
  4139. name = model.name;
  4140. schema.loadClass(model, false);
  4141. model.prototype.$isMongooseModelPrototype = true;
  4142. } else {
  4143. // generate new class
  4144. model = function model(doc, fields, skipId) {
  4145. model.hooks.execPreSync('createModel', doc);
  4146. if (!(this instanceof model)) {
  4147. return new model(doc, fields, skipId);
  4148. }
  4149. const discriminatorKey = model.schema.options.discriminatorKey;
  4150. if (model.discriminators == null || doc == null || doc[discriminatorKey] == null) {
  4151. Model.call(this, doc, fields, skipId);
  4152. return;
  4153. }
  4154. // If discriminator key is set, use the discriminator instead (gh-7586)
  4155. const Discriminator = model.discriminators[doc[discriminatorKey]] ||
  4156. getDiscriminatorByValue(model.discriminators, doc[discriminatorKey]);
  4157. if (Discriminator != null) {
  4158. return new Discriminator(doc, fields, skipId);
  4159. }
  4160. // Otherwise, just use the top-level model
  4161. Model.call(this, doc, fields, skipId);
  4162. };
  4163. }
  4164. model.hooks = schema.s.hooks.clone();
  4165. model.base = base;
  4166. model.modelName = name;
  4167. if (!(model.prototype instanceof Model)) {
  4168. Object.setPrototypeOf(model, Model);
  4169. Object.setPrototypeOf(model.prototype, Model.prototype);
  4170. }
  4171. model.model = function model(name) {
  4172. return this.db.model(name);
  4173. };
  4174. model.db = connection;
  4175. model.prototype.db = connection;
  4176. model.prototype[modelDbSymbol] = connection;
  4177. model.discriminators = model.prototype.discriminators = undefined;
  4178. model[modelSymbol] = true;
  4179. model.events = new EventEmitter();
  4180. schema._preCompile();
  4181. const _userProvidedOptions = schema._userProvidedOptions || {};
  4182. const collectionOptions = {
  4183. schemaUserProvidedOptions: _userProvidedOptions,
  4184. capped: schema.options.capped,
  4185. Promise: model.base.Promise,
  4186. modelName: name
  4187. };
  4188. if (schema.options.autoCreate !== void 0) {
  4189. collectionOptions.autoCreate = schema.options.autoCreate;
  4190. }
  4191. const collection = connection.collection(
  4192. collectionName,
  4193. collectionOptions
  4194. );
  4195. model.prototype.collection = collection;
  4196. model.prototype.$collection = collection;
  4197. model.prototype[modelCollectionSymbol] = collection;
  4198. model.prototype.$__setSchema(schema);
  4199. // apply methods and statics
  4200. applyMethods(model, schema);
  4201. applyStatics(model, schema);
  4202. applyHooks(model, schema);
  4203. applyStaticHooks(model, schema.s.hooks, schema.statics);
  4204. model.schema = model.prototype.$__schema;
  4205. model.collection = collection;
  4206. model.$__collection = collection;
  4207. // Create custom query constructor
  4208. model.Query = function() {
  4209. Query.apply(this, arguments);
  4210. };
  4211. Object.setPrototypeOf(model.Query.prototype, Query.prototype);
  4212. model.Query.base = Query.base;
  4213. model.Query.prototype.constructor = Query;
  4214. model._applyQueryMiddleware();
  4215. applyQueryMethods(model, schema.query);
  4216. return model;
  4217. };
  4218. /**
  4219. * If auto encryption is enabled, returns a ClientEncryption instance that is configured with the same settings that
  4220. * Mongoose's underlying MongoClient is using. If the client has not yet been configured, returns null.
  4221. *
  4222. * @returns {ClientEncryption | null}
  4223. */
  4224. Model.clientEncryption = function clientEncryption() {
  4225. const ClientEncryption = this.base.driver.get().ClientEncryption;
  4226. if (!ClientEncryption) {
  4227. throw new Error('The mongodb driver must be used to obtain a ClientEncryption object.');
  4228. }
  4229. const client = this.collection?.conn?.client;
  4230. if (!client) return null;
  4231. const autoEncryptionOptions = client.options.autoEncryption;
  4232. if (!autoEncryptionOptions) return null;
  4233. const {
  4234. keyVaultNamespace,
  4235. keyVaultClient,
  4236. kmsProviders,
  4237. credentialProviders,
  4238. proxyOptions,
  4239. tlsOptions
  4240. } = autoEncryptionOptions;
  4241. return new ClientEncryption(keyVaultClient ?? client,
  4242. { keyVaultNamespace, kmsProviders, credentialProviders, proxyOptions, tlsOptions }
  4243. );
  4244. };
  4245. /**
  4246. * Update this model to use the new connection, including updating all internal
  4247. * references and creating a new `Collection` instance using the new connection.
  4248. * Not for external use, only used by `setDriver()` to ensure that you can still
  4249. * call `setDriver()` after creating a model using `mongoose.model()`.
  4250. *
  4251. * @param {Connection} newConnection the new connection to use
  4252. * @api private
  4253. */
  4254. Model.$__updateConnection = function $__updateConnection(newConnection) {
  4255. this.db = newConnection;
  4256. this.prototype.db = newConnection;
  4257. this.prototype[modelDbSymbol] = newConnection;
  4258. const collection = newConnection.collection(
  4259. this.collection.collectionName,
  4260. this.collection.opts
  4261. );
  4262. this.prototype.collection = collection;
  4263. this.prototype.$collection = collection;
  4264. this.prototype[modelCollectionSymbol] = collection;
  4265. this.collection = collection;
  4266. this.$__collection = collection;
  4267. };
  4268. /**
  4269. * Register custom query methods for this model
  4270. *
  4271. * @param {Model} model
  4272. * @param {Schema} schema
  4273. * @api private
  4274. */
  4275. function applyQueryMethods(model, methods) {
  4276. for (const i in methods) {
  4277. model.Query.prototype[i] = methods[i];
  4278. }
  4279. }
  4280. /**
  4281. * Subclass this model with `conn`, `schema`, and `collection` settings.
  4282. *
  4283. * @param {Connection} conn
  4284. * @param {Schema} [schema]
  4285. * @param {String} [collection]
  4286. * @return {Model}
  4287. * @api private
  4288. * @memberOf Model
  4289. * @static
  4290. * @method __subclass
  4291. */
  4292. Model.__subclass = function subclass(conn, schema, collection) {
  4293. // subclass model using this connection and collection name
  4294. const _this = this;
  4295. const Model = function Model(doc, fields, skipId) {
  4296. if (!(this instanceof Model)) {
  4297. return new Model(doc, fields, skipId);
  4298. }
  4299. _this.call(this, doc, fields, skipId);
  4300. };
  4301. Object.setPrototypeOf(Model, _this);
  4302. Object.setPrototypeOf(Model.prototype, _this.prototype);
  4303. Model.db = conn;
  4304. Model.prototype.db = conn;
  4305. Model.prototype[modelDbSymbol] = conn;
  4306. _this[subclassedSymbol] = _this[subclassedSymbol] || [];
  4307. _this[subclassedSymbol].push(Model);
  4308. if (_this.discriminators != null) {
  4309. Model.discriminators = {};
  4310. for (const key of Object.keys(_this.discriminators)) {
  4311. Model.discriminators[key] = _this.discriminators[key].
  4312. __subclass(_this.db, _this.discriminators[key].schema, collection);
  4313. }
  4314. }
  4315. const s = schema && typeof schema !== 'string'
  4316. ? schema
  4317. : _this.prototype.$__schema;
  4318. const options = s.options || {};
  4319. const _userProvidedOptions = s._userProvidedOptions || {};
  4320. if (!collection) {
  4321. collection = _this.prototype.$__schema.get('collection') ||
  4322. utils.toCollectionName(_this.modelName, this.base.pluralize());
  4323. }
  4324. const collectionOptions = {
  4325. schemaUserProvidedOptions: _userProvidedOptions,
  4326. capped: s && options.capped
  4327. };
  4328. Model.prototype.collection = conn.collection(collection, collectionOptions);
  4329. Model.prototype.$collection = Model.prototype.collection;
  4330. Model.prototype[modelCollectionSymbol] = Model.prototype.collection;
  4331. Model.collection = Model.prototype.collection;
  4332. Model.$__collection = Model.collection;
  4333. // Errors handled internally, so ignore
  4334. Model.init().catch(() => {});
  4335. return Model;
  4336. };
  4337. /**
  4338. * Apply changes made to this model's schema after this model was compiled.
  4339. * By default, adding virtuals and other properties to a schema after the model is compiled does nothing.
  4340. * Call this function to apply virtuals and properties that were added later.
  4341. *
  4342. * #### Example:
  4343. *
  4344. * const schema = new mongoose.Schema({ field: String });
  4345. * const TestModel = mongoose.model('Test', schema);
  4346. * TestModel.schema.virtual('myVirtual').get(function() {
  4347. * return this.field + ' from myVirtual';
  4348. * });
  4349. * const doc = new TestModel({ field: 'Hello' });
  4350. * doc.myVirtual; // undefined
  4351. *
  4352. * TestModel.recompileSchema();
  4353. * doc.myVirtual; // 'Hello from myVirtual'
  4354. *
  4355. * @return {undefined}
  4356. * @api public
  4357. * @memberOf Model
  4358. * @static
  4359. * @method recompileSchema
  4360. */
  4361. Model.recompileSchema = function recompileSchema() {
  4362. this.prototype.$__setSchema(this.schema);
  4363. if (this.schema._applyDiscriminators != null) {
  4364. for (const disc of this.schema._applyDiscriminators.keys()) {
  4365. this.discriminator(disc, this.schema._applyDiscriminators.get(disc));
  4366. }
  4367. }
  4368. delete this.schema._defaultToObjectOptionsMap;
  4369. applyEmbeddedDiscriminators(this.schema, new WeakSet(), true);
  4370. };
  4371. /**
  4372. * Helper for console.log. Given a model named 'MyModel', returns the string
  4373. * `'Model { MyModel }'`.
  4374. *
  4375. * #### Example:
  4376. *
  4377. * const MyModel = mongoose.model('Test', Schema({ name: String }));
  4378. * MyModel.inspect(); // 'Model { Test }'
  4379. * console.log(MyModel); // Prints 'Model { Test }'
  4380. *
  4381. * @api public
  4382. */
  4383. Model.inspect = function() {
  4384. return `Model { ${this.modelName} }`;
  4385. };
  4386. /**
  4387. * Return the MongoDB namespace for this model as a string. The namespace is the database name, followed by '.', followed by the collection name.
  4388. *
  4389. * #### Example:
  4390. *
  4391. * const conn = mongoose.createConnection('mongodb://127.0.0.1:27017/mydb');
  4392. * const TestModel = conn.model('Test', mongoose.Schema({ name: String }));
  4393. *
  4394. * TestModel.namespace(); // 'mydb.tests'
  4395. *
  4396. * @api public
  4397. */
  4398. Model.namespace = function namespace() {
  4399. return this.db.name + '.' + this.collection.collectionName;
  4400. };
  4401. if (util.inspect.custom) {
  4402. // Avoid Node deprecation warning DEP0079
  4403. Model[util.inspect.custom] = Model.inspect;
  4404. }
  4405. /*!
  4406. * Applies query middleware from this model's schema to this model's
  4407. * Query constructor.
  4408. */
  4409. Model._applyQueryMiddleware = function _applyQueryMiddleware() {
  4410. const Query = this.Query;
  4411. const queryMiddleware = this.schema.s.hooks.filter(hook => {
  4412. const contexts = _getContexts(hook);
  4413. if (hook.name === 'validate') {
  4414. return !!contexts.query;
  4415. }
  4416. if (hook.name === 'deleteOne' || hook.name === 'updateOne') {
  4417. return !!contexts.query || utils.hasOwnKeys(contexts) === false;
  4418. }
  4419. if (hook.query != null || hook.document != null) {
  4420. return !!hook.query;
  4421. }
  4422. return true;
  4423. });
  4424. Query.prototype._queryMiddleware = queryMiddleware;
  4425. };
  4426. function _getContexts(hook) {
  4427. const ret = {};
  4428. if (Object.hasOwn(hook, 'query')) {
  4429. ret.query = hook.query;
  4430. }
  4431. if (Object.hasOwn(hook, 'document')) {
  4432. ret.document = hook.document;
  4433. }
  4434. return ret;
  4435. }
  4436. /*!
  4437. * Module exports.
  4438. */
  4439. module.exports = exports = Model;