|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877 |
- 'use strict';
-
- /*!
- * Module dependencies.
- */
-
- const Aggregate = require('./aggregate');
- const ChangeStream = require('./cursor/ChangeStream');
- const Document = require('./document');
- const DocumentNotFoundError = require('./error').DocumentNotFoundError;
- const DivergentArrayError = require('./error').DivergentArrayError;
- const Error = require('./error');
- const EventEmitter = require('events').EventEmitter;
- const MongooseBuffer = require('./types/buffer');
- const OverwriteModelError = require('./error').OverwriteModelError;
- const PromiseProvider = require('./promise_provider');
- const Query = require('./query');
- const RemoveOptions = require('./options/removeOptions');
- const SaveOptions = require('./options/saveOptions');
- const Schema = require('./schema');
- const VersionError = require('./error').VersionError;
- const ParallelSaveError = require('./error').ParallelSaveError;
- const applyQueryMiddleware = require('./helpers/query/applyQueryMiddleware');
- const applyHooks = require('./helpers/model/applyHooks');
- const applyMethods = require('./helpers/model/applyMethods');
- const applyStaticHooks = require('./helpers/model/applyStaticHooks');
- const applyStatics = require('./helpers/model/applyStatics');
- const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
- const assignVals = require('./helpers/populate/assignVals');
- const castBulkWrite = require('./helpers/model/castBulkWrite');
- const discriminator = require('./helpers/model/discriminator');
- const getDiscriminatorByValue = require('./queryhelpers').getDiscriminatorByValue;
- const immediate = require('./helpers/immediate');
- const internalToObjectOptions = require('./options').internalToObjectOptions;
- const isPathExcluded = require('./helpers/projection/isPathExcluded');
- const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedInclusive');
- const get = require('./helpers/get');
- const getSchemaTypes = require('./helpers/populate/getSchemaTypes');
- const getVirtual = require('./helpers/populate/getVirtual');
- const leanPopulateMap = require('./helpers/populate/leanPopulateMap');
- const modifiedPaths = require('./helpers/update/modifiedPaths');
- const normalizeRefPath = require('./helpers/populate/normalizeRefPath');
- const parallel = require('async/parallel');
- const parallelLimit = require('async/parallelLimit');
- const util = require('util');
- const utils = require('./utils');
-
- const VERSION_WHERE = 1;
- const VERSION_INC = 2;
- const VERSION_ALL = VERSION_WHERE | VERSION_INC;
-
- const arrayAtomicsSymbol = require('./helpers/symbols').arrayAtomicsSymbol;
- const modelCollectionSymbol = Symbol.for('mongoose#Model#collection');
- const modelSymbol = require('./helpers/symbols').modelSymbol;
- const populateModelSymbol = require('./helpers/symbols').populateModelSymbol;
- const schemaMixedSymbol = require('./schema/symbols').schemaMixedSymbol;
- const subclassedSymbol = Symbol('mongoose#Model#subclassed');
-
- /**
- * A Model is a class that's your primary tool for interacting with MongoDB.
- * An instance of a Model is called a [Document](./api.html#Document).
- *
- * In Mongoose, the term "Model" refers to subclasses of the `mongoose.Model`
- * class. You should not use the `mongoose.Model` class directly. The
- * [`mongoose.model()`](./api.html#mongoose_Mongoose-model) and
- * [`connection.model()`](./api.html#connection_Connection-model) functions
- * create subclasses of `mongoose.Model` as shown below.
- *
- * ####Example:
- *
- * // `UserModel` is a "Model", a subclass of `mongoose.Model`.
- * const UserModel = mongoose.model('User', new Schema({ name: String }));
- *
- * // You can use a Model to create new documents using `new`:
- * const userDoc = new UserModel({ name: 'Foo' });
- * await userDoc.save();
- *
- * // You also use a model to create queries:
- * const userFromDb = await UserModel.findOne({ name: 'Foo' });
- *
- * @param {Object} doc values for initial set
- * @param [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](./api.html#query_Query-select).
- * @inherits Document http://mongoosejs.com/docs/api.html#document-js
- * @event `error`: If listening to this event, 'error' is emitted when a document was saved without passing a callback and an `error` occurred. If not listening, the event bubbles to the connection used to create this Model.
- * @event `index`: Emitted after `Model#ensureIndexes` completes. If an error occurred it is passed with the event.
- * @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.
- * @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.
- * @api public
- */
-
- function Model(doc, fields, skipId) {
- if (fields instanceof Schema) {
- throw new TypeError('2nd argument to `Model` must be a POJO or string, ' +
- '**not** a schema. Make sure you\'re calling `mongoose.model()`, not ' +
- '`mongoose.Model()`.');
- }
- Document.call(this, doc, fields, skipId);
- }
-
- /*!
- * Inherits from Document.
- *
- * All Model.prototype features are available on
- * top level (non-sub) documents.
- */
-
- Model.prototype.__proto__ = Document.prototype;
- Model.prototype.$isMongooseModelPrototype = true;
-
- /**
- * Connection the model uses.
- *
- * @api public
- * @property db
- * @memberOf Model
- * @instance
- */
-
- Model.prototype.db;
-
- /**
- * Collection the model uses.
- *
- * This property is read-only. Modifying this property is a no-op.
- *
- * @api public
- * @property collection
- * @memberOf Model
- * @instance
- */
-
- Model.prototype.collection;
-
- /**
- * The name of the model
- *
- * @api public
- * @property modelName
- * @memberOf Model
- * @instance
- */
-
- Model.prototype.modelName;
-
- /**
- * Additional properties to attach to the query when calling `save()` and
- * `isNew` is false.
- *
- * @api public
- * @property $where
- * @memberOf Model
- * @instance
- */
-
- Model.prototype.$where;
-
- /**
- * If this is a discriminator model, `baseModelName` is the name of
- * the base model.
- *
- * @api public
- * @property baseModelName
- * @memberOf Model
- * @instance
- */
-
- Model.prototype.baseModelName;
-
- /**
- * Event emitter that reports any errors that occurred. Useful for global error
- * handling.
- *
- * ####Example:
- *
- * MyModel.events.on('error', err => console.log(err.message));
- *
- * // Prints a 'CastError' because of the above handler
- * await MyModel.findOne({ _id: 'notanid' }).catch(noop);
- *
- * @api public
- * @fires error whenever any query or model function errors
- * @memberOf Model
- * @static events
- */
-
- Model.events;
-
- /*!
- * Compiled middleware for this model. Set in `applyHooks()`.
- *
- * @api private
- * @property _middleware
- * @memberOf Model
- * @static
- */
-
- Model._middleware;
-
- /*!
- * ignore
- */
-
- function _applyCustomWhere(doc, where) {
- if (doc.$where == null) {
- return;
- }
-
- const keys = Object.keys(doc.$where);
- const len = keys.length;
- for (let i = 0; i < len; ++i) {
- where[keys[i]] = doc.$where[keys[i]];
- }
- }
-
- /*!
- * ignore
- */
-
- Model.prototype.$__handleSave = function(options, callback) {
- const _this = this;
- let saveOptions = {};
-
- if ('safe' in options) {
- _handleSafe(options);
- }
- applyWriteConcern(this.schema, options);
- if ('w' in options) {
- saveOptions.w = options.w;
- }
- if ('j' in options) {
- saveOptions.j = options.j;
- }
- if ('wtimeout' in options) {
- saveOptions.wtimeout = options.wtimeout;
- }
- if ('checkKeys' in options) {
- saveOptions.checkKeys = options.checkKeys;
- }
-
- const session = this.$session();
- if (!saveOptions.hasOwnProperty('session')) {
- saveOptions.session = session;
- }
-
- if (Object.keys(saveOptions).length === 0) {
- saveOptions = null;
- }
-
- if (this.isNew) {
- // send entire doc
- const obj = this.toObject(internalToObjectOptions);
-
- if ((obj || {})._id === void 0) {
- // documents must have an _id else mongoose won't know
- // what to update later if more changes are made. the user
- // wouldn't know what _id was generated by mongodb either
- // nor would the ObjectId generated by mongodb necessarily
- // match the schema definition.
- setTimeout(function() {
- callback(new Error('document must have an _id before saving'));
- }, 0);
- return;
- }
-
- this.$__version(true, obj);
- this[modelCollectionSymbol].insertOne(obj, saveOptions, function(err, ret) {
- if (err) {
- _this.isNew = true;
- _this.emit('isNew', true);
- _this.constructor.emit('isNew', true);
-
- callback(err, null);
- return;
- }
-
- callback(null, ret);
- });
- this.$__reset();
- this.isNew = false;
- this.emit('isNew', false);
- this.constructor.emit('isNew', false);
- // Make it possible to retry the insert
- this.$__.inserting = true;
- } else {
- // Make sure we don't treat it as a new object on error,
- // since it already exists
- this.$__.inserting = false;
-
- const delta = this.$__delta();
-
- if (delta) {
- if (delta instanceof Error) {
- callback(delta);
- return;
- }
-
- const where = this.$__where(delta[0]);
- if (where instanceof Error) {
- callback(where);
- return;
- }
-
- _applyCustomWhere(this, where);
-
- this[modelCollectionSymbol].updateOne(where, delta[1], saveOptions, function(err, ret) {
- if (err) {
- callback(err);
- return;
- }
- ret.$where = where;
- callback(null, ret);
- });
- } else {
- this.$__reset();
- callback();
- return;
- }
-
- this.emit('isNew', false);
- this.constructor.emit('isNew', false);
- }
- };
-
- /*!
- * ignore
- */
-
- Model.prototype.$__save = function(options, callback) {
- this.$__handleSave(options, (error, result) => {
- const hooks = this.schema.s.hooks;
- if (error) {
- return hooks.execPost('save:error', this, [this], { error: error }, (error) => {
- callback(error, this);
- });
- }
-
- // store the modified paths before the document is reset
- const modifiedPaths = this.modifiedPaths();
-
- this.$__reset();
-
- let numAffected = 0;
- if (get(options, 'safe.w') !== 0 && get(options, 'w') !== 0) {
- // Skip checking if write succeeded if writeConcern is set to
- // unacknowledged writes, because otherwise `numAffected` will always be 0
- if (result) {
- if (Array.isArray(result)) {
- numAffected = result.length;
- } else if (result.result && result.result.n !== undefined) {
- numAffected = result.result.n;
- } else if (result.result && result.result.nModified !== undefined) {
- numAffected = result.result.nModified;
- } else {
- numAffected = result;
- }
- }
-
- // was this an update that required a version bump?
- if (this.$__.version && !this.$__.inserting) {
- const doIncrement = VERSION_INC === (VERSION_INC & this.$__.version);
- this.$__.version = undefined;
-
- const key = this.schema.options.versionKey;
- const version = this.$__getValue(key) || 0;
-
- if (numAffected <= 0) {
- // the update failed. pass an error back
- const err = this.$__.$versionError ||
- new VersionError(this, version, modifiedPaths);
- return callback(err);
- }
-
- // increment version if was successful
- if (doIncrement) {
- this.$__setValue(key, version + 1);
- }
- }
-
- if (result != null && numAffected <= 0) {
- error = new DocumentNotFoundError(result.$where,
- this.constructor.modelName, numAffected, result);
- return hooks.execPost('save:error', this, [this], { error: error }, (error) => {
- callback(error, this);
- });
- }
- }
- this.$__.saving = undefined;
- this.emit('save', this, numAffected);
- this.constructor.emit('save', this, numAffected);
- callback(null, this);
- });
- };
-
- /*!
- * ignore
- */
-
- function generateVersionError(doc, modifiedPaths) {
- const key = doc.schema.options.versionKey;
- if (!key) {
- return null;
- }
- const version = doc.$__getValue(key) || 0;
- return new VersionError(doc, version, modifiedPaths);
- }
-
- /**
- * Saves this document.
- *
- * ####Example:
- *
- * product.sold = Date.now();
- * product = await product.save();
- *
- * If save is successful, the returned promise will fulfill with the document
- * saved.
- *
- * ####Example:
- *
- * const newProduct = await product.save();
- * newProduct === product; // true
- *
- * @param {Object} [options] options optional options
- * @param {Session} [options.session=null] the [session](https://docs.mongodb.com/manual/reference/server-sessions/) associated with this save operation. If not specified, defaults to the [document's associated session](api.html#document_Document-$session).
- * @param {Object} [options.safe] (DEPRECATED) overrides [schema's safe option](http://mongoosejs.com//docs/guide.html#safe). Use the `w` option instead.
- * @param {Boolean} [options.validateBeforeSave] set to false to save without validating.
- * @param {Number|String} [options.w] set the [write concern](https://docs.mongodb.com/manual/reference/write-concern/#w-option). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern)
- * @param {Boolean} [options.j] set to true for MongoDB to wait until this `save()` has been [journaled before resolving the returned promise](https://docs.mongodb.com/manual/reference/write-concern/#j-option). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern)
- * @param {Number} [options.wtimeout] sets a [timeout for the write concern](https://docs.mongodb.com/manual/reference/write-concern/#wtimeout). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern).
- * @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/#Restrictions-on-Field-Names)
- * @param {Boolean} [options.timestamps=true] if `false` and [timestamps](./guide.html#timestamps) are enabled, skip timestamps for this `save()`.
- * @param {Function} [fn] optional callback
- * @throws {DocumentNotFoundError} if this [save updates an existing document](api.html#document_Document-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).
- * @return {Promise|undefined} Returns undefined if used with callback or a Promise otherwise.
- * @api public
- * @see middleware http://mongoosejs.com/docs/middleware.html
- */
-
- Model.prototype.save = function(options, fn) {
- let parallelSave;
-
- if (this.$__.saving) {
- parallelSave = new ParallelSaveError(this);
- } else {
- this.$__.saving = new ParallelSaveError(this);
- }
-
- if (typeof options === 'function') {
- fn = options;
- options = undefined;
- }
-
- if (fn) {
- fn = this.constructor.$wrapCallback(fn);
- }
-
- options = new SaveOptions(options);
- if (options.hasOwnProperty('session')) {
- this.$session(options.session);
- }
-
- this.$__.$versionError = generateVersionError(this, this.modifiedPaths());
-
- return utils.promiseOrCallback(fn, cb => {
- if (parallelSave) {
- this.$__handleReject(parallelSave);
- return cb(parallelSave);
- }
-
- this.$__.saveOptions = options;
-
- this.$__save(options, error => {
- this.$__.saving = undefined;
- delete this.$__.saveOptions;
-
- if (error) {
- this.$__handleReject(error);
- return cb(error);
- }
- cb(null, this);
- });
- }, this.constructor.events);
- };
-
- /*!
- * Determines whether versioning should be skipped for the given path
- *
- * @param {Document} self
- * @param {String} path
- * @return {Boolean} true if versioning should be skipped for the given path
- */
- function shouldSkipVersioning(self, path) {
- const skipVersioning = self.schema.options.skipVersioning;
- if (!skipVersioning) return false;
-
- // Remove any array indexes from the path
- path = path.replace(/\.\d+\./, '.');
-
- return skipVersioning[path];
- }
-
- /*!
- * Apply the operation to the delta (update) clause as
- * well as track versioning for our where clause.
- *
- * @param {Document} self
- * @param {Object} where
- * @param {Object} delta
- * @param {Object} data
- * @param {Mixed} val
- * @param {String} [operation]
- */
-
- function operand(self, where, delta, data, val, op) {
- // delta
- op || (op = '$set');
- if (!delta[op]) delta[op] = {};
- delta[op][data.path] = val;
-
- // disabled versioning?
- if (self.schema.options.versionKey === false) return;
-
- // path excluded from versioning?
- if (shouldSkipVersioning(self, data.path)) return;
-
- // already marked for versioning?
- if (VERSION_ALL === (VERSION_ALL & self.$__.version)) return;
-
- switch (op) {
- case '$set':
- case '$unset':
- case '$pop':
- case '$pull':
- case '$pullAll':
- case '$push':
- case '$addToSet':
- break;
- default:
- // nothing to do
- return;
- }
-
- // ensure updates sent with positional notation are
- // editing the correct array element.
- // only increment the version if an array position changes.
- // modifying elements of an array is ok if position does not change.
- if (op === '$push' || op === '$addToSet' || op === '$pullAll' || op === '$pull') {
- self.$__.version = VERSION_INC;
- } else if (/^\$p/.test(op)) {
- // potentially changing array positions
- self.increment();
- } else if (Array.isArray(val)) {
- // $set an array
- self.increment();
- } else if (/\.\d+\.|\.\d+$/.test(data.path)) {
- // now handling $set, $unset
- // subpath of array
- self.$__.version = VERSION_WHERE;
- }
- }
-
- /*!
- * Compiles an update and where clause for a `val` with _atomics.
- *
- * @param {Document} self
- * @param {Object} where
- * @param {Object} delta
- * @param {Object} data
- * @param {Array} value
- */
-
- function handleAtomics(self, where, delta, data, value) {
- if (delta.$set && delta.$set[data.path]) {
- // $set has precedence over other atomics
- return;
- }
-
- if (typeof value.$__getAtomics === 'function') {
- value.$__getAtomics().forEach(function(atomic) {
- const op = atomic[0];
- const val = atomic[1];
- operand(self, where, delta, data, val, op);
- });
- return;
- }
-
- // legacy support for plugins
-
- const atomics = value[arrayAtomicsSymbol];
- const ops = Object.keys(atomics);
- let i = ops.length;
- let val;
- let op;
-
- if (i === 0) {
- // $set
-
- if (utils.isMongooseObject(value)) {
- value = value.toObject({depopulate: 1, _isNested: true});
- } else if (value.valueOf) {
- value = value.valueOf();
- }
-
- return operand(self, where, delta, data, value);
- }
-
- function iter(mem) {
- return utils.isMongooseObject(mem)
- ? mem.toObject({depopulate: 1, _isNested: true})
- : mem;
- }
-
- while (i--) {
- op = ops[i];
- val = atomics[op];
-
- if (utils.isMongooseObject(val)) {
- val = val.toObject({depopulate: true, transform: false, _isNested: true});
- } else if (Array.isArray(val)) {
- val = val.map(iter);
- } else if (val.valueOf) {
- val = val.valueOf();
- }
-
- if (op === '$addToSet') {
- val = {$each: val};
- }
-
- operand(self, where, delta, data, val, op);
- }
- }
-
- /**
- * Produces a special query document of the modified properties used in updates.
- *
- * @api private
- * @method $__delta
- * @memberOf Model
- * @instance
- */
-
- Model.prototype.$__delta = function() {
- const dirty = this.$__dirty();
- if (!dirty.length && VERSION_ALL !== this.$__.version) {
- return;
- }
-
- const where = {};
- const delta = {};
- const len = dirty.length;
- const divergent = [];
- let d = 0;
-
- where._id = this._doc._id;
- // If `_id` is an object, need to depopulate, but also need to be careful
- // because `_id` can technically be null (see gh-6406)
- if (get(where, '_id.$__', null) != null) {
- where._id = where._id.toObject({ transform: false, depopulate: true });
- }
-
- for (; d < len; ++d) {
- const data = dirty[d];
- let value = data.value;
-
- const match = checkDivergentArray(this, data.path, value);
- if (match) {
- divergent.push(match);
- continue;
- }
-
- const pop = this.populated(data.path, true);
- if (!pop && this.$__.selected) {
- // If any array was selected using an $elemMatch projection, we alter the path and where clause
- // NOTE: MongoDB only supports projected $elemMatch on top level array.
- const pathSplit = data.path.split('.');
- const top = pathSplit[0];
- if (this.$__.selected[top] && this.$__.selected[top].$elemMatch) {
- // If the selected array entry was modified
- if (pathSplit.length > 1 && pathSplit[1] == 0 && typeof where[top] === 'undefined') {
- where[top] = this.$__.selected[top];
- pathSplit[1] = '$';
- data.path = pathSplit.join('.');
- }
- // if the selected array was modified in any other way throw an error
- else {
- divergent.push(data.path);
- continue;
- }
- }
- }
-
- if (divergent.length) continue;
-
- if (value === undefined) {
- operand(this, where, delta, data, 1, '$unset');
- } else if (value === null) {
- operand(this, where, delta, data, null);
- } else if (value.isMongooseArray && value.$path() && value[arrayAtomicsSymbol]) {
- // arrays and other custom types (support plugins etc)
- handleAtomics(this, where, delta, data, value);
- } else if (value[MongooseBuffer.pathSymbol] && Buffer.isBuffer(value)) {
- // MongooseBuffer
- value = value.toObject();
- operand(this, where, delta, data, value);
- } else {
- value = utils.clone(value, {
- depopulate: true,
- transform: false,
- virtuals: false,
- getters: false,
- _isNested: true
- });
- operand(this, where, delta, data, value);
- }
- }
-
- if (divergent.length) {
- return new DivergentArrayError(divergent);
- }
-
- if (this.$__.version) {
- this.$__version(where, delta);
- }
-
- return [where, delta];
- };
-
- /*!
- * Determine if array was populated with some form of filter and is now
- * being updated in a manner which could overwrite data unintentionally.
- *
- * @see https://github.com/Automattic/mongoose/issues/1334
- * @param {Document} doc
- * @param {String} path
- * @return {String|undefined}
- */
-
- function checkDivergentArray(doc, path, array) {
- // see if we populated this path
- const pop = doc.populated(path, true);
-
- if (!pop && doc.$__.selected) {
- // If any array was selected using an $elemMatch projection, we deny the update.
- // NOTE: MongoDB only supports projected $elemMatch on top level array.
- const top = path.split('.')[0];
- if (doc.$__.selected[top + '.$']) {
- return top;
- }
- }
-
- if (!(pop && array && array.isMongooseArray)) return;
-
- // If the array was populated using options that prevented all
- // documents from being returned (match, skip, limit) or they
- // deselected the _id field, $pop and $set of the array are
- // not safe operations. If _id was deselected, we do not know
- // how to remove elements. $pop will pop off the _id from the end
- // of the array in the db which is not guaranteed to be the
- // same as the last element we have here. $set of the entire array
- // would be similarily destructive as we never received all
- // elements of the array and potentially would overwrite data.
- const check = pop.options.match ||
- pop.options.options && utils.object.hasOwnProperty(pop.options.options, 'limit') || // 0 is not permitted
- pop.options.options && pop.options.options.skip || // 0 is permitted
- pop.options.select && // deselected _id?
- (pop.options.select._id === 0 ||
- /\s?-_id\s?/.test(pop.options.select));
-
- if (check) {
- const atomics = array[arrayAtomicsSymbol];
- if (Object.keys(atomics).length === 0 || atomics.$set || atomics.$pop) {
- return path;
- }
- }
- }
-
- /**
- * Appends versioning to the where and update clauses.
- *
- * @api private
- * @method $__version
- * @memberOf Model
- * @instance
- */
-
- Model.prototype.$__version = function(where, delta) {
- const key = this.schema.options.versionKey;
-
- if (where === true) {
- // this is an insert
- if (key) this.$__setValue(key, delta[key] = 0);
- return;
- }
-
- // updates
-
- // only apply versioning if our versionKey was selected. else
- // there is no way to select the correct version. we could fail
- // fast here and force them to include the versionKey but
- // thats a bit intrusive. can we do this automatically?
- if (!this.isSelected(key)) {
- return;
- }
-
- // $push $addToSet don't need the where clause set
- if (VERSION_WHERE === (VERSION_WHERE & this.$__.version)) {
- const value = this.$__getValue(key);
- if (value != null) where[key] = value;
- }
-
- if (VERSION_INC === (VERSION_INC & this.$__.version)) {
- if (get(delta.$set, key, null) != null) {
- // Version key is getting set, means we'll increment the doc's version
- // after a successful save, so we should set the incremented version so
- // future saves don't fail (gh-5779)
- ++delta.$set[key];
- } else {
- delta.$inc = delta.$inc || {};
- delta.$inc[key] = 1;
- }
- }
- };
-
- /**
- * Signal that we desire an increment of this documents version.
- *
- * ####Example:
- *
- * Model.findById(id, function (err, doc) {
- * doc.increment();
- * doc.save(function (err) { .. })
- * })
- *
- * @see versionKeys http://mongoosejs.com/docs/guide.html#versionKey
- * @api public
- */
-
- Model.prototype.increment = function increment() {
- this.$__.version = VERSION_ALL;
- return this;
- };
-
- /**
- * Returns a query object
- *
- * @api private
- * @method $__where
- * @memberOf Model
- * @instance
- */
-
- Model.prototype.$__where = function _where(where) {
- where || (where = {});
-
- if (!where._id) {
- where._id = this._doc._id;
- }
-
- if (this._doc._id === void 0) {
- return new Error('No _id found on document!');
- }
-
- return where;
- };
-
- /**
- * Removes this document from the db.
- *
- * ####Example:
- * product.remove(function (err, product) {
- * if (err) return handleError(err);
- * Product.findById(product._id, function (err, product) {
- * console.log(product) // null
- * })
- * })
- *
- *
- * As an extra measure of flow control, remove will return a Promise (bound to `fn` if passed) so it could be chained, or hooked to recieve errors
- *
- * ####Example:
- * product.remove().then(function (product) {
- * ...
- * }).catch(function (err) {
- * assert.ok(err)
- * })
- *
- * @param {function(err,product)} [fn] optional callback
- * @return {Promise} Promise
- * @api public
- */
-
- Model.prototype.remove = function remove(options, fn) {
- if (typeof options === 'function') {
- fn = options;
- options = undefined;
- }
-
- options = new RemoveOptions(options);
- if (options.hasOwnProperty('session')) {
- this.$session(options.session);
- }
-
- if (fn) {
- fn = this.constructor.$wrapCallback(fn);
- }
-
- return utils.promiseOrCallback(fn, cb => {
- this.$__remove(options, cb);
- }, this.constructor.events);
- };
-
- /**
- * Alias for remove
- */
-
- Model.prototype.delete = Model.prototype.remove;
-
- /**
- * Removes this document from the db. Equivalent to `.remove()`.
- *
- * ####Example:
- * product = await product.deleteOne();
- * await Product.findById(product._id); // null
- *
- * @param {function(err,product)} [fn] optional callback
- * @return {Promise} Promise
- * @api public
- */
-
- Model.prototype.deleteOne = function deleteOne(options, fn) {
- if (typeof options === 'function') {
- fn = options;
- options = undefined;
- }
-
- if (!options) {
- options = {};
- }
-
- if (fn) {
- fn = this.constructor.$wrapCallback(fn);
- }
-
- return utils.promiseOrCallback(fn, cb => {
- this.$__deleteOne(options, cb);
- }, this.constructor.events);
- };
-
- /*!
- * ignore
- */
-
- Model.prototype.$__remove = function $__remove(options, cb) {
- if (this.$__.isDeleted) {
- return immediate(() => cb(null, this));
- }
-
- const where = this.$__where();
- if (where instanceof Error) {
- return cb(where);
- }
-
- _applyCustomWhere(this, where);
-
- const session = this.$session();
- if (!options.hasOwnProperty('session')) {
- options.session = session;
- }
-
- this[modelCollectionSymbol].deleteOne(where, options, err => {
- if (!err) {
- this.$__.isDeleted = true;
- this.emit('remove', this);
- this.constructor.emit('remove', this);
- return cb(null, this);
- }
- this.$__.isDeleted = false;
- cb(err);
- });
- };
-
- /*!
- * ignore
- */
-
- Model.prototype.$__deleteOne = Model.prototype.$__remove;
-
- /**
- * Returns another Model instance.
- *
- * ####Example:
- *
- * var doc = new Tank;
- * doc.model('User').findById(id, callback);
- *
- * @param {String} name model name
- * @api public
- */
-
- Model.prototype.model = function model(name) {
- return this.db.model(name);
- };
-
- /**
- * Returns true if at least one document exists in the database that matches
- * the given `filter`, and false otherwise.
- *
- * Under the hood, `MyModel.exists({ answer: 42 })` is equivalent to
- * `MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean().then(doc => !!doc)`
- *
- * ####Example:
- * await Character.deleteMany({});
- * await Character.create({ name: 'Jean-Luc Picard' });
- *
- * await Character.exists({ name: /picard/i }); // true
- * await Character.exists({ name: /riker/i }); // false
- *
- * This function triggers the following middleware.
- *
- * - `findOne()`
- *
- * @param {Object} filter
- * @param {Function} [callback] callback
- * @return {Promise}
- */
-
- Model.exists = function exists(filter, callback) {
- const query = this.findOne(filter).select({ _id: 1 }).lean();
-
- if (typeof callback === 'function') {
- query.exec(function(err, doc) {
- if (err != null) {
- return callback(err);
- }
- callback(null, !!doc);
- });
- return;
- }
-
- return query.then(doc => !!doc);
- };
-
- /**
- * Adds a discriminator type.
- *
- * ####Example:
- *
- * function BaseSchema() {
- * Schema.apply(this, arguments);
- *
- * this.add({
- * name: String,
- * createdAt: Date
- * });
- * }
- * util.inherits(BaseSchema, Schema);
- *
- * var PersonSchema = new BaseSchema();
- * var BossSchema = new BaseSchema({ department: String });
- *
- * var Person = mongoose.model('Person', PersonSchema);
- * var Boss = Person.discriminator('Boss', BossSchema);
- * new Boss().__t; // "Boss". `__t` is the default `discriminatorKey`
- *
- * var employeeSchema = new Schema({ boss: ObjectId });
- * var Employee = Person.discriminator('Employee', employeeSchema, 'staff');
- * new Employee().__t; // "staff" because of 3rd argument above
- *
- * @param {String} name discriminator model name
- * @param {Schema} schema discriminator model schema
- * @param {String} value the string stored in the `discriminatorKey` property
- * @api public
- */
-
- Model.discriminator = function(name, schema, value) {
- let model;
- if (typeof name === 'function') {
- model = name;
- name = utils.getFunctionName(model);
- if (!(model.prototype instanceof Model)) {
- throw new Error('The provided class ' + name + ' must extend Model');
- }
- }
-
- schema = discriminator(this, name, schema, value, true);
- if (this.db.models[name]) {
- throw new OverwriteModelError(name);
- }
-
- schema.$isRootDiscriminator = true;
- schema.$globalPluginsApplied = true;
-
- model = this.db.model(model || name, schema, this.collection.name);
- this.discriminators[name] = model;
- const d = this.discriminators[name];
- d.prototype.__proto__ = this.prototype;
- Object.defineProperty(d, 'baseModelName', {
- value: this.modelName,
- configurable: true,
- writable: false
- });
-
- // apply methods and statics
- applyMethods(d, schema);
- applyStatics(d, schema);
-
- if (this[subclassedSymbol] != null) {
- for (const submodel of this[subclassedSymbol]) {
- submodel.discriminators = submodel.discriminators || {};
- submodel.discriminators[name] =
- model.__subclass(model.db, schema, submodel.collection.name);
- }
- }
-
- return d;
- };
-
- // Model (class) features
-
- /*!
- * Give the constructor the ability to emit events.
- */
-
- for (const i in EventEmitter.prototype) {
- Model[i] = EventEmitter.prototype[i];
- }
-
- /**
- * This function is responsible for building [indexes](https://docs.mongodb.com/manual/indexes/),
- * unless [`autoIndex`](http://mongoosejs.com/docs/guide.html#autoIndex) is turned off.
- *
- * Mongoose calls this function automatically when a model is created using
- * [`mongoose.model()`](/docs/api.html#mongoose_Mongoose-model) or
- * * [`connection.model()`](/docs/api.html#connection_Connection-model), so you
- * don't need to call it. This function is also idempotent, so you may call it
- * to get back a promise that will resolve when your indexes are finished
- * building as an alternative to [`MyModel.on('index')`](/docs/guide.html#indexes)
- *
- * ####Example:
- *
- * var eventSchema = new Schema({ thing: { type: 'string', unique: true }})
- * // This calls `Event.init()` implicitly, so you don't need to call
- * // `Event.init()` on your own.
- * var Event = mongoose.model('Event', eventSchema);
- *
- * Event.init().then(function(Event) {
- * // You can also use `Event.on('index')` if you prefer event emitters
- * // over promises.
- * console.log('Indexes are done building!');
- * });
- *
- * @api public
- * @param {Function} [callback]
- * @returns {Promise}
- */
-
- Model.init = function init(callback) {
- this.schema.emit('init', this);
-
- if (this.$init != null) {
- if (callback) {
- this.$init.then(() => callback(), err => callback(err));
- return null;
- }
- return this.$init;
- }
-
- const Promise = PromiseProvider.get();
- const autoIndex = this.schema.options.autoIndex == null ?
- this.db.config.autoIndex :
- this.schema.options.autoIndex;
- const autoCreate = this.schema.options.autoCreate == null ?
- this.db.config.autoCreate :
- this.schema.options.autoCreate;
-
- const _ensureIndexes = autoIndex ?
- cb => this.ensureIndexes({ _automatic: true }, cb) :
- cb => cb();
- const _createCollection = autoCreate ?
- cb => this.createCollection({}, cb) :
- cb => cb();
-
- this.$init = new Promise((resolve, reject) => {
- _createCollection(error => {
- if (error) {
- return reject(error);
- }
- _ensureIndexes(error => {
- if (error) {
- return reject(error);
- }
- resolve(this);
- });
- });
- });
-
- if (callback) {
- this.$init.then(() => callback(), err => callback(err));
- this.$caught = true;
- return null;
- } else {
- const _catch = this.$init.catch;
- const _this = this;
- this.$init.catch = function() {
- this.$caught = true;
- return _catch.apply(_this.$init, arguments);
- };
- }
-
- return this.$init;
- };
-
-
- /**
- * Create the collection for this model. By default, if no indexes are specified,
- * mongoose will not create the collection for the model until any documents are
- * created. Use this method to create the collection explicitly.
- *
- * Note 1: You may need to call this before starting a transaction
- * See https://docs.mongodb.com/manual/core/transactions/#transactions-and-operations
- *
- * Note 2: You don't have to call this if your schema contains index or unique field.
- * In that case, just use `Model.init()`
- *
- * ####Example:
- *
- * var userSchema = new Schema({ name: String })
- * var User = mongoose.model('User', userSchema);
- *
- * User.createCollection().then(function(collection) {
- * console.log('Collection is created!');
- * });
- *
- * @api public
- * @param {Object} [options] see [MongoDB driver docs](http://mongodb.github.io/node-mongodb-native/3.1/api/Db.html#createCollection)
- * @param {Function} [callback]
- * @returns {Promise}
- */
-
- Model.createCollection = function createCollection(options, callback) {
- if (typeof options === 'string') {
- throw new Error('You can\'t specify a new collection name in Model.createCollection.' +
- 'This is not like Connection.createCollection. Only options are accepted here.');
- } else if (typeof options === 'function') {
- callback = options;
- options = null;
- }
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- const schemaCollation = get(this, 'schema.options.collation', null);
- if (schemaCollation != null) {
- options = Object.assign({ collation: schemaCollation }, options);
- }
-
- return utils.promiseOrCallback(callback, cb => {
- this.db.createCollection(this.collection.collectionName, options, utils.tick((error) => {
- if (error) {
- return cb(error);
- }
- this.collection = this.db.collection(this.collection.collectionName, options);
- cb(null, this.collection);
- }));
- }, this.events);
- };
-
- /**
- * Makes the indexes in MongoDB match the indexes defined in this model's
- * schema. This function will drop any indexes that are not defined in
- * the model's schema except the `_id` index, and build any indexes that
- * are in your schema but not in MongoDB.
- *
- * See the [introductory blog post](http://thecodebarbarian.com/whats-new-in-mongoose-5-2-syncindexes)
- * for more information.
- *
- * ####Example:
- *
- * const schema = new Schema({ name: { type: String, unique: true } });
- * const Customer = mongoose.model('Customer', schema);
- * await Customer.createIndex({ age: 1 }); // Index is not in schema
- * // Will drop the 'age' index and create an index on `name`
- * await Customer.syncIndexes();
- *
- * @param {Object} [options] options to pass to `ensureIndexes()`
- * @param {Function} [callback] optional callback
- * @return {Promise|undefined} Returns `undefined` if callback is specified, returns a promise if no callback.
- * @api public
- */
-
- Model.syncIndexes = function syncIndexes(options, callback) {
- callback = this.$wrapCallback(callback);
-
- const dropNonSchemaIndexes = (cb) => {
- this.listIndexes((err, indexes) => {
- if (err != null) {
- return cb(err);
- }
-
- const schemaIndexes = this.schema.indexes();
- const toDrop = [];
-
- for (const index of indexes) {
- let found = false;
- // Never try to drop `_id` index, MongoDB server doesn't allow it
- if (index.key._id) {
- continue;
- }
-
- for (const schemaIndex of schemaIndexes) {
- const key = schemaIndex[0];
- const options = _decorateDiscriminatorIndexOptions(this,
- utils.clone(schemaIndex[1]));
-
- // If these options are different, need to rebuild the index
- const optionKeys = ['unique', 'partialFilterExpression', 'sparse', 'expireAfterSeconds'];
- const indexCopy = Object.assign({}, index);
- for (const key of optionKeys) {
- if (!(key in options) && !(key in indexCopy)) {
- continue;
- }
- indexCopy[key] = options[key];
- }
- if (utils.deepEqual(key, index.key) &&
- utils.deepEqual(index, indexCopy)) {
- found = true;
- break;
- }
- }
-
- if (!found) {
- toDrop.push(index.name);
- }
- }
-
- if (toDrop.length === 0) {
- return cb(null, []);
- }
-
- dropIndexes(toDrop, cb);
- });
- };
-
- const dropIndexes = (toDrop, cb) => {
- let remaining = toDrop.length;
- let error = false;
- toDrop.forEach(indexName => {
- this.collection.dropIndex(indexName, err => {
- if (err != null) {
- error = true;
- return cb(err);
- }
- if (!error) {
- --remaining || cb(null, toDrop);
- }
- });
- });
- };
-
- return utils.promiseOrCallback(callback, cb => {
- dropNonSchemaIndexes((err, dropped) => {
- if (err != null) {
- return cb(err);
- }
- this.createIndexes(options, err => {
- if (err != null) {
- return cb(err);
- }
- cb(null, dropped);
- });
- });
- }, this.events);
- };
-
- /**
- * Lists the indexes currently defined in MongoDB. This may or may not be
- * the same as the indexes defined in your schema depending on whether you
- * use the [`autoIndex` option](/docs/guide.html#autoIndex) and if you
- * build indexes manually.
- *
- * @param {Function} [cb] optional callback
- * @return {Promise|undefined} Returns `undefined` if callback is specified, returns a promise if no callback.
- * @api public
- */
-
- Model.listIndexes = function init(callback) {
- callback = this.$wrapCallback(callback);
-
- const _listIndexes = cb => {
- this.collection.listIndexes().toArray(cb);
- };
-
- return utils.promiseOrCallback(callback, cb => {
- // Buffering
- if (this.collection.buffer) {
- this.collection.addQueue(_listIndexes, [cb]);
- } else {
- _listIndexes(cb);
- }
- }, this.events);
- };
-
- /**
- * Sends `createIndex` commands to mongo for each index declared in the schema.
- * The `createIndex` commands are sent in series.
- *
- * ####Example:
- *
- * Event.ensureIndexes(function (err) {
- * if (err) return handleError(err);
- * });
- *
- * After completion, an `index` event is emitted on this `Model` passing an error if one occurred.
- *
- * ####Example:
- *
- * var eventSchema = new Schema({ thing: { type: 'string', unique: true }})
- * var Event = mongoose.model('Event', eventSchema);
- *
- * Event.on('index', function (err) {
- * if (err) console.error(err); // error occurred during index creation
- * })
- *
- * _NOTE: It is not recommended that you run this in production. Index creation may impact database performance depending on your load. Use with caution._
- *
- * @param {Object} [options] internal options
- * @param {Function} [cb] optional callback
- * @return {Promise}
- * @api public
- */
-
- Model.ensureIndexes = function ensureIndexes(options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = null;
- }
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return utils.promiseOrCallback(callback, cb => {
- _ensureIndexes(this, options || {}, error => {
- if (error) {
- return cb(error);
- }
- cb(null);
- });
- }, this.events);
- };
-
- /**
- * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#createIndex)
- * function.
- *
- * @param {Object} [options] internal options
- * @param {Function} [cb] optional callback
- * @return {Promise}
- * @api public
- */
-
- Model.createIndexes = function createIndexes(options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
- options = options || {};
- options.createIndex = true;
- return this.ensureIndexes(options, callback);
- };
-
- /*!
- * ignore
- */
-
- function _ensureIndexes(model, options, callback) {
- const indexes = model.schema.indexes();
-
- options = options || {};
-
- const done = function(err) {
- if (err && !model.$caught) {
- model.emit('error', err);
- }
- model.emit('index', err);
- callback && callback(err);
- };
-
- for (const index of indexes) {
- const keys = Object.keys(index[0]);
- if (keys.length === 1 && keys[0] === '_id' && index[0]._id !== 'hashed') {
- console.warn('mongoose: Cannot specify a custom index on `_id` for ' +
- 'model name "' + model.modelName + '", ' +
- 'MongoDB does not allow overwriting the default `_id` index. See ' +
- 'http://bit.ly/mongodb-id-index');
- }
- }
-
- if (!indexes.length) {
- immediate(function() {
- done();
- });
- return;
- }
- // Indexes are created one-by-one to support how MongoDB < 2.4 deals
- // with background indexes.
-
- const indexSingleDone = function(err, fields, options, name) {
- model.emit('index-single-done', err, fields, options, name);
- };
- const indexSingleStart = function(fields, options) {
- model.emit('index-single-start', fields, options);
- };
-
- const baseSchema = model.schema._baseSchema;
- const baseSchemaIndexes = baseSchema ? baseSchema.indexes() : [];
-
- const create = function() {
- if (options._automatic) {
- if (model.schema.options.autoIndex === false ||
- (model.schema.options.autoIndex == null && model.db.config.autoIndex === false)) {
- return done();
- }
- }
-
- const index = indexes.shift();
- if (!index) {
- return done();
- }
-
- if (baseSchemaIndexes.find(i => utils.deepEqual(i, index))) {
- return create();
- }
-
- const indexFields = utils.clone(index[0]);
- const indexOptions = utils.clone(index[1]);
-
- _decorateDiscriminatorIndexOptions(model, indexOptions);
- if ('safe' in options) {
- _handleSafe(options);
- }
- applyWriteConcern(model.schema, indexOptions);
-
- indexSingleStart(indexFields, options);
- let useCreateIndex = !!model.base.options.useCreateIndex;
- if ('useCreateIndex' in model.db.config) {
- useCreateIndex = !!model.db.config.useCreateIndex;
- }
- if ('createIndex' in options) {
- useCreateIndex = !!options.createIndex;
- }
-
- const methodName = useCreateIndex ? 'createIndex' : 'ensureIndex';
- model.collection[methodName](indexFields, indexOptions, utils.tick(function(err, name) {
- indexSingleDone(err, indexFields, indexOptions, name);
- if (err) {
- return done(err);
- }
- create();
- }));
- };
-
- immediate(function() {
- // If buffering is off, do this manually.
- if (options._automatic && !model.collection.collection) {
- model.collection.addQueue(create, []);
- } else {
- create();
- }
- });
- }
-
- function _decorateDiscriminatorIndexOptions(model, indexOptions) {
- // If the model is a discriminator and it has a unique index, add a
- // partialFilterExpression by default so the unique index will only apply
- // to that discriminator.
- if (model.baseModelName != null && indexOptions.unique &&
- !('partialFilterExpression' in indexOptions) &&
- !('sparse' in indexOptions)) {
-
- const value = (
- model.schema.discriminatorMapping &&
- model.schema.discriminatorMapping.value
- ) || model.modelName;
-
- indexOptions.partialFilterExpression = {
- [model.schema.options.discriminatorKey]: value
- };
- }
- return indexOptions;
- }
-
- const safeDeprecationWarning = 'Mongoose: the `safe` option for `save()` is ' +
- 'deprecated. Use the `w` option instead: http://bit.ly/mongoose-save';
-
- const _handleSafe = util.deprecate(function _handleSafe(options) {
- if (options.safe) {
- if (typeof options.safe === 'boolean') {
- options.w = options.safe;
- delete options.safe;
- }
- if (typeof options.safe === 'object') {
- options.w = options.safe.w;
- options.j = options.safe.j;
- options.wtimeout = options.safe.wtimeout;
- delete options.safe;
- }
- }
- }, safeDeprecationWarning);
-
- /**
- * Schema the model uses.
- *
- * @property schema
- * @receiver Model
- * @api public
- * @memberOf Model
- */
-
- Model.schema;
-
- /*!
- * Connection instance the model uses.
- *
- * @property db
- * @api public
- * @memberOf Model
- */
-
- Model.db;
-
- /*!
- * Collection the model uses.
- *
- * @property collection
- * @api public
- * @memberOf Model
- */
-
- Model.collection;
-
- /**
- * Base Mongoose instance the model uses.
- *
- * @property base
- * @api public
- * @memberOf Model
- */
-
- Model.base;
-
- /**
- * Registered discriminators for this model.
- *
- * @property discriminators
- * @api public
- * @memberOf Model
- */
-
- Model.discriminators;
-
- /**
- * Translate any aliases fields/conditions so the final query or document object is pure
- *
- * ####Example:
- *
- * Character
- * .find(Character.translateAliases({
- * '名': 'Eddard Stark' // Alias for 'name'
- * })
- * .exec(function(err, characters) {})
- *
- * ####Note:
- * Only translate arguments of object type anything else is returned raw
- *
- * @param {Object} raw fields/conditions that may contain aliased keys
- * @return {Object} the translated 'pure' fields/conditions
- */
- Model.translateAliases = function translateAliases(fields) {
- const translate = (key, value) => {
- let alias;
- const translated = [];
- const fieldKeys = key.split('.');
- let currentSchema = this.schema;
- for (const i in fieldKeys) {
- const name = fieldKeys[i];
- if (currentSchema && currentSchema.aliases[name]) {
- alias = currentSchema.aliases[name];
- // Alias found,
- translated.push(alias);
- } else {
- // Alias not found, so treat as un-aliased key
- translated.push(name);
- }
-
- // Check if aliased path is a schema
- if (currentSchema && currentSchema.paths[alias]) {
- currentSchema = currentSchema.paths[alias].schema;
- }
- else
- currentSchema = null;
- }
-
- const translatedKey = translated.join('.');
- if (fields instanceof Map)
- fields.set(translatedKey, value);
- else
- fields[translatedKey] = value;
-
- if (translatedKey !== key) {
- // We'll be using the translated key instead
- if (fields instanceof Map) {
- // Delete from map
- fields.delete(key);
- } else {
- // Delete from object
- delete fields[key]; // We'll be using the translated key instead
- }
- }
- return fields;
- };
-
- if (typeof fields === 'object') {
- // Fields is an object (query conditions or document fields)
- if (fields instanceof Map) {
- // A Map was supplied
- for (const field of new Map(fields)) {
- fields = translate(field[0], field[1]);
- }
- } else {
- // Infer a regular object was supplied
- for (const key of Object.keys(fields)) {
- fields = translate(key, fields[key]);
- if (key[0] === '$') {
- if (Array.isArray(fields[key])) {
- for (const i in fields[key]) {
- // Recursively translate nested queries
- fields[key][i] = this.translateAliases(fields[key][i]);
- }
- }
- }
- }
- }
-
- return fields;
- } else {
- // Don't know typeof fields
- return fields;
- }
- };
-
- /**
- * Removes all documents that match `conditions` from the collection.
- * To remove just the first document that matches `conditions`, set the `single`
- * option to true.
- *
- * ####Example:
- *
- * const res = await Character.remove({ name: 'Eddard Stark' });
- * res.deletedCount; // Number of documents removed
- *
- * ####Note:
- *
- * This method sends a remove command directly to MongoDB, no Mongoose documents
- * are involved. Because no Mongoose documents are involved, Mongoose does
- * not execute [document middleware](/docs/middleware.html#types-of-middleware).
- *
- * @param {Object} conditions
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.remove = function remove(conditions, callback) {
- if (typeof conditions === 'function') {
- callback = conditions;
- conditions = {};
- }
-
- // get the mongodb collection object
- const mq = new this.Query({}, {}, this, this.collection);
-
- callback = this.$wrapCallback(callback);
-
- return mq.remove(conditions, callback);
- };
-
- /**
- * Deletes the first document that matches `conditions` from the collection.
- * Behaves like `remove()`, but deletes at most one document regardless of the
- * `single` option.
- *
- * ####Example:
- *
- * Character.deleteOne({ name: 'Eddard Stark' }, function (err) {});
- *
- * ####Note:
- *
- * Like `Model.remove()`, this function does **not** trigger `pre('remove')` or `post('remove')` hooks.
- *
- * @param {Object} conditions
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.deleteOne = function deleteOne(conditions, options, callback) {
- if (typeof conditions === 'function') {
- callback = conditions;
- conditions = {};
- options = null;
- }
- else if (typeof options === 'function') {
- callback = options;
- options = null;
- }
-
- // get the mongodb collection object
- const mq = new this.Query(conditions, {}, this, this.collection);
- mq.setOptions(options);
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return mq.deleteOne(callback);
- };
-
- /**
- * Deletes all of the documents that match `conditions` from the collection.
- * Behaves like `remove()`, but deletes all documents that match `conditions`
- * regardless of the `single` option.
- *
- * ####Example:
- *
- * Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, function (err) {});
- *
- * ####Note:
- *
- * Like `Model.remove()`, this function does **not** trigger `pre('remove')` or `post('remove')` hooks.
- *
- * @param {Object} conditions
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.deleteMany = function deleteMany(conditions, options, callback) {
- if (typeof conditions === 'function') {
- callback = conditions;
- conditions = {};
- options = null;
- }
- else if (typeof options === 'function') {
- callback = options;
- options = null;
- }
-
- // get the mongodb collection object
- const mq = new this.Query(conditions, {}, this, this.collection);
- mq.setOptions(options);
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return mq.deleteMany(callback);
- };
-
- /**
- * Finds documents
- *
- * The `conditions` are cast to their respective SchemaTypes before the command is sent.
- *
- * ####Examples:
- *
- * // named john and at least 18
- * MyModel.find({ name: 'john', age: { $gte: 18 }});
- *
- * // executes, passing results to callback
- * MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});
- *
- * // executes, name LIKE john and only selecting the "name" and "friends" fields
- * MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })
- *
- * // passing options
- * MyModel.find({ name: /john/i }, null, { skip: 10 })
- *
- * // passing options and executes
- * MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {});
- *
- * // executing a query explicitly
- * var query = MyModel.find({ name: /john/i }, null, { skip: 10 })
- * query.exec(function (err, docs) {});
- *
- * // using the promise returned from executing a query
- * var query = MyModel.find({ name: /john/i }, null, { skip: 10 });
- * var promise = query.exec();
- * promise.addBack(function (err, docs) {});
- *
- * @param {Object} conditions
- * @param {Object|String} [projection] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Function} [callback]
- * @return {Query}
- * @see field selection #query_Query-select
- * @see promise #promise-js
- * @api public
- */
-
- Model.find = function find(conditions, projection, options, callback) {
- if (typeof conditions === 'function') {
- callback = conditions;
- conditions = {};
- projection = null;
- options = null;
- } else if (typeof projection === 'function') {
- callback = projection;
- projection = null;
- options = null;
- } else if (typeof options === 'function') {
- callback = options;
- options = null;
- }
-
- const mq = new this.Query({}, {}, this, this.collection);
- mq.select(projection);
-
- mq.setOptions(options);
- if (this.schema.discriminatorMapping &&
- this.schema.discriminatorMapping.isRoot &&
- mq.selectedInclusively()) {
- // Need to select discriminator key because original schema doesn't have it
- mq.select(this.schema.options.discriminatorKey);
- }
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return mq.find(conditions, callback);
- };
-
- /**
- * Finds a single document by its _id field. `findById(id)` is almost*
- * equivalent to `findOne({ _id: id })`. If you want to query by a document's
- * `_id`, use `findById()` instead of `findOne()`.
- *
- * The `id` is cast based on the Schema before sending the command.
- *
- * This function triggers the following middleware.
- *
- * - `findOne()`
- *
- * \* Except for how it treats `undefined`. If you use `findOne()`, you'll see
- * that `findOne(undefined)` and `findOne({ _id: undefined })` are equivalent
- * to `findOne({})` and return arbitrary documents. However, mongoose
- * translates `findById(undefined)` into `findOne({ _id: null })`.
- *
- * ####Example:
- *
- * // find adventure by id and execute
- * Adventure.findById(id, function (err, adventure) {});
- *
- * // same as above
- * Adventure.findById(id).exec(callback);
- *
- * // select only the adventures name and length
- * Adventure.findById(id, 'name length', function (err, adventure) {});
- *
- * // same as above
- * Adventure.findById(id, 'name length').exec(callback);
- *
- * // include all properties except for `length`
- * Adventure.findById(id, '-length').exec(function (err, adventure) {});
- *
- * // passing options (in this case return the raw js objects, not mongoose documents by passing `lean`
- * Adventure.findById(id, 'name', { lean: true }, function (err, doc) {});
- *
- * // same as above
- * Adventure.findById(id, 'name').lean().exec(function (err, doc) {});
- *
- * @param {Object|String|Number} id value of `_id` to query by
- * @param {Object|String} [projection] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Function} [callback]
- * @return {Query}
- * @see field selection #query_Query-select
- * @see lean queries /docs/tutorials/lean.html
- * @api public
- */
-
- Model.findById = function findById(id, projection, options, callback) {
- if (typeof id === 'undefined') {
- id = null;
- }
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return this.findOne({_id: id}, projection, options, callback);
- };
-
- /**
- * Finds one document.
- *
- * The `conditions` are cast to their respective SchemaTypes before the command is sent.
- *
- * *Note:* `conditions` is optional, and if `conditions` is null or undefined,
- * mongoose will send an empty `findOne` command to MongoDB, which will return
- * an arbitrary document. If you're querying by `_id`, use `findById()` instead.
- *
- * ####Example:
- *
- * // find one iphone adventures - iphone adventures??
- * Adventure.findOne({ type: 'iphone' }, function (err, adventure) {});
- *
- * // same as above
- * Adventure.findOne({ type: 'iphone' }).exec(function (err, adventure) {});
- *
- * // select only the adventures name
- * Adventure.findOne({ type: 'iphone' }, 'name', function (err, adventure) {});
- *
- * // same as above
- * Adventure.findOne({ type: 'iphone' }, 'name').exec(function (err, adventure) {});
- *
- * // specify options, in this case lean
- * Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }, callback);
- *
- * // same as above
- * Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }).exec(callback);
- *
- * // chaining findOne queries (same as above)
- * Adventure.findOne({ type: 'iphone' }).select('name').lean().exec(callback);
- *
- * @param {Object} [conditions]
- * @param {Object|String} [projection] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Function} [callback]
- * @return {Query}
- * @see field selection #query_Query-select
- * @see lean queries /docs/tutorials/lean.html
- * @api public
- */
-
- Model.findOne = function findOne(conditions, projection, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = null;
- } else if (typeof projection === 'function') {
- callback = projection;
- projection = null;
- options = null;
- } else if (typeof conditions === 'function') {
- callback = conditions;
- conditions = {};
- projection = null;
- options = null;
- }
-
- // get the mongodb collection object
- const mq = new this.Query({}, {}, this, this.collection);
- mq.select(projection);
-
- mq.setOptions(options);
- if (this.schema.discriminatorMapping &&
- this.schema.discriminatorMapping.isRoot &&
- mq.selectedInclusively()) {
- mq.select(this.schema.options.discriminatorKey);
- }
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return mq.findOne(conditions, callback);
- };
-
- /**
- * Estimates the number of documents in the MongoDB collection. Faster than
- * using `countDocuments()` for large collections because
- * `estimatedDocumentCount()` uses collection metadata rather than scanning
- * the entire collection.
- *
- * ####Example:
- *
- * const numAdventures = Adventure.estimatedDocumentCount();
- *
- * @param {Object} [options]
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.estimatedDocumentCount = function estimatedDocumentCount(options, callback) {
- // get the mongodb collection object
- const mq = new this.Query({}, {}, this, this.collection);
-
- callback = this.$wrapCallback(callback);
-
- return mq.estimatedDocumentCount(options, callback);
- };
-
- /**
- * Counts number of documents matching `filter` in a database collection.
- *
- * ####Example:
- *
- * Adventure.countDocuments({ type: 'jungle' }, function (err, count) {
- * console.log('there are %d jungle adventures', count);
- * });
- *
- * If you want to count all documents in a large collection,
- * use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model.estimatedDocumentCount)
- * instead. If you call `countDocuments({})`, MongoDB will always execute
- * a full collection scan and **not** use any indexes.
- *
- * The `countDocuments()` function is similar to `count()`, but there are a
- * [few operators that `countDocuments()` does not support](https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments).
- * Below are the operators that `count()` supports but `countDocuments()` does not,
- * and the suggested replacement:
- *
- * - `$where`: [`$expr`](https://docs.mongodb.com/manual/reference/operator/query/expr/)
- * - `$near`: [`$geoWithin`](https://docs.mongodb.com/manual/reference/operator/query/geoWithin/) with [`$center`](https://docs.mongodb.com/manual/reference/operator/query/center/#op._S_center)
- * - `$nearSphere`: [`$geoWithin`](https://docs.mongodb.com/manual/reference/operator/query/geoWithin/) with [`$centerSphere`](https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere)
- *
- * @param {Object} filter
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.countDocuments = function countDocuments(conditions, callback) {
- if (typeof conditions === 'function') {
- callback = conditions;
- conditions = {};
- }
-
- // get the mongodb collection object
- const mq = new this.Query({}, {}, this, this.collection);
-
- callback = this.$wrapCallback(callback);
-
- return mq.countDocuments(conditions, callback);
- };
-
- /**
- * Counts number of documents that match `filter` in a database collection.
- *
- * This method is deprecated. If you want to count the number of documents in
- * a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model.estimatedDocumentCount)
- * instead. Otherwise, use the [`countDocuments()`](/docs/api.html#model_Model.countDocuments) function instead.
- *
- * ####Example:
- *
- * Adventure.count({ type: 'jungle' }, function (err, count) {
- * if (err) ..
- * console.log('there are %d jungle adventures', count);
- * });
- *
- * @deprecated
- * @param {Object} filter
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.count = function count(conditions, callback) {
- if (typeof conditions === 'function') {
- callback = conditions;
- conditions = {};
- }
-
- // get the mongodb collection object
- const mq = new this.Query({}, {}, this, this.collection);
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return mq.count(conditions, callback);
- };
-
- /**
- * Creates a Query for a `distinct` operation.
- *
- * Passing a `callback` executes the query.
- *
- * ####Example
- *
- * Link.distinct('url', { clicks: {$gt: 100}}, function (err, result) {
- * if (err) return handleError(err);
- *
- * assert(Array.isArray(result));
- * console.log('unique urls with more than 100 clicks', result);
- * })
- *
- * var query = Link.distinct('url');
- * query.exec(callback);
- *
- * @param {String} field
- * @param {Object} [conditions] optional
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.distinct = function distinct(field, conditions, callback) {
- // get the mongodb collection object
- const mq = new this.Query({}, {}, this, this.collection);
-
- if (typeof conditions === 'function') {
- callback = conditions;
- conditions = {};
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return mq.distinct(field, conditions, callback);
- };
-
- /**
- * Creates a Query, applies the passed conditions, and returns the Query.
- *
- * For example, instead of writing:
- *
- * User.find({age: {$gte: 21, $lte: 65}}, callback);
- *
- * we can instead write:
- *
- * User.where('age').gte(21).lte(65).exec(callback);
- *
- * Since the Query class also supports `where` you can continue chaining
- *
- * User
- * .where('age').gte(21).lte(65)
- * .where('name', /^b/i)
- * ... etc
- *
- * @param {String} path
- * @param {Object} [val] optional value
- * @return {Query}
- * @api public
- */
-
- Model.where = function where(path, val) {
- void val; // eslint
- // get the mongodb collection object
- const mq = new this.Query({}, {}, this, this.collection).find({});
- return mq.where.apply(mq, arguments);
- };
-
- /**
- * Creates a `Query` and specifies a `$where` condition.
- *
- * 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.
- *
- * Blog.$where('this.username.indexOf("val") !== -1').exec(function (err, docs) {});
- *
- * @param {String|Function} argument is a javascript string or anonymous function
- * @method $where
- * @memberOf Model
- * @return {Query}
- * @see Query.$where #query_Query-%24where
- * @api public
- */
-
- Model.$where = function $where() {
- const mq = new this.Query({}, {}, this, this.collection).find({});
- return mq.$where.apply(mq, arguments);
- };
-
- /**
- * Issues a mongodb findAndModify update command.
- *
- * Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found document (if any) to the callback. The query executes if `callback` is passed else a Query object is returned.
- *
- * ####Options:
- *
- * - `new`: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
- * - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
- * - `fields`: {Object|String} - Field selection. Equivalent to `.select(fields).findOneAndUpdate()`
- * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
- * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
- * - `runValidators`: if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
- * - `setDefaultsOnInsert`: if this and `upsert` are true, mongoose will apply the [defaults](http://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on [MongoDB's `$setOnInsert` operator](https://docs.mongodb.org/v2.4/reference/operator/update/setOnInsert/).
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
- * - `strict`: overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict) for this update
- *
- * ####Examples:
- *
- * A.findOneAndUpdate(conditions, update, options, callback) // executes
- * A.findOneAndUpdate(conditions, update, options) // returns Query
- * A.findOneAndUpdate(conditions, update, callback) // executes
- * A.findOneAndUpdate(conditions, update) // returns Query
- * A.findOneAndUpdate() // returns Query
- *
- * ####Note:
- *
- * All top level update keys which are not `atomic` operation names are treated as set operations:
- *
- * ####Example:
- *
- * var query = { name: 'borne' };
- * Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)
- *
- * // is sent as
- * Model.findOneAndUpdate(query, { $set: { name: 'jason bourne' }}, options, callback)
- *
- * This helps prevent accidentally overwriting your document with `{ name: 'jason bourne' }`.
- *
- * ####Note:
- *
- * Values are cast to their appropriate types when using the findAndModify helpers.
- * However, the below are not executed by default.
- *
- * - defaults. Use the `setDefaultsOnInsert` option to override.
- *
- * `findAndModify` helpers support limited validation. You can
- * enable these by setting the `runValidators` options,
- * respectively.
- *
- * If you need full-fledged validation, use the traditional approach of first
- * retrieving the document.
- *
- * Model.findById(id, function (err, doc) {
- * if (err) ..
- * doc.name = 'jason bourne';
- * doc.save(callback);
- * });
- *
- * @param {Object} [conditions]
- * @param {Object} [update]
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](/docs/api.html#query_Query-lean) and [the Mongoose lean tutorial](/docs/tutorials/lean.html).
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Boolean} [options.omitUndefined=false] If true, delete any properties whose value is `undefined` when casting an update. In other words, if this is set, Mongoose will delete `baz` from the update in `Model.updateOne({}, { foo: 'bar', baz: undefined })` before sending the update to the server.
- * @param {Function} [callback]
- * @return {Query}
- * @see Tutorial /docs/tutorials/findoneandupdate.html
- * @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
- * @api public
- */
-
- Model.findOneAndUpdate = function(conditions, update, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = null;
- } else if (arguments.length === 1) {
- if (typeof conditions === 'function') {
- const msg = 'Model.findOneAndUpdate(): First argument must not be a function.\n\n'
- + ' ' + this.modelName + '.findOneAndUpdate(conditions, update, options, callback)\n'
- + ' ' + this.modelName + '.findOneAndUpdate(conditions, update, options)\n'
- + ' ' + this.modelName + '.findOneAndUpdate(conditions, update)\n'
- + ' ' + this.modelName + '.findOneAndUpdate(update)\n'
- + ' ' + this.modelName + '.findOneAndUpdate()\n';
- throw new TypeError(msg);
- }
- update = conditions;
- conditions = undefined;
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- let fields;
- if (options) {
- fields = options.fields || options.projection;
- }
-
- update = utils.clone(update, {
- depopulate: true,
- _isNested: true
- });
-
- _decorateUpdateWithVersionKey(update, options, this.schema.options.versionKey);
-
- const mq = new this.Query({}, {}, this, this.collection);
- mq.select(fields);
-
- return mq.findOneAndUpdate(conditions, update, options, callback);
- };
-
- /*!
- * Decorate the update with a version key, if necessary
- */
-
- function _decorateUpdateWithVersionKey(update, options, versionKey) {
- if (!versionKey || !get(options, 'upsert', false)) {
- return;
- }
-
- const updatedPaths = modifiedPaths(update);
- if (!updatedPaths[versionKey]) {
- if (options.overwrite) {
- update[versionKey] = 0;
- } else {
- if (!update.$setOnInsert) {
- update.$setOnInsert = {};
- }
- update.$setOnInsert[versionKey] = 0;
- }
- }
- }
-
- /**
- * Issues a mongodb findAndModify update command by a document's _id field.
- * `findByIdAndUpdate(id, ...)` is equivalent to `findOneAndUpdate({ _id: id }, ...)`.
- *
- * Finds a matching document, updates it according to the `update` arg,
- * passing any `options`, and returns the found document (if any) to the
- * callback. The query executes if `callback` is passed.
- *
- * This function triggers the following middleware.
- *
- * - `findOneAndUpdate()`
- *
- * ####Options:
- *
- * - `new`: bool - true to return the modified document rather than the original. defaults to false
- * - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
- * - `runValidators`: if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
- * - `setDefaultsOnInsert`: if this and `upsert` are true, mongoose will apply the [defaults](http://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on [MongoDB's `$setOnInsert` operator](https://docs.mongodb.org/v2.4/reference/operator/update/setOnInsert/).
- * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
- * - `select`: sets the document fields to return
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
- * - `strict`: overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict) for this update
- *
- * ####Examples:
- *
- * A.findByIdAndUpdate(id, update, options, callback) // executes
- * A.findByIdAndUpdate(id, update, options) // returns Query
- * A.findByIdAndUpdate(id, update, callback) // executes
- * A.findByIdAndUpdate(id, update) // returns Query
- * A.findByIdAndUpdate() // returns Query
- *
- * ####Note:
- *
- * All top level update keys which are not `atomic` operation names are treated as set operations:
- *
- * ####Example:
- *
- * Model.findByIdAndUpdate(id, { name: 'jason bourne' }, options, callback)
- *
- * // is sent as
- * Model.findByIdAndUpdate(id, { $set: { name: 'jason bourne' }}, options, callback)
- *
- * This helps prevent accidentally overwriting your document with `{ name: 'jason bourne' }`.
- *
- * ####Note:
- *
- * Values are cast to their appropriate types when using the findAndModify helpers.
- * However, the below are not executed by default.
- *
- * - defaults. Use the `setDefaultsOnInsert` option to override.
- *
- * `findAndModify` helpers support limited validation. You can
- * enable these by setting the `runValidators` options,
- * respectively.
- *
- * If you need full-fledged validation, use the traditional approach of first
- * retrieving the document.
- *
- * Model.findById(id, function (err, doc) {
- * if (err) ..
- * doc.name = 'jason bourne';
- * doc.save(callback);
- * });
- *
- * @param {Object|Number|String} id value of `_id` to query by
- * @param {Object} [update]
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](/docs/api.html#query_Query-lean) and the [Mongoose lean tutorial](/docs/tutorials/lean.html).
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Boolean} [options.omitUndefined=false] If true, delete any properties whose value is `undefined` when casting an update. In other words, if this is set, Mongoose will delete `baz` from the update in `Model.updateOne({}, { foo: 'bar', baz: undefined })` before sending the update to the server.
- * @param {Function} [callback]
- * @return {Query}
- * @see Model.findOneAndUpdate #model_Model.findOneAndUpdate
- * @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
- * @api public
- */
-
- Model.findByIdAndUpdate = function(id, update, options, callback) {
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
- if (arguments.length === 1) {
- if (typeof id === 'function') {
- const msg = 'Model.findByIdAndUpdate(): First argument must not be a function.\n\n'
- + ' ' + this.modelName + '.findByIdAndUpdate(id, callback)\n'
- + ' ' + this.modelName + '.findByIdAndUpdate(id)\n'
- + ' ' + this.modelName + '.findByIdAndUpdate()\n';
- throw new TypeError(msg);
- }
- return this.findOneAndUpdate({_id: id}, undefined);
- }
-
- // if a model is passed in instead of an id
- if (id instanceof Document) {
- id = id._id;
- }
-
- return this.findOneAndUpdate.call(this, {_id: id}, update, options, callback);
- };
-
- /**
- * Issue a MongoDB `findOneAndDelete()` command.
- *
- * Finds a matching document, removes it, and passes the found document
- * (if any) to the callback.
- *
- * Executes the query if `callback` is passed.
- *
- * This function triggers the following middleware.
- *
- * - `findOneAndDelete()`
- *
- * This function differs slightly from `Model.findOneAndRemove()` in that
- * `findOneAndRemove()` becomes a [MongoDB `findAndModify()` command](https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/),
- * as opposed to a `findOneAndDelete()` command. For most mongoose use cases,
- * this distinction is purely pedantic. You should use `findOneAndDelete()`
- * unless you have a good reason not to.
- *
- * ####Options:
- *
- * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
- * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
- * - `select`: sets the document fields to return
- * - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
- * - `strict`: overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict) for this update
- *
- * ####Examples:
- *
- * A.findOneAndDelete(conditions, options, callback) // executes
- * A.findOneAndDelete(conditions, options) // return Query
- * A.findOneAndDelete(conditions, callback) // executes
- * A.findOneAndDelete(conditions) // returns Query
- * A.findOneAndDelete() // returns Query
- *
- * Values are cast to their appropriate types when using the findAndModify helpers.
- * However, the below are not executed by default.
- *
- * - defaults. Use the `setDefaultsOnInsert` option to override.
- *
- * `findAndModify` helpers support limited validation. You can
- * enable these by setting the `runValidators` options,
- * respectively.
- *
- * If you need full-fledged validation, use the traditional approach of first
- * retrieving the document.
- *
- * Model.findById(id, function (err, doc) {
- * if (err) ..
- * doc.name = 'jason bourne';
- * doc.save(callback);
- * });
- *
- * @param {Object} conditions
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.findOneAndDelete = function(conditions, options, callback) {
- if (arguments.length === 1 && typeof conditions === 'function') {
- const msg = 'Model.findOneAndDelete(): First argument must not be a function.\n\n'
- + ' ' + this.modelName + '.findOneAndDelete(conditions, callback)\n'
- + ' ' + this.modelName + '.findOneAndDelete(conditions)\n'
- + ' ' + this.modelName + '.findOneAndDelete()\n';
- throw new TypeError(msg);
- }
-
- if (typeof options === 'function') {
- callback = options;
- options = undefined;
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- let fields;
- if (options) {
- fields = options.select;
- options.select = undefined;
- }
-
- const mq = new this.Query({}, {}, this, this.collection);
- mq.select(fields);
-
- return mq.findOneAndDelete(conditions, options, callback);
- };
-
- /**
- * Issue a MongoDB `findOneAndDelete()` command by a document's _id field.
- * In other words, `findByIdAndDelete(id)` is a shorthand for
- * `findOneAndDelete({ _id: id })`.
- *
- * This function triggers the following middleware.
- *
- * - `findOneAndDelete()`
- *
- * @param {Object|Number|String} id value of `_id` to query by
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Function} [callback]
- * @return {Query}
- * @see Model.findOneAndRemove #model_Model.findOneAndRemove
- * @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
- */
-
- Model.findByIdAndDelete = function(id, options, callback) {
- if (arguments.length === 1 && typeof id === 'function') {
- const msg = 'Model.findByIdAndDelete(): First argument must not be a function.\n\n'
- + ' ' + this.modelName + '.findByIdAndDelete(id, callback)\n'
- + ' ' + this.modelName + '.findByIdAndDelete(id)\n'
- + ' ' + this.modelName + '.findByIdAndDelete()\n';
- throw new TypeError(msg);
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return this.findOneAndDelete({_id: id}, options, callback);
- };
-
- /**
- * Issue a MongoDB `findOneAndReplace()` command.
- *
- * Finds a matching document, replaces it with the provided doc, and passes the
- * returned doc to the callback.
- *
- * Executes the query if `callback` is passed.
- *
- * This function triggers the following query middleware.
- *
- * - `findOneAndReplace()`
- *
- * ####Options:
- *
- * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
- * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
- * - `select`: sets the document fields to return
- * - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
- * - `strict`: overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict) for this update
- *
- * ####Examples:
- *
- * A.findOneAndReplace(conditions, options, callback) // executes
- * A.findOneAndReplace(conditions, options) // return Query
- * A.findOneAndReplace(conditions, callback) // executes
- * A.findOneAndReplace(conditions) // returns Query
- * A.findOneAndReplace() // returns Query
- *
- * Values are cast to their appropriate types when using the findAndModify helpers.
- * However, the below are not executed by default.
- *
- * - defaults. Use the `setDefaultsOnInsert` option to override.
- *
- * @param {Object} filter Replace the first document that matches this filter
- * @param {Object} [replacement] Replace with this document
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](http://mongoosejs.com/docs/api.html#query_Query-lean).
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Boolean} [options.omitUndefined=false] If true, delete any properties whose value is `undefined` when casting an update. In other words, if this is set, Mongoose will delete `baz` from the update in `Model.updateOne({}, { foo: 'bar', baz: undefined })` before sending the update to the server.
- * @param {Function} [callback]
- * @return {Query}
- * @api public
- */
-
- Model.findOneAndReplace = function(filter, replacement, options, callback) {
- if (arguments.length === 1 && typeof filter === 'function') {
- const msg = 'Model.findOneAndReplace(): First argument must not be a function.\n\n'
- + ' ' + this.modelName + '.findOneAndReplace(conditions, callback)\n'
- + ' ' + this.modelName + '.findOneAndReplace(conditions)\n'
- + ' ' + this.modelName + '.findOneAndReplace()\n';
- throw new TypeError(msg);
- }
-
- if (arguments.length === 3 && typeof options === 'function') {
- callback = options;
- options = replacement;
- replacement = void 0;
- }
- if (arguments.length === 2 && typeof replacement === 'function') {
- callback = replacement;
- replacement = void 0;
- options = void 0;
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- let fields;
- if (options) {
- fields = options.select;
- options.select = undefined;
- }
-
- const mq = new this.Query({}, {}, this, this.collection);
- mq.select(fields);
-
- return mq.findOneAndReplace(filter, replacement, options, callback);
- };
-
- /**
- * Issue a mongodb findAndModify remove command.
- *
- * Finds a matching document, removes it, passing the found document (if any) to the callback.
- *
- * Executes the query if `callback` is passed.
- *
- * This function triggers the following middleware.
- *
- * - `findOneAndRemove()`
- *
- * ####Options:
- *
- * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
- * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
- * - `select`: sets the document fields to return
- * - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
- * - `strict`: overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict) for this update
- *
- * ####Examples:
- *
- * A.findOneAndRemove(conditions, options, callback) // executes
- * A.findOneAndRemove(conditions, options) // return Query
- * A.findOneAndRemove(conditions, callback) // executes
- * A.findOneAndRemove(conditions) // returns Query
- * A.findOneAndRemove() // returns Query
- *
- * Values are cast to their appropriate types when using the findAndModify helpers.
- * However, the below are not executed by default.
- *
- * - defaults. Use the `setDefaultsOnInsert` option to override.
- *
- * `findAndModify` helpers support limited validation. You can
- * enable these by setting the `runValidators` options,
- * respectively.
- *
- * If you need full-fledged validation, use the traditional approach of first
- * retrieving the document.
- *
- * Model.findById(id, function (err, doc) {
- * if (err) ..
- * doc.name = 'jason bourne';
- * doc.save(callback);
- * });
- *
- * @param {Object} conditions
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Function} [callback]
- * @return {Query}
- * @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
- * @api public
- */
-
- Model.findOneAndRemove = function(conditions, options, callback) {
- if (arguments.length === 1 && typeof conditions === 'function') {
- const msg = 'Model.findOneAndRemove(): First argument must not be a function.\n\n'
- + ' ' + this.modelName + '.findOneAndRemove(conditions, callback)\n'
- + ' ' + this.modelName + '.findOneAndRemove(conditions)\n'
- + ' ' + this.modelName + '.findOneAndRemove()\n';
- throw new TypeError(msg);
- }
-
- if (typeof options === 'function') {
- callback = options;
- options = undefined;
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- let fields;
- if (options) {
- fields = options.select;
- options.select = undefined;
- }
-
- const mq = new this.Query({}, {}, this, this.collection);
- mq.select(fields);
-
- return mq.findOneAndRemove(conditions, options, callback);
- };
-
- /**
- * Issue a mongodb findAndModify remove command by a document's _id field. `findByIdAndRemove(id, ...)` is equivalent to `findOneAndRemove({ _id: id }, ...)`.
- *
- * Finds a matching document, removes it, passing the found document (if any) to the callback.
- *
- * Executes the query if `callback` is passed.
- *
- * This function triggers the following middleware.
- *
- * - `findOneAndRemove()`
- *
- * ####Options:
- *
- * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
- * - `select`: sets the document fields to return
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
- * - `strict`: overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict) for this update
- *
- * ####Examples:
- *
- * A.findByIdAndRemove(id, options, callback) // executes
- * A.findByIdAndRemove(id, options) // return Query
- * A.findByIdAndRemove(id, callback) // executes
- * A.findByIdAndRemove(id) // returns Query
- * A.findByIdAndRemove() // returns Query
- *
- * @param {Object|Number|String} id value of `_id` to query by
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Function} [callback]
- * @return {Query}
- * @see Model.findOneAndRemove #model_Model.findOneAndRemove
- * @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
- */
-
- Model.findByIdAndRemove = function(id, options, callback) {
- if (arguments.length === 1 && typeof id === 'function') {
- const msg = 'Model.findByIdAndRemove(): First argument must not be a function.\n\n'
- + ' ' + this.modelName + '.findByIdAndRemove(id, callback)\n'
- + ' ' + this.modelName + '.findByIdAndRemove(id)\n'
- + ' ' + this.modelName + '.findByIdAndRemove()\n';
- throw new TypeError(msg);
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return this.findOneAndRemove({_id: id}, options, callback);
- };
-
- /**
- * Shortcut for saving one or more documents to the database.
- * `MyModel.create(docs)` does `new MyModel(doc).save()` for every doc in
- * docs.
- *
- * This function triggers the following middleware.
- *
- * - `save()`
- *
- * ####Example:
- *
- * // pass a spread of docs and a callback
- * Candy.create({ type: 'jelly bean' }, { type: 'snickers' }, function (err, jellybean, snickers) {
- * if (err) // ...
- * });
- *
- * // pass an array of docs
- * var array = [{ type: 'jelly bean' }, { type: 'snickers' }];
- * Candy.create(array, function (err, candies) {
- * if (err) // ...
- *
- * var jellybean = candies[0];
- * var snickers = candies[1];
- * // ...
- * });
- *
- * // callback is optional; use the returned promise if you like:
- * var promise = Candy.create({ type: 'jawbreaker' });
- * promise.then(function (jawbreaker) {
- * // ...
- * })
- *
- * @param {Array|Object} docs Documents to insert, as a spread or array
- * @param {Object} [options] Options passed down to `save()`. To specify `options`, `docs` **must** be an array, not a spread.
- * @param {Function} [callback] callback
- * @return {Promise}
- * @api public
- */
-
- Model.create = function create(doc, options, callback) {
- let args;
- let cb;
- const discriminatorKey = this.schema.options.discriminatorKey;
-
- if (Array.isArray(doc)) {
- args = doc;
- cb = typeof options === 'function' ? options : callback;
- options = options != null && typeof options === 'object' ? options : {};
- } else {
- const last = arguments[arguments.length - 1];
- options = {};
- // Handle falsy callbacks re: #5061
- if (typeof last === 'function' || !last) {
- cb = last;
- args = utils.args(arguments, 0, arguments.length - 1);
- } else {
- args = utils.args(arguments);
- }
-
- if (args.length === 2 &&
- args[0] != null &&
- args[1] != null &&
- args[0].session == null &&
- last.session != null &&
- last.session.constructor.name === 'ClientSession' &&
- !this.schema.path('session')) {
- // Probably means the user is running into the common mistake of trying
- // to use a spread to specify options, see gh-7535
- console.warn('WARNING: to pass a `session` to `Model.create()` in ' +
- 'Mongoose, you **must** pass an array as the first argument. See: ' +
- 'https://mongoosejs.com/docs/api.html#model_Model.create');
- }
- }
-
- if (cb) {
- cb = this.$wrapCallback(cb);
- }
-
- return utils.promiseOrCallback(cb, cb => {
- if (args.length === 0) {
- return cb(null);
- }
-
- const toExecute = [];
- let firstError;
- args.forEach(doc => {
- toExecute.push(callback => {
- const Model = this.discriminators && doc[discriminatorKey] != null ?
- this.discriminators[doc[discriminatorKey]] || getDiscriminatorByValue(this, doc[discriminatorKey]) :
- this;
- if (Model == null) {
- throw new Error(`Discriminator "${doc[discriminatorKey]}" not ` +
- `found for model "${this.modelName}"`);
- }
- let toSave = doc;
- const callbackWrapper = (error, doc) => {
- if (error) {
- if (!firstError) {
- firstError = error;
- }
- return callback(null, { error: error });
- }
- callback(null, { doc: doc });
- };
-
- if (!(toSave instanceof Model)) {
- try {
- toSave = new Model(toSave);
- } catch (error) {
- return callbackWrapper(error);
- }
- }
-
- toSave.save(options, callbackWrapper);
- });
- });
-
- parallel(toExecute, (error, res) => {
- const savedDocs = [];
- const len = res.length;
- for (let i = 0; i < len; ++i) {
- if (res[i].doc) {
- savedDocs.push(res[i].doc);
- }
- }
-
- if (firstError) {
- return cb(firstError, savedDocs);
- }
-
- if (doc instanceof Array) {
- cb(null, savedDocs);
- } else {
- cb.apply(this, [null].concat(savedDocs));
- }
- });
- }, this.events);
- };
-
- /**
- * _Requires a replica set running MongoDB >= 3.6.0._ Watches the
- * underlying collection for changes using
- * [MongoDB change streams](https://docs.mongodb.com/manual/changeStreams/).
- *
- * This function does **not** trigger any middleware. In particular, it
- * does **not** trigger aggregate middleware.
- *
- * The ChangeStream object is an event emitter that emits the following events:
- *
- * - 'change': A change occurred, see below example
- * - '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.
- * - 'end': Emitted if the underlying stream is closed
- * - 'close': Emitted if the underlying stream is closed
- *
- * ####Example:
- *
- * const doc = await Person.create({ name: 'Ned Stark' });
- * const changeStream = Person.watch().on('change', change => console.log(change));
- * // Will print from the above `console.log()`:
- * // { _id: { _data: ... },
- * // operationType: 'delete',
- * // ns: { db: 'mydb', coll: 'Person' },
- * // documentKey: { _id: 5a51b125c5500f5aa094c7bd } }
- * await doc.remove();
- *
- * @param {Array} [pipeline]
- * @param {Object} [options] see the [mongodb driver options](http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#watch)
- * @return {ChangeStream} mongoose-specific change stream wrapper, inherits from EventEmitter
- * @api public
- */
-
- Model.watch = function(pipeline, options) {
- return new ChangeStream(this, pipeline, options);
- };
-
- /**
- * _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://docs.mongodb.com/manual/release-notes/3.6/#client-sessions)
- * for benefits like causal consistency, [retryable writes](https://docs.mongodb.com/manual/core/retryable-writes/),
- * and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
- *
- * Calling `MyModel.startSession()` is equivalent to calling `MyModel.db.startSession()`.
- *
- * This function does not trigger any middleware.
- *
- * ####Example:
- *
- * const session = await Person.startSession();
- * let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });
- * await doc.remove();
- * // `doc` will always be null, even if reading from a replica set
- * // secondary. Without causal consistency, it is possible to
- * // get a doc back from the below query if the query reads from a
- * // secondary that is experiencing replication lag.
- * doc = await Person.findOne({ name: 'Ned Stark' }, null, { session, readPreference: 'secondary' });
- *
- * @param {Object} [options] see the [mongodb driver options](http://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#startSession)
- * @param {Boolean} [options.causalConsistency=true] set to false to disable causal consistency
- * @param {Function} [callback]
- * @return {Promise<ClientSession>} promise that resolves to a MongoDB driver `ClientSession`
- * @api public
- */
-
- Model.startSession = function() {
- return this.db.startSession.apply(this.db, arguments);
- };
-
- /**
- * Shortcut for validating an array of documents and inserting them into
- * MongoDB if they're all valid. This function is faster than `.create()`
- * because it only sends one operation to the server, rather than one for each
- * document.
- *
- * Mongoose always validates each document **before** sending `insertMany`
- * to MongoDB. So if one document has a validation error, no documents will
- * be saved, unless you set
- * [the `ordered` option to false](https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/#error-handling).
- *
- * This function does **not** trigger save middleware.
- *
- * This function triggers the following middleware.
- *
- * - `insertMany()`
- *
- * ####Example:
- *
- * var arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
- * Movies.insertMany(arr, function(error, docs) {});
- *
- * @param {Array|Object|*} doc(s)
- * @param {Object} [options] see the [mongodb driver options](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#insertMany)
- * @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()`.
- * @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](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~insertWriteOpCallback) with a `mongoose` property that contains `validationErrors` if this is an unordered `insertMany`.
- * @param {Function} [callback] callback
- * @return {Promise}
- * @api public
- */
-
- Model.insertMany = function(arr, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = null;
- }
- return utils.promiseOrCallback(callback, cb => {
- this.$__insertMany(arr, options, cb);
- }, this.events);
- };
-
- /*!
- * ignore
- */
-
- Model.$__insertMany = function(arr, options, callback) {
- const _this = this;
- if (typeof options === 'function') {
- callback = options;
- options = null;
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
- callback = callback || utils.noop;
- options = options || {};
- const limit = get(options, 'limit', 1000);
- const rawResult = get(options, 'rawResult', false);
- const ordered = get(options, 'ordered', true);
-
- if (!Array.isArray(arr)) {
- arr = [arr];
- }
-
- const toExecute = [];
- const validationErrors = [];
- arr.forEach(function(doc) {
- toExecute.push(function(callback) {
- if (!(doc instanceof _this)) {
- doc = new _this(doc);
- }
- if (options.session != null) {
- doc.$session(options.session);
- }
- doc.validate({ __noPromise: true }, function(error) {
- if (error) {
- // Option `ordered` signals that insert should be continued after reaching
- // a failing insert. Therefore we delegate "null", meaning the validation
- // failed. It's up to the next function to filter out all failed models
- if (ordered === false) {
- validationErrors.push(error);
- return callback(null, null);
- }
- return callback(error);
- }
- callback(null, doc);
- });
- });
- });
-
- parallelLimit(toExecute, limit, function(error, docs) {
- if (error) {
- callback(error, null);
- return;
- }
- // We filter all failed pre-validations by removing nulls
- const docAttributes = docs.filter(function(doc) {
- return doc != null;
- });
- // Quickly escape while there aren't any valid docAttributes
- if (docAttributes.length < 1) {
- callback(null, []);
- return;
- }
- const docObjects = docAttributes.map(function(doc) {
- if (doc.schema.options.versionKey) {
- doc[doc.schema.options.versionKey] = 0;
- }
- if (doc.initializeTimestamps) {
- return doc.initializeTimestamps().toObject(internalToObjectOptions);
- }
- return doc.toObject(internalToObjectOptions);
- });
-
- _this.collection.insertMany(docObjects, options, function(error, res) {
- if (error) {
- callback(error, null);
- return;
- }
- for (let i = 0; i < docAttributes.length; ++i) {
- docAttributes[i].$__reset();
- docAttributes[i].isNew = false;
- docAttributes[i].emit('isNew', false);
- docAttributes[i].constructor.emit('isNew', false);
- }
- if (rawResult) {
- if (ordered === false) {
- // Decorate with mongoose validation errors in case of unordered,
- // because then still do `insertMany()`
- res.mongoose = {
- validationErrors: validationErrors
- };
- }
- return callback(null, res);
- }
- callback(null, docAttributes);
- });
- });
- };
-
- /**
- * Sends multiple `insertOne`, `updateOne`, `updateMany`, `replaceOne`,
- * `deleteOne`, and/or `deleteMany` operations to the MongoDB server in one
- * command. This is faster than sending multiple independent operations (like)
- * if you use `create()`) because with `bulkWrite()` there is only one round
- * trip to MongoDB.
- *
- * Mongoose will perform casting on all operations you provide.
- *
- * This function does **not** trigger any middleware, not `save()` nor `update()`.
- * If you need to trigger
- * `save()` middleware for every document use [`create()`](http://mongoosejs.com/docs/api.html#model_Model.create) instead.
- *
- * ####Example:
- *
- * Character.bulkWrite([
- * {
- * insertOne: {
- * document: {
- * name: 'Eddard Stark',
- * title: 'Warden of the North'
- * }
- * }
- * },
- * {
- * updateOne: {
- * filter: { name: 'Eddard Stark' },
- * // If you were using the MongoDB driver directly, you'd need to do
- * // `update: { $set: { title: ... } }` but mongoose adds $set for
- * // you.
- * update: { title: 'Hand of the King' }
- * }
- * },
- * {
- * deleteOne: {
- * {
- * filter: { name: 'Eddard Stark' }
- * }
- * }
- * }
- * ]).then(res => {
- * // Prints "1 1 1"
- * console.log(res.insertedCount, res.modifiedCount, res.deletedCount);
- * });
- *
- * The [supported operations](https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/#db.collection.bulkWrite) are:
- *
- * - `insertOne`
- * - `updateOne`
- * - `updateMany`
- * - `deleteOne`
- * - `deleteMany`
- * - `replaceOne`
- *
- * @param {Array} ops
- * @param {Object} [ops.insertOne.document] The document to insert
- * @param {Object} [opts.updateOne.filter] Update the first document that matches this filter
- * @param {Object} [opts.updateOne.update] An object containing [update operators](https://docs.mongodb.com/manual/reference/operator/update/)
- * @param {Boolean} [opts.updateOne.upsert=false] If true, insert a doc if none match
- * @param {Object} [opts.updateOne.collation] The [MongoDB collation](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-collations) to use
- * @param {Array} [opts.updateOne.arrayFilters] The [array filters](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-36-array-filters.html) used in `update`
- * @param {Object} [opts.updateMany.filter] Update all the documents that match this filter
- * @param {Object} [opts.updateMany.update] An object containing [update operators](https://docs.mongodb.com/manual/reference/operator/update/)
- * @param {Boolean} [opts.updateMany.upsert=false] If true, insert a doc if no documents match `filter`
- * @param {Object} [opts.updateMany.collation] The [MongoDB collation](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-collations) to use
- * @param {Array} [opts.updateMany.arrayFilters] The [array filters](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-36-array-filters.html) used in `update`
- * @param {Object} [opts.deleteOne.filter] Delete the first document that matches this filter
- * @param {Object} [opts.deleteMany.filter] Delete all documents that match this filter
- * @param {Object} [opts.replaceOne.filter] Replace the first document that matches this filter
- * @param {Object} [opts.replaceOne.replacement] The replacement document
- * @param {Boolean} [opts.replaceOne.upsert=false] If true, insert a doc if no documents match `filter`
- * @param {Object} [options]
- * @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.
- * @param {ClientSession} [options.session=null] The session associated with this bulk write. See [transactions docs](/docs/transactions.html).
- * @param {String|number} [options.w=1] The [write concern](https://docs.mongodb.com/manual/reference/write-concern/). See [`Query#w()`](/docs/api.html#query_Query-w) for more information.
- * @param {number} [options.wtimeout=null] The [write concern timeout](https://docs.mongodb.com/manual/reference/write-concern/#wtimeout).
- * @param {Boolean} [options.j=true] If false, disable [journal acknowledgement](https://docs.mongodb.com/manual/reference/write-concern/#j-option)
- * @param {Boolean} [options.bypassDocumentValidation=false] If true, disable [MongoDB server-side schema validation](https://docs.mongodb.com/manual/core/schema-validation/) for all writes in this bulk.
- * @param {Function} [callback] callback `function(error, bulkWriteOpResult) {}`
- * @return {Promise} resolves to a [`BulkWriteOpResult`](http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~BulkWriteOpResult) if the operation succeeds
- * @api public
- */
-
- Model.bulkWrite = function(ops, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = null;
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
- options = options || {};
-
- const validations = ops.map(op => castBulkWrite(this, op, options));
-
- return utils.promiseOrCallback(callback, cb => {
- parallel(validations, error => {
- if (error) {
- return cb(error);
- }
-
- this.collection.bulkWrite(ops, options, (error, res) => {
- if (error) {
- return cb(error);
- }
-
- cb(null, res);
- });
- });
- }, this.events);
- };
-
- /**
- * Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
- * The document returned has no paths marked as modified initially.
- *
- * ####Example:
- *
- * // hydrate previous data into a Mongoose document
- * var mongooseCandy = Candy.hydrate({ _id: '54108337212ffb6d459f854c', type: 'jelly bean' });
- *
- * @param {Object} obj
- * @return {Model} document instance
- * @api public
- */
-
- Model.hydrate = function(obj) {
- const model = require('./queryhelpers').createModel(this, obj);
- model.init(obj);
- return model;
- };
-
- /**
- * Updates one document in the database without returning it.
- *
- * This function triggers the following middleware.
- *
- * - `update()`
- *
- * ####Examples:
- *
- * MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
- *
- * const res = await MyModel.update({ name: 'Tobi' }, { ferret: true });
- * res.n; // Number of documents that matched `{ name: 'Tobi' }`
- * // Number of documents that were changed. If every doc matched already
- * // had `ferret` set to `true`, `nModified` will be 0.
- * res.nModified;
- *
- * ####Valid options:
- *
- * - `strict` (boolean): overrides the [schema-level `strict` option](/docs/guide.html#strict) for this update
- * - `upsert` (boolean): whether to create the doc if it doesn't match (false)
- * - `writeConcern` (object): sets the [write concern](https://docs.mongodb.com/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](/docs/guide.html#writeConcern)
- * - `omitUndefined` (boolean): If true, delete any properties whose value is `undefined` when casting an update. In other words, if this is set, Mongoose will delete `baz` from the update in `Model.updateOne({}, { foo: 'bar', baz: undefined })` before sending the update to the server.
- * - `multi` (boolean): whether multiple documents should be updated (false)
- * - `runValidators`: if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
- * - `setDefaultsOnInsert` (boolean): if this and `upsert` are true, mongoose will apply the [defaults](http://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on [MongoDB's `$setOnInsert` operator](https://docs.mongodb.org/v2.4/reference/operator/update/setOnInsert/).
- * - `timestamps` (boolean): If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
- * - `overwrite` (boolean): disables update-only mode, allowing you to overwrite the doc (false)
- *
- * All `update` values are cast to their appropriate SchemaTypes before being sent.
- *
- * The `callback` function receives `(err, rawResponse)`.
- *
- * - `err` is the error if any occurred
- * - `rawResponse` is the full response from Mongo
- *
- * ####Note:
- *
- * All top level keys which are not `atomic` operation names are treated as set operations:
- *
- * ####Example:
- *
- * var query = { name: 'borne' };
- * Model.update(query, { name: 'jason bourne' }, options, callback);
- *
- * // is sent as
- * Model.update(query, { $set: { name: 'jason bourne' }}, options, function(err, res));
- * // if overwrite option is false. If overwrite is true, sent without the $set wrapper.
- *
- * This helps prevent accidentally overwriting all documents in your collection with `{ name: 'jason bourne' }`.
- *
- * ####Note:
- *
- * Be careful to not use an existing model instance for the update clause (this won't work and can cause weird behavior like infinite loops). Also, ensure that the update clause does not have an _id property, which causes Mongo to return a "Mod on _id not allowed" error.
- *
- * ####Note:
- *
- * Mongoose casts values and runs setters when using update. The following
- * features are **not** applied by default.
- *
- * - [defaults](/docs/defaults.html#the-setdefaultsoninsert-option)
- * - [validators](/docs/validation.html#update-validators)
- * - middleware
- *
- * If you need document middleware and fully-featured validation, load the
- * document first and then use [`save()`](/docs/api.html#model_Model-save).
- *
- * Model.findOne({ name: 'borne' }, function (err, doc) {
- * if (err) ..
- * doc.name = 'jason bourne';
- * doc.save(callback);
- * })
- *
- * @see strict http://mongoosejs.com/docs/guide.html#strict
- * @see response http://docs.mongodb.org/v2.6/reference/command/update/#output
- * @param {Object} filter
- * @param {Object} doc
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
- * @param {Object} [options.writeConcern=null] sets the [write concern](https://docs.mongodb.com/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](/docs/guide.html#writeConcern)
- * @param {Boolean} [options.omitUndefined=false] If true, delete any properties whose value is `undefined` when casting an update. In other words, if this is set, Mongoose will delete `baz` from the update in `Model.updateOne({}, { foo: 'bar', baz: undefined })` before sending the update to the server.
- * @param {Boolean} [options.multi=false] whether multiple documents should be updated or just the first one that matches `filter`.
- * @param {Boolean} [options.runValidators=false] if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
- * @param {Boolean} [options.setDefaultsOnInsert=false] if this and `upsert` are true, mongoose will apply the [defaults](http://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on [MongoDB's `$setOnInsert` operator](https://docs.mongodb.org/v2.4/reference/operator/update/setOnInsert/).
- * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
- * @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `doc`, Mongoose will wrap `doc` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`.
- * @param {Function} [callback] params are (error, writeOpResult)
- * @param {Function} [callback]
- * @return {Query}
- * @see MongoDB docs https://docs.mongodb.com/manual/reference/command/update/#update-command-output
- * @see writeOpResult http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult
- * @see Query docs https://mongoosejs.com/docs/queries.html
- * @api public
- */
-
- Model.update = function update(conditions, doc, options, callback) {
- return _update(this, 'update', conditions, doc, options, callback);
- };
-
- /**
- * Same as `update()`, except MongoDB will update _all_ documents that match
- * `filter` (as opposed to just the first one) regardless of the value of
- * the `multi` option.
- *
- * **Note** updateMany will _not_ fire update middleware. Use `pre('updateMany')`
- * and `post('updateMany')` instead.
- *
- * ####Example:
- * const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
- * res.n; // Number of documents matched
- * res.nModified; // Number of documents modified
- *
- * This function triggers the following middleware.
- *
- * - `updateMany()`
- *
- * @param {Object} filter
- * @param {Object} doc
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
- * @param {Object} [options.writeConcern=null] sets the [write concern](https://docs.mongodb.com/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](/docs/guide.html#writeConcern)
- * @param {Boolean} [options.omitUndefined=false] If true, delete any properties whose value is `undefined` when casting an update. In other words, if this is set, Mongoose will delete `baz` from the update in `Model.updateOne({}, { foo: 'bar', baz: undefined })` before sending the update to the server.
- * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
- * @param {Function} [callback] `function(error, res) {}` where `res` has 3 properties: `n`, `nModified`, `ok`.
- * @return {Query}
- * @see Query docs https://mongoosejs.com/docs/queries.html
- * @see MongoDB docs https://docs.mongodb.com/manual/reference/command/update/#update-command-output
- * @see writeOpResult http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult
- * @api public
- */
-
- Model.updateMany = function updateMany(conditions, doc, options, callback) {
- return _update(this, 'updateMany', conditions, doc, options, callback);
- };
-
- /**
- * Same as `update()`, except it does not support the `multi` or `overwrite`
- * options.
- *
- * - MongoDB will update _only_ the first document that matches `filter` regardless of the value of the `multi` option.
- * - Use `replaceOne()` if you want to overwrite an entire document rather than using atomic operators like `$set`.
- *
- * ####Example:
- * const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
- * res.n; // Number of documents matched
- * res.nModified; // Number of documents modified
- *
- * This function triggers the following middleware.
- *
- * - `updateOne()`
- *
- * @param {Object} filter
- * @param {Object} doc
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
- * @param {Object} [options.writeConcern=null] sets the [write concern](https://docs.mongodb.com/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](/docs/guide.html#writeConcern)
- * @param {Boolean} [options.omitUndefined=false] If true, delete any properties whose value is `undefined` when casting an update. In other words, if this is set, Mongoose will delete `baz` from the update in `Model.updateOne({}, { foo: 'bar', baz: undefined })` before sending the update to the server.
- * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
- * @param {Function} [callback] params are (error, writeOpResult)
- * @return {Query}
- * @see Query docs https://mongoosejs.com/docs/queries.html
- * @see MongoDB docs https://docs.mongodb.com/manual/reference/command/update/#update-command-output
- * @see writeOpResult http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult
- * @api public
- */
-
- Model.updateOne = function updateOne(conditions, doc, options, callback) {
- return _update(this, 'updateOne', conditions, doc, options, callback);
- };
-
- /**
- * Same as `update()`, except MongoDB replace the existing document with the
- * given document (no atomic operators like `$set`).
- *
- * ####Example:
- * const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
- * res.n; // Number of documents matched
- * res.nModified; // Number of documents modified
- *
- * This function triggers the following middleware.
- *
- * - `replaceOne()`
- *
- * @param {Object} filter
- * @param {Object} doc
- * @param {Object} [options] optional see [`Query.prototype.setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
- * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](http://mongoosejs.com/docs/guide.html#strict)
- * @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
- * @param {Object} [options.writeConcern=null] sets the [write concern](https://docs.mongodb.com/manual/reference/write-concern/) for replica sets. Overrides the [schema-level write concern](/docs/guide.html#writeConcern)
- * @param {Boolean} [options.omitUndefined=false] If true, delete any properties whose value is `undefined` when casting an update. In other words, if this is set, Mongoose will delete `baz` from the update in `Model.updateOne({}, { foo: 'bar', baz: undefined })` before sending the update to the server.
- * @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
- * @param {Function} [callback] `function(error, res) {}` where `res` has 3 properties: `n`, `nModified`, `ok`.
- * @return {Query}
- * @see Query docs https://mongoosejs.com/docs/queries.html
- * @see writeOpResult http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~WriteOpResult
- * @return {Query}
- * @api public
- */
-
- Model.replaceOne = function replaceOne(conditions, doc, options, callback) {
- const versionKey = get(this, 'schema.options.versionKey', null);
- if (versionKey && !doc[versionKey]) {
- doc[versionKey] = 0;
- }
-
- return _update(this, 'replaceOne', conditions, doc, options, callback);
- };
-
- /*!
- * Common code for `updateOne()`, `updateMany()`, `replaceOne()`, and `update()`
- * because they need to do the same thing
- */
-
- function _update(model, op, conditions, doc, options, callback) {
- const mq = new model.Query({}, {}, model, model.collection);
- if (callback) {
- callback = model.$wrapCallback(callback);
- }
- // gh-2406
- // make local deep copy of conditions
- if (conditions instanceof Document) {
- conditions = conditions.toObject();
- } else {
- conditions = utils.clone(conditions);
- }
- options = typeof options === 'function' ? options : utils.clone(options);
-
- const versionKey = get(model, 'schema.options.versionKey', null);
- _decorateUpdateWithVersionKey(doc, options, versionKey);
-
- return mq[op](conditions, doc, options, callback);
- }
-
- /**
- * Executes a mapReduce command.
- *
- * `o` is an object specifying all mapReduce options as well as the map and reduce functions. All options are delegated to the driver implementation. See [node-mongodb-native mapReduce() documentation](http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#mapreduce) for more detail about options.
- *
- * This function does not trigger any middleware.
- *
- * ####Example:
- *
- * var o = {};
- * // `map()` and `reduce()` are run on the MongoDB server, not Node.js,
- * // these functions are converted to strings
- * o.map = function () { emit(this.name, 1) };
- * o.reduce = function (k, vals) { return vals.length };
- * User.mapReduce(o, function (err, results) {
- * console.log(results)
- * })
- *
- * ####Other options:
- *
- * - `query` {Object} query filter object.
- * - `sort` {Object} sort input objects using this key
- * - `limit` {Number} max number of documents
- * - `keeptemp` {Boolean, default:false} keep temporary data
- * - `finalize` {Function} finalize function
- * - `scope` {Object} scope variables exposed to map/reduce/finalize during execution
- * - `jsMode` {Boolean, default:false} it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X
- * - `verbose` {Boolean, default:false} provide statistics on job execution time.
- * - `readPreference` {String}
- * - `out*` {Object, default: {inline:1}} sets the output target for the map reduce job.
- *
- * ####* out options:
- *
- * - `{inline:1}` the results are returned in an array
- * - `{replace: 'collectionName'}` add the results to collectionName: the results replace the collection
- * - `{reduce: 'collectionName'}` add the results to collectionName: if dups are detected, uses the reducer / finalize functions
- * - `{merge: 'collectionName'}` add the results to collectionName: if dups exist the new docs overwrite the old
- *
- * If `options.out` is set to `replace`, `merge`, or `reduce`, a Model instance is returned that can be used for further querying. Queries run against this model are all executed with the [`lean` option](/docs/tutorials/lean.html); meaning only the js object is returned and no Mongoose magic is applied (getters, setters, etc).
- *
- * ####Example:
- *
- * var o = {};
- * // You can also define `map()` and `reduce()` as strings if your
- * // linter complains about `emit()` not being defined
- * o.map = 'function () { emit(this.name, 1) }';
- * o.reduce = 'function (k, vals) { return vals.length }';
- * o.out = { replace: 'createdCollectionNameForResults' }
- * o.verbose = true;
- *
- * User.mapReduce(o, function (err, model, stats) {
- * console.log('map reduce took %d ms', stats.processtime)
- * model.find().where('value').gt(10).exec(function (err, docs) {
- * console.log(docs);
- * });
- * })
- *
- * // `mapReduce()` returns a promise. However, ES6 promises can only
- * // resolve to exactly one value,
- * o.resolveToObject = true;
- * var promise = User.mapReduce(o);
- * promise.then(function (res) {
- * var model = res.model;
- * var stats = res.stats;
- * console.log('map reduce took %d ms', stats.processtime)
- * return model.find().where('value').gt(10).exec();
- * }).then(function (docs) {
- * console.log(docs);
- * }).then(null, handleError).end()
- *
- * @param {Object} o an object specifying map-reduce options
- * @param {Function} [callback] optional callback
- * @see http://www.mongodb.org/display/DOCS/MapReduce
- * @return {Promise}
- * @api public
- */
-
- Model.mapReduce = function mapReduce(o, callback) {
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
- return utils.promiseOrCallback(callback, cb => {
- if (!Model.mapReduce.schema) {
- const opts = {noId: true, noVirtualId: true, strict: false};
- Model.mapReduce.schema = new Schema({}, opts);
- }
-
- if (!o.out) o.out = {inline: 1};
- if (o.verbose !== false) o.verbose = true;
-
- o.map = String(o.map);
- o.reduce = String(o.reduce);
-
- if (o.query) {
- let q = new this.Query(o.query);
- q.cast(this);
- o.query = q._conditions;
- q = undefined;
- }
-
- this.collection.mapReduce(null, null, o, (err, res) => {
- if (err) {
- return cb(err);
- }
- if (res.collection) {
- // returned a collection, convert to Model
- const model = Model.compile('_mapreduce_' + res.collection.collectionName,
- Model.mapReduce.schema, res.collection.collectionName, this.db,
- this.base);
-
- model._mapreduce = true;
- res.model = model;
-
- return cb(null, res);
- }
-
- cb(null, res);
- });
- }, this.events);
- };
-
- /**
- * Performs [aggregations](http://docs.mongodb.org/manual/applications/aggregation/) on the models collection.
- *
- * If a `callback` is passed, the `aggregate` is executed and a `Promise` is returned. If a callback is not passed, the `aggregate` itself is returned.
- *
- * This function triggers the following middleware.
- *
- * - `aggregate()`
- *
- * ####Example:
- *
- * // Find the max balance of all accounts
- * Users.aggregate([
- * { $group: { _id: null, maxBalance: { $max: '$balance' }}},
- * { $project: { _id: 0, maxBalance: 1 }}
- * ]).
- * then(function (res) {
- * console.log(res); // [ { maxBalance: 98000 } ]
- * });
- *
- * // Or use the aggregation pipeline builder.
- * Users.aggregate().
- * group({ _id: null, maxBalance: { $max: '$balance' } }).
- * project('-id maxBalance').
- * exec(function (err, res) {
- * if (err) return handleError(err);
- * console.log(res); // [ { maxBalance: 98 } ]
- * });
- *
- * ####NOTE:
- *
- * - Arguments are not cast to the model's schema because `$project` operators allow redefining the "shape" of the documents at any stage of the pipeline, which may leave documents in an incompatible format.
- * - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
- * - Requires MongoDB >= 2.1
- *
- * @see Aggregate #aggregate_Aggregate
- * @see MongoDB http://docs.mongodb.org/manual/applications/aggregation/
- * @param {Array} [pipeline] aggregation pipeline as an array of objects
- * @param {Function} [callback]
- * @return {Aggregate}
- * @api public
- */
-
- Model.aggregate = function aggregate(pipeline, callback) {
- if (arguments.length > 2 || get(pipeline, 'constructor.name') === 'Object') {
- throw new Error('Mongoose 5.x disallows passing a spread of operators ' +
- 'to `Model.aggregate()`. Instead of ' +
- '`Model.aggregate({ $match }, { $skip })`, do ' +
- '`Model.aggregate([{ $match }, { $skip }])`');
- }
-
- if (typeof pipeline === 'function') {
- callback = pipeline;
- pipeline = [];
- }
-
- const aggregate = new Aggregate(pipeline || []);
- aggregate.model(this);
-
- if (typeof callback === 'undefined') {
- return aggregate;
- }
-
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- aggregate.exec(callback);
- return aggregate;
- };
-
- /**
- * Implements `$geoSearch` functionality for Mongoose
- *
- * This function does not trigger any middleware
- *
- * ####Example:
- *
- * var options = { near: [10, 10], maxDistance: 5 };
- * Locations.geoSearch({ type : "house" }, options, function(err, res) {
- * console.log(res);
- * });
- *
- * ####Options:
- * - `near` {Array} x,y point to search for
- * - `maxDistance` {Number} the maximum distance from the point near that a result can be
- * - `limit` {Number} The maximum number of results to return
- * - `lean` {Object|Boolean} return the raw object instead of the Mongoose Model
- *
- * @param {Object} conditions an object that specifies the match condition (required)
- * @param {Object} options for the geoSearch, some (near, maxDistance) are required
- * @param {Object|Boolean} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](/docs/api.html#query_Query-lean) and the [Mongoose lean tutorial](/docs/tutorials/lean.html).
- * @param {Function} [callback] optional callback
- * @return {Promise}
- * @see http://docs.mongodb.org/manual/reference/command/geoSearch/
- * @see http://docs.mongodb.org/manual/core/geohaystack/
- * @api public
- */
-
- Model.geoSearch = function(conditions, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- return utils.promiseOrCallback(callback, cb => {
- let error;
- if (conditions === undefined || !utils.isObject(conditions)) {
- error = new Error('Must pass conditions to geoSearch');
- } else if (!options.near) {
- error = new Error('Must specify the near option in geoSearch');
- } else if (!Array.isArray(options.near)) {
- error = new Error('near option must be an array [x, y]');
- }
-
- if (error) {
- return cb(error);
- }
-
- // send the conditions in the options object
- options.search = conditions;
-
- this.collection.geoHaystackSearch(options.near[0], options.near[1], options, (err, res) => {
- if (err) {
- return cb(err);
- }
-
- let count = res.results.length;
- if (options.lean || count === 0) {
- return cb(null, res.results);
- }
-
- const errSeen = false;
-
- function init(err) {
- if (err && !errSeen) {
- return cb(err);
- }
-
- if (!--count && !errSeen) {
- cb(null, res.results);
- }
- }
-
- for (let i = 0; i < res.results.length; ++i) {
- const temp = res.results[i];
- res.results[i] = new this();
- res.results[i].init(temp, {}, init);
- }
- });
- }, this.events);
- };
-
- /**
- * Populates document references.
- *
- * ####Available top-level options:
- *
- * - path: space delimited path(s) to populate
- * - select: optional fields to select
- * - match: optional query conditions to match
- * - model: optional name of the model to use for population
- * - options: optional query options like sort, limit, etc
- * - justOne: optional boolean, if true Mongoose will always set `path` to an array. Inferred from schema by default.
- *
- * ####Examples:
- *
- * // populates a single object
- * User.findById(id, function (err, user) {
- * var opts = [
- * { path: 'company', match: { x: 1 }, select: 'name' },
- * { path: 'notes', options: { limit: 10 }, model: 'override' }
- * ];
- *
- * User.populate(user, opts, function (err, user) {
- * console.log(user);
- * });
- * });
- *
- * // populates an array of objects
- * User.find(match, function (err, users) {
- * var opts = [{ path: 'company', match: { x: 1 }, select: 'name' }];
- *
- * var promise = User.populate(users, opts);
- * promise.then(console.log).end();
- * })
- *
- * // imagine a Weapon model exists with two saved documents:
- * // { _id: 389, name: 'whip' }
- * // { _id: 8921, name: 'boomerang' }
- * // and this schema:
- * // new Schema({
- * // name: String,
- * // weapon: { type: ObjectId, ref: 'Weapon' }
- * // });
- *
- * var user = { name: 'Indiana Jones', weapon: 389 };
- * Weapon.populate(user, { path: 'weapon', model: 'Weapon' }, function (err, user) {
- * console.log(user.weapon.name); // whip
- * })
- *
- * // populate many plain objects
- * var users = [{ name: 'Indiana Jones', weapon: 389 }]
- * users.push({ name: 'Batman', weapon: 8921 })
- * Weapon.populate(users, { path: 'weapon' }, function (err, users) {
- * users.forEach(function (user) {
- * console.log('%s uses a %s', users.name, user.weapon.name)
- * // Indiana Jones uses a whip
- * // Batman uses a boomerang
- * });
- * });
- * // Note that we didn't need to specify the Weapon model because
- * // it is in the schema's ref
- *
- * @param {Document|Array} docs Either a single document or array of documents to populate.
- * @param {Object} options A hash of key/val (path, options) used for population.
- * @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.
- * @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`](/docs/schematypes.html#schematype-options).
- * @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.
- * @param {Object|Function} [options.match=null] Add an additional filter to the populate query. Can be a filter object containing [MongoDB query syntax](https://docs.mongodb.com/manual/tutorial/query-documents/), or a function that returns a filter object.
- * @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.
- * @param {Function} [callback(err,doc)] Optional callback, executed upon completion. Receives `err` and the `doc(s)`.
- * @return {Promise}
- * @api public
- */
-
- Model.populate = function(docs, paths, callback) {
- const _this = this;
- if (callback) {
- callback = this.$wrapCallback(callback);
- }
-
- // normalized paths
- paths = utils.populate(paths);
-
- // data that should persist across subPopulate calls
- const cache = {};
-
- return utils.promiseOrCallback(callback, cb => {
- _populate(_this, docs, paths, cache, cb);
- }, this.events);
- };
-
- /*!
- * Populate helper
- *
- * @param {Model} model the model to use
- * @param {Document|Array} docs Either a single document or array of documents to populate.
- * @param {Object} paths
- * @param {Function} [cb(err,doc)] Optional callback, executed upon completion. Receives `err` and the `doc(s)`.
- * @return {Function}
- * @api private
- */
-
- function _populate(model, docs, paths, cache, callback) {
- const length = paths.length;
- let pending = paths.length;
-
- if (length === 0) {
- return callback(null, docs);
- }
-
- // each path has its own query options and must be executed separately
- for (let i = 0; i < length; ++i) {
- populate(model, docs, paths[i], next);
- }
-
- function next(err) {
- if (err) {
- return callback(err, null);
- }
- if (--pending) {
- return;
- }
- callback(null, docs);
- }
- }
-
- /*!
- * Populates `docs`
- */
- const excludeIdReg = /\s?-_id\s?/;
- const excludeIdRegGlobal = /\s?-_id\s?/g;
-
- function populate(model, docs, options, callback) {
- // normalize single / multiple docs passed
- if (!Array.isArray(docs)) {
- docs = [docs];
- }
-
- if (docs.length === 0 || docs.every(utils.isNullOrUndefined)) {
- return callback();
- }
-
- const modelsMap = getModelsMapForPopulate(model, docs, options);
-
- if (modelsMap instanceof Error) {
- return immediate(function() {
- callback(modelsMap);
- });
- }
-
- const len = modelsMap.length;
- let mod;
- let match;
- let select;
- let vals = [];
-
- function flatten(item) {
- // no need to include undefined values in our query
- return undefined !== item;
- }
-
- let _remaining = len;
- let hasOne = false;
- for (let i = 0; i < len; ++i) {
- mod = modelsMap[i];
- select = mod.options.select;
- match = _formatMatch(mod.match);
-
- let ids = utils.array.flatten(mod.ids, flatten);
- ids = utils.array.unique(ids);
-
- const assignmentOpts = {};
- assignmentOpts.sort = get(mod, 'options.options.sort', void 0);
- assignmentOpts.excludeId = excludeIdReg.test(select) || (select && select._id === 0);
-
- if (ids.length === 0 || ids.every(utils.isNullOrUndefined)) {
- --_remaining;
- // Ensure that we set populate virtuals with count option to 0 even
- // if we don't actually execute a query.
- if (mod.count) {
- next(mod, assignmentOpts, null, []);
- }
- continue;
- }
-
- hasOne = true;
- if (mod.foreignField.size === 1) {
- const foreignField = Array.from(mod.foreignField)[0];
- const foreignSchemaType = mod.model.schema.path(foreignField);
- if (foreignField !== '_id' || !match['_id']) {
- ids = _filterInvalidIds(ids, foreignSchemaType, mod.options.skipInvalidIds);
- match[foreignField] = { $in: ids };
- }
- } else {
- const $or = [];
- if (Array.isArray(match.$or)) {
- match.$and = [{ $or: match.$or }, { $or: $or }];
- delete match.$or;
- } else {
- match.$or = $or;
- }
- for (const foreignField of mod.foreignField) {
- if (foreignField !== '_id' || !match['_id']) {
- const foreignSchemaType = mod.model.schema.path(foreignField);
- ids = _filterInvalidIds(ids, foreignSchemaType, mod.options.skipInvalidIds);
- $or.push({ [foreignField]: { $in: ids } });
- }
- }
- }
-
- if (assignmentOpts.excludeId) {
- // override the exclusion from the query so we can use the _id
- // for document matching during assignment. we'll delete the
- // _id back off before returning the result.
- if (typeof select === 'string') {
- select = select.replace(excludeIdRegGlobal, ' ');
- } else {
- // preserve original select conditions by copying
- select = utils.object.shallowCopy(select);
- delete select._id;
- }
- }
-
- if (mod.options.options && mod.options.options.limit) {
- assignmentOpts.originalLimit = mod.options.options.limit;
- mod.options.options.limit = mod.options.options.limit * ids.length;
- }
-
- const subPopulate = utils.clone(mod.options.populate);
-
- const query = mod.model.find(match, select, mod.options.options);
-
- // If using count, still need the `foreignField` so we can match counts
- // to documents, otherwise we would need a separate `count()` for every doc.
- if (mod.count) {
- for (const foreignField of mod.foreignField) {
- query.select(foreignField);
- }
- }
-
- // If we're doing virtual populate and projection is inclusive and foreign
- // field is not selected, automatically select it because mongoose needs it.
- // If projection is exclusive and client explicitly unselected the foreign
- // field, that's the client's fault.
- for (const foreignField of mod.foreignField) {
- if (foreignField !== '_id' && query.selectedInclusively() &&
- !isPathSelectedInclusive(query._fields, foreignField)) {
- query.select(foreignField);
- }
- }
-
- // If we need to sub-populate, call populate recursively
- if (subPopulate) {
- query.populate(subPopulate);
- }
-
- query.exec(next.bind(this, mod, assignmentOpts));
- }
-
- if (!hasOne) {
- return callback();
- }
-
- function next(options, assignmentOpts, err, valsFromDb) {
- if (mod.options.options && mod.options.options.limit) {
- mod.options.options.limit = assignmentOpts.originalLimit;
- }
-
- if (err) return callback(err, null);
- vals = vals.concat(valsFromDb);
- _assign(null, vals, options, assignmentOpts);
- if (--_remaining === 0) {
- callback();
- }
- }
-
- function _assign(err, vals, mod, assignmentOpts) {
- if (err) {
- return callback(err, null);
- }
-
- const options = mod.options;
- const isVirtual = mod.isVirtual;
- const justOne = mod.justOne;
- let _val;
- const lean = options.options && options.options.lean;
- const len = vals.length;
- const rawOrder = {};
- const rawDocs = {};
- let key;
- let val;
-
- // Clone because `assignRawDocsToIdStructure` will mutate the array
- const allIds = utils.clone(mod.allIds);
-
- // optimization:
- // record the document positions as returned by
- // the query result.
- for (let i = 0; i < len; i++) {
- val = vals[i];
- if (val == null) {
- continue;
- }
- for (const foreignField of mod.foreignField) {
- _val = utils.getValue(foreignField, val);
- if (Array.isArray(_val)) {
- _val = utils.array.flatten(_val);
- const _valLength = _val.length;
- for (let j = 0; j < _valLength; ++j) {
- let __val = _val[j];
- if (__val instanceof Document) {
- __val = __val._id;
- }
- key = String(__val);
- if (rawDocs[key]) {
- if (Array.isArray(rawDocs[key])) {
- rawDocs[key].push(val);
- rawOrder[key].push(i);
- } else {
- rawDocs[key] = [rawDocs[key], val];
- rawOrder[key] = [rawOrder[key], i];
- }
- } else {
- if (isVirtual && !justOne) {
- rawDocs[key] = [val];
- rawOrder[key] = [i];
- } else {
- rawDocs[key] = val;
- rawOrder[key] = i;
- }
- }
- }
- } else {
- if (_val instanceof Document) {
- _val = _val._id;
- }
- key = String(_val);
- if (rawDocs[key]) {
- if (Array.isArray(rawDocs[key])) {
- rawDocs[key].push(val);
- rawOrder[key].push(i);
- } else {
- rawDocs[key] = [rawDocs[key], val];
- rawOrder[key] = [rawOrder[key], i];
- }
- } else {
- rawDocs[key] = val;
- rawOrder[key] = i;
- }
- }
- // flag each as result of population
- if (lean) {
- leanPopulateMap.set(val, mod.model);
- } else {
- val.$__.wasPopulated = true;
- }
- }
- }
-
- assignVals({
- originalModel: model,
- // If virtual, make sure to not mutate original field
- rawIds: mod.isVirtual ? allIds : mod.allIds,
- allIds: allIds,
- foreignField: mod.foreignField,
- rawDocs: rawDocs,
- rawOrder: rawOrder,
- docs: mod.docs,
- path: options.path,
- options: assignmentOpts,
- justOne: mod.justOne,
- isVirtual: mod.isVirtual,
- allOptions: mod,
- lean: lean,
- virtual: mod.virtual,
- count: mod.count,
- match: mod.match
- });
- }
- }
-
- /*!
- * Optionally filter out invalid ids that don't conform to foreign field's schema
- * to avoid cast errors (gh-7706)
- */
-
- function _filterInvalidIds(ids, foreignSchemaType, skipInvalidIds) {
- if (!skipInvalidIds) {
- return ids;
- }
- return ids.filter(id => {
- try {
- foreignSchemaType.cast(id);
- return true;
- } catch (err) {
- return false;
- }
- });
- }
-
- /*!
- * Format `mod.match` given that it may be an array that we need to $or if
- * the client has multiple docs with match functions
- */
-
- function _formatMatch(match) {
- if (Array.isArray(match)) {
- if (match.length > 1) {
- return { $or: [].concat(match.map(m => Object.assign({}, m))) };
- }
- return Object.assign({}, match[0]);
- }
- return Object.assign({}, match);
- }
-
- function getModelsMapForPopulate(model, docs, options) {
- let i;
- let doc;
- const len = docs.length;
- const available = {};
- const map = [];
- const modelNameFromQuery = options.model && options.model.modelName || options.model;
- let schema;
- let refPath;
- let Model;
- let currentOptions;
- let modelNames;
- let modelName;
- let modelForFindSchema;
-
- const originalModel = options.model;
- let isVirtual = false;
- const modelSchema = model.schema;
-
- for (i = 0; i < len; i++) {
- doc = docs[i];
-
- schema = getSchemaTypes(modelSchema, doc, options.path);
- const isUnderneathDocArray = schema && schema.$isUnderneathDocArray;
- if (isUnderneathDocArray && get(options, 'options.sort') != null) {
- return new Error('Cannot populate with `sort` on path ' + options.path +
- ' because it is a subproperty of a document array');
- }
-
- modelNames = null;
- let isRefPath = false;
- if (Array.isArray(schema)) {
- for (let j = 0; j < schema.length; ++j) {
- let _modelNames;
- try {
- const res = _getModelNames(doc, schema[j]);
- _modelNames = res.modelNames;
- isRefPath = res.isRefPath;
- } catch (error) {
- return error;
- }
- if (!_modelNames) {
- continue;
- }
- modelNames = modelNames || [];
- for (let x = 0; x < _modelNames.length; ++x) {
- if (modelNames.indexOf(_modelNames[x]) === -1) {
- modelNames.push(_modelNames[x]);
- }
- }
- }
- } else {
- try {
- const res = _getModelNames(doc, schema);
- modelNames = res.modelNames;
- isRefPath = res.isRefPath;
- } catch (error) {
- return error;
- }
-
- if (!modelNames) {
- continue;
- }
- }
-
- const virtual = getVirtual(model.schema, options.path);
- let localField;
- let count = false;
- if (virtual && virtual.options) {
- const virtualPrefix = virtual.$nestedSchemaPath ?
- virtual.$nestedSchemaPath + '.' : '';
- if (typeof virtual.options.localField === 'function') {
- localField = virtualPrefix + virtual.options.localField.call(doc, doc);
- } else {
- localField = virtualPrefix + virtual.options.localField;
- }
- count = virtual.options.count;
- } else {
- localField = options.path;
- }
- let foreignField = virtual && virtual.options ?
- virtual.options.foreignField :
- '_id';
-
- // `justOne = null` means we don't know from the schema whether the end
- // result should be an array or a single doc. This can result from
- // populating a POJO using `Model.populate()`
- let justOne = null;
- if ('justOne' in options) {
- justOne = options.justOne;
- } else if (virtual && virtual.options && virtual.options.refPath) {
- const normalizedRefPath =
- normalizeRefPath(virtual.options.refPath, doc, options.path);
- justOne = !!virtual.options.justOne;
- isVirtual = true;
- const refValue = utils.getValue(normalizedRefPath, doc);
- modelNames = Array.isArray(refValue) ? refValue : [refValue];
- } else if (virtual && virtual.options && virtual.options.ref) {
- let normalizedRef;
- if (typeof virtual.options.ref === 'function') {
- normalizedRef = virtual.options.ref.call(doc, doc);
- } else {
- normalizedRef = virtual.options.ref;
- }
- justOne = !!virtual.options.justOne;
- isVirtual = true;
- if (!modelNames) {
- modelNames = [].concat(normalizedRef);
- }
- } else if (schema && !schema[schemaMixedSymbol]) {
- // Skip Mixed types because we explicitly don't do casting on those.
- justOne = !schema.$isMongooseArray;
- }
-
- if (!modelNames) {
- continue;
- }
-
- if (virtual && (!localField || !foreignField)) {
- return new Error('If you are populating a virtual, you must set the ' +
- 'localField and foreignField options');
- }
-
- options.isVirtual = isVirtual;
- options.virtual = virtual;
- if (typeof localField === 'function') {
- localField = localField.call(doc, doc);
- }
- if (typeof foreignField === 'function') {
- foreignField = foreignField.call(doc);
- }
-
- const localFieldPathType = modelSchema._getPathType(localField);
- const localFieldPath = localFieldPathType === 'real' ? modelSchema.paths[localField] : localFieldPathType.schema;
- const localFieldGetters = localFieldPath && localFieldPath.getters ? localFieldPath.getters : [];
- let ret;
-
- const _populateOptions = get(options, 'options', {});
-
- const getters = 'getters' in _populateOptions ?
- _populateOptions.getters :
- options.isVirtual && get(virtual, 'options.getters', false);
- if (localFieldGetters.length > 0 && getters) {
- const hydratedDoc = (doc.$__ != null) ? doc : model.hydrate(doc);
- const localFieldValue = utils.getValue(localField, doc);
- if (Array.isArray(localFieldValue)) {
- const localFieldHydratedValue = utils.getValue(localField.split('.').slice(0, -1), hydratedDoc);
- ret = localFieldValue.map((localFieldArrVal, localFieldArrIndex) =>
- localFieldPath.applyGetters(localFieldArrVal, localFieldHydratedValue[localFieldArrIndex]));
- } else {
- ret = localFieldPath.applyGetters(localFieldValue, hydratedDoc);
- }
- } else {
- ret = convertTo_id(utils.getValue(localField, doc));
- }
-
- const id = String(utils.getValue(foreignField, doc));
- options._docs[id] = Array.isArray(ret) ? ret.slice() : ret;
-
- let match = get(options, 'match', null) ||
- get(currentOptions, 'match', null) ||
- get(options, 'virtual.options.options.match', null);
-
- const hasMatchFunction = typeof match === 'function';
- if (hasMatchFunction) {
- match = match.call(doc, doc);
- }
-
- let k = modelNames.length;
- while (k--) {
- modelName = modelNames[k];
- if (modelName == null) {
- continue;
- }
-
- // `PopulateOptions#connection`: if the model is passed as a string, the
- // connection matters because different connections have different models.
- const connection = options.connection != null ? options.connection : model.db;
-
- try {
- Model = originalModel && originalModel[modelSymbol] ?
- originalModel :
- modelName[modelSymbol] ? modelName : connection.model(modelName);
- } catch (error) {
- return error;
- }
-
- let ids = ret;
- const flat = Array.isArray(ret) ? utils.array.flatten(ret) : [];
- if (isRefPath && Array.isArray(ret) && flat.length === modelNames.length) {
- ids = flat.filter((val, i) => modelNames[i] === modelName);
- }
-
- if (!available[modelName]) {
- currentOptions = {
- model: Model
- };
-
- if (isVirtual && virtual.options && virtual.options.options) {
- currentOptions.options = utils.clone(virtual.options.options);
- }
- utils.merge(currentOptions, options);
-
- // Used internally for checking what model was used to populate this
- // path.
- options[populateModelSymbol] = Model;
-
- available[modelName] = {
- model: Model,
- options: currentOptions,
- match: hasMatchFunction ? [match] : match,
- docs: [doc],
- ids: [ids],
- allIds: [ret],
- localField: new Set([localField]),
- foreignField: new Set([foreignField]),
- justOne: justOne,
- isVirtual: isVirtual,
- virtual: virtual,
- count: count,
- [populateModelSymbol]: Model
- };
- map.push(available[modelName]);
- } else {
- available[modelName].localField.add(localField);
- available[modelName].foreignField.add(foreignField);
- available[modelName].docs.push(doc);
- available[modelName].ids.push(ids);
- available[modelName].allIds.push(ret);
- if (hasMatchFunction) {
- available[modelName].match.push(match);
- }
- }
- }
- }
-
- function _getModelNames(doc, schema) {
- let modelNames;
- let discriminatorKey;
- let isRefPath = false;
-
- if (schema && schema.caster) {
- schema = schema.caster;
- }
- if (schema && schema.$isSchemaMap) {
- schema = schema.$__schemaType;
- }
-
- if (!schema && model.discriminators) {
- discriminatorKey = model.schema.discriminatorMapping.key;
- }
-
- refPath = schema && schema.options && schema.options.refPath;
-
- const normalizedRefPath = normalizeRefPath(refPath, doc, options.path);
-
- if (modelNameFromQuery) {
- modelNames = [modelNameFromQuery]; // query options
- } else if (normalizedRefPath) {
- if (options._queryProjection != null && isPathExcluded(options._queryProjection, normalizedRefPath)) {
- throw new Error('refPath `' + normalizedRefPath +
- '` must not be excluded in projection, got ' +
- util.inspect(options._queryProjection));
- }
- modelNames = utils.getValue(normalizedRefPath, doc);
- if (Array.isArray(modelNames)) {
- modelNames = utils.array.flatten(modelNames);
- }
-
- isRefPath = true;
- } else {
- let modelForCurrentDoc = model;
- let schemaForCurrentDoc;
-
- if (!schema && discriminatorKey) {
- modelForFindSchema = utils.getValue(discriminatorKey, doc);
-
- if (modelForFindSchema) {
- try {
- modelForCurrentDoc = model.db.model(modelForFindSchema);
- } catch (error) {
- return error;
- }
-
- schemaForCurrentDoc = modelForCurrentDoc.schema._getSchema(options.path);
-
- if (schemaForCurrentDoc && schemaForCurrentDoc.caster) {
- schemaForCurrentDoc = schemaForCurrentDoc.caster;
- }
- }
- } else {
- schemaForCurrentDoc = schema;
- }
- const virtual = getVirtual(modelForCurrentDoc.schema, options.path);
-
- let ref;
- if ((ref = get(schemaForCurrentDoc, 'options.ref')) != null) {
- ref = handleRefFunction(ref, doc);
- modelNames = [ref];
- } else if ((ref = get(virtual, 'options.ref')) != null) {
- ref = handleRefFunction(ref, doc);
-
- // When referencing nested arrays, the ref should be an Array
- // of modelNames.
- if (Array.isArray(ref)) {
- modelNames = ref;
- } else {
- modelNames = [ref];
- }
-
- isVirtual = true;
- } else {
- // We may have a discriminator, in which case we don't want to
- // populate using the base model by default
- modelNames = discriminatorKey ? null : [model.modelName];
- }
- }
-
- if (!modelNames) {
- return { modelNames: modelNames, isRefPath: isRefPath };
- }
-
- if (!Array.isArray(modelNames)) {
- modelNames = [modelNames];
- }
-
- return { modelNames: modelNames, isRefPath: isRefPath };
- }
-
- return map;
- }
-
- /*!
- * ignore
- */
-
- function handleRefFunction(ref, doc) {
- if (typeof ref === 'function' && !ref[modelSymbol]) {
- return ref.call(doc, doc);
- }
- return ref;
- }
-
- /*!
- * Retrieve the _id of `val` if a Document or Array of Documents.
- *
- * @param {Array|Document|Any} val
- * @return {Array|Document|Any}
- */
-
- function convertTo_id(val) {
- if (val instanceof Model) return val._id;
-
- if (Array.isArray(val)) {
- for (let i = 0; i < val.length; ++i) {
- if (val[i] instanceof Model) {
- val[i] = val[i]._id;
- }
- }
- if (val.isMongooseArray && val.$schema()) {
- return val.$schema().cast(val, val.$parent());
- }
-
- return [].concat(val);
- }
-
- // `populate('map')` may be an object if populating on a doc that hasn't
- // been hydrated yet
- if (val != null && val.constructor.name === 'Object') {
- const ret = [];
- for (const key of Object.keys(val)) {
- ret.push(val[key]);
- }
- return ret;
- }
- // If doc has already been hydrated, e.g. `doc.populate('map').execPopulate()`
- // then `val` will already be a map
- if (val instanceof Map) {
- return Array.from(val.values());
- }
-
- return val;
- }
-
- /*!
- * Compiler utility.
- *
- * @param {String|Function} name model name or class extending Model
- * @param {Schema} schema
- * @param {String} collectionName
- * @param {Connection} connection
- * @param {Mongoose} base mongoose instance
- */
-
- Model.compile = function compile(name, schema, collectionName, connection, base) {
- const versioningEnabled = schema.options.versionKey !== false;
-
- if (versioningEnabled && !schema.paths[schema.options.versionKey]) {
- // add versioning to top level documents only
- const o = {};
- o[schema.options.versionKey] = Number;
- schema.add(o);
- }
-
- let model;
- if (typeof name === 'function' && name.prototype instanceof Model) {
- model = name;
- name = model.name;
- schema.loadClass(model, false);
- model.prototype.$isMongooseModelPrototype = true;
- } else {
- // generate new class
- model = function model(doc, fields, skipId) {
- model.hooks.execPreSync('createModel', doc);
- if (!(this instanceof model)) {
- return new model(doc, fields, skipId);
- }
- const discriminatorKey = model.schema.options.discriminatorKey;
-
- if (model.discriminators == null || doc == null || doc[discriminatorKey] == null) {
- Model.call(this, doc, fields, skipId);
- return;
- }
-
- // If discriminator key is set, use the discriminator instead (gh-7586)
- const Discriminator = model.discriminators[doc[discriminatorKey]] ||
- getDiscriminatorByValue(model, doc[discriminatorKey]);
- if (Discriminator != null) {
- return new Discriminator(doc, fields, skipId);
- }
-
- // Otherwise, just use the top-level model
- Model.call(this, doc, fields, skipId);
- };
- }
-
- model.hooks = schema.s.hooks.clone();
- model.base = base;
- model.modelName = name;
-
- if (!(model.prototype instanceof Model)) {
- model.__proto__ = Model;
- model.prototype.__proto__ = Model.prototype;
- }
- model.model = Model.prototype.model;
- model.db = model.prototype.db = connection;
- model.discriminators = model.prototype.discriminators = undefined;
- model[modelSymbol] = true;
- model.events = new EventEmitter();
-
- model.prototype.$__setSchema(schema);
-
- const _userProvidedOptions = schema._userProvidedOptions || {};
-
- // `bufferCommands` is true by default...
- let bufferCommands = true;
- // First, take the global option
- if (connection.base.get('bufferCommands') != null) {
- bufferCommands = connection.base.get('bufferCommands');
- }
- // Connection-specific overrides the global option
- if (connection.config.bufferCommands != null) {
- bufferCommands = connection.config.bufferCommands;
- }
- // And schema options override global and connection
- if (_userProvidedOptions.bufferCommands != null) {
- bufferCommands = _userProvidedOptions.bufferCommands;
- }
-
- const collectionOptions = {
- bufferCommands: bufferCommands,
- capped: schema.options.capped,
- Promise: model.base.Promise
- };
-
- model.prototype.collection = connection.collection(
- collectionName,
- collectionOptions
- );
- model.prototype[modelCollectionSymbol] = model.prototype.collection;
-
- // apply methods and statics
- applyMethods(model, schema);
- applyStatics(model, schema);
- applyHooks(model, schema);
- applyStaticHooks(model, schema.s.hooks, schema.statics);
-
- model.schema = model.prototype.schema;
- model.collection = model.prototype.collection;
-
- // Create custom query constructor
- model.Query = function() {
- Query.apply(this, arguments);
- };
- model.Query.prototype = Object.create(Query.prototype);
- model.Query.base = Query.base;
- applyQueryMiddleware(model.Query, model);
- applyQueryMethods(model, schema.query);
-
- return model;
- };
-
- /*!
- * Register custom query methods for this model
- *
- * @param {Model} model
- * @param {Schema} schema
- */
-
- function applyQueryMethods(model, methods) {
- for (const i in methods) {
- model.Query.prototype[i] = methods[i];
- }
- }
-
- /*!
- * Subclass this model with `conn`, `schema`, and `collection` settings.
- *
- * @param {Connection} conn
- * @param {Schema} [schema]
- * @param {String} [collection]
- * @return {Model}
- */
-
- Model.__subclass = function subclass(conn, schema, collection) {
- // subclass model using this connection and collection name
- const _this = this;
-
- const Model = function Model(doc, fields, skipId) {
- if (!(this instanceof Model)) {
- return new Model(doc, fields, skipId);
- }
- _this.call(this, doc, fields, skipId);
- };
-
- Model.__proto__ = _this;
- Model.prototype.__proto__ = _this.prototype;
- Model.db = Model.prototype.db = conn;
-
- _this[subclassedSymbol] = _this[subclassedSymbol] || [];
- _this[subclassedSymbol].push(Model);
- if (_this.discriminators != null) {
- Model.discriminators = {};
- for (const key of Object.keys(_this.discriminators)) {
- Model.discriminators[key] = _this.discriminators[key].
- __subclass(_this.db, _this.discriminators[key].schema, collection);
- }
- }
-
- const s = schema && typeof schema !== 'string'
- ? schema
- : _this.prototype.schema;
-
- const options = s.options || {};
- const _userProvidedOptions = s._userProvidedOptions || {};
-
- if (!collection) {
- collection = _this.prototype.schema.get('collection') ||
- utils.toCollectionName(_this.modelName, this.base.pluralize());
- }
-
- let bufferCommands = true;
- if (s) {
- if (conn.config.bufferCommands != null) {
- bufferCommands = conn.config.bufferCommands;
- }
- if (_userProvidedOptions.bufferCommands != null) {
- bufferCommands = _userProvidedOptions.bufferCommands;
- }
- }
- const collectionOptions = {
- bufferCommands: bufferCommands,
- capped: s && options.capped
- };
-
- Model.prototype.collection = conn.collection(collection, collectionOptions);
- Model.prototype[modelCollectionSymbol] = Model.prototype.collection;
- Model.collection = Model.prototype.collection;
- // Errors handled internally, so ignore
- Model.init(() => {});
- return Model;
- };
-
- Model.$wrapCallback = function(callback) {
- if (callback == null) {
- return callback;
- }
- if (typeof callback !== 'function') {
- throw new Error('Callback must be a function, got ' + callback);
- }
- const _this = this;
- return function() {
- try {
- callback.apply(null, arguments);
- } catch (error) {
- _this.emit('error', error);
- }
- };
- };
-
- /**
- * Helper for console.log
- *
- * @api public
- * @method inspect
- * @instance
- */
-
- Model.inspect = function() {
- return `Model { ${this.modelName} }`;
- };
-
- if (util.inspect.custom) {
- /*!
- * Avoid Node deprecation warning DEP0079
- */
-
- Model[util.inspect.custom] = Model.inspect;
- }
-
- /*!
- * Module exports.
- */
-
- module.exports = exports = Model;
|