|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411 |
- 'use strict';
-
- /*!
- * Module dependencies.
- */
-
- const EventEmitter = require('events').EventEmitter;
- const InternalCache = require('./internal');
- const MongooseError = require('./error');
- const MixedSchema = require('./schema/mixed');
- const ObjectExpectedError = require('./error/objectExpected');
- const ObjectParameterError = require('./error/objectParameter');
- const StrictModeError = require('./error/strict');
- const ValidatorError = require('./schematype').ValidatorError;
- const VirtualType = require('./virtualtype');
- const cleanModifiedSubpaths = require('./helpers/document/cleanModifiedSubpaths');
- const compile = require('./helpers/document/compile').compile;
- const defineKey = require('./helpers/document/compile').defineKey;
- const flatten = require('./helpers/common').flatten;
- const get = require('./helpers/get');
- const getEmbeddedDiscriminatorPath = require('./helpers/document/getEmbeddedDiscriminatorPath');
- const idGetter = require('./plugins/idGetter');
- const isDefiningProjection = require('./helpers/projection/isDefiningProjection');
- const isExclusive = require('./helpers/projection/isExclusive');
- const inspect = require('util').inspect;
- const internalToObjectOptions = require('./options').internalToObjectOptions;
- const mpath = require('mpath');
- const utils = require('./utils');
-
- const ValidationError = MongooseError.ValidationError;
- const clone = utils.clone;
- const deepEqual = utils.deepEqual;
- const isMongooseObject = utils.isMongooseObject;
-
- const arrayAtomicsSymbol = require('./helpers/symbols').arrayAtomicsSymbol;
- const documentArrayParent = require('./helpers/symbols').documentArrayParent;
- const documentSchemaSymbol = require('./helpers/symbols').documentSchemaSymbol;
- const getSymbol = require('./helpers/symbols').getSymbol;
- const populateModelSymbol = require('./helpers/symbols').populateModelSymbol;
-
- let DocumentArray;
- let MongooseArray;
- let Embedded;
-
- const specialProperties = utils.specialProperties;
-
- /**
- * The core Mongoose document constructor. You should not call this directly,
- * the Mongoose [Model constructor](./api.html#Model) calls this for you.
- *
- * @param {Object} obj the values to set
- * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
- * @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
- * @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
- * @event `init`: Emitted on a document after it has was retreived from the db and fully hydrated by Mongoose.
- * @event `save`: Emitted when the document is successfully saved
- * @api private
- */
-
- function Document(obj, fields, skipId, options) {
- if (typeof skipId === 'object' && skipId != null) {
- options = skipId;
- skipId = options.skipId;
- }
- options = options || {};
-
- this.$__ = new InternalCache;
- this.$__.emitter = new EventEmitter();
- this.isNew = 'isNew' in options ? options.isNew : true;
- this.errors = undefined;
- this.$__.$options = options || {};
-
- if (obj != null && typeof obj !== 'object') {
- throw new ObjectParameterError(obj, 'obj', 'Document');
- }
-
- const schema = this.schema;
-
- if (typeof fields === 'boolean') {
- this.$__.strictMode = fields;
- fields = undefined;
- } else {
- this.$__.strictMode = schema.options.strict;
- this.$__.selected = fields;
- }
-
- const required = schema.requiredPaths(true);
- for (let i = 0; i < required.length; ++i) {
- this.$__.activePaths.require(required[i]);
- }
-
- this.$__.emitter.setMaxListeners(0);
-
- let exclude = null;
-
- // determine if this doc is a result of a query with
- // excluded fields
- if (utils.isPOJO(fields)) {
- exclude = isExclusive(fields);
- }
-
- const hasIncludedChildren = exclude === false && fields ?
- $__hasIncludedChildren(fields) :
- {};
-
- if (this._doc == null) {
- this.$__buildDoc(obj, fields, skipId, exclude, hasIncludedChildren, false);
-
- // By default, defaults get applied **before** setting initial values
- // Re: gh-6155
- $__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, true, {
- isNew: this.isNew
- });
- }
-
- if (obj) {
- if (obj instanceof Document) {
- this.isNew = obj.isNew;
- }
- // Skip set hooks
- if (this.$__original_set) {
- this.$__original_set(obj, undefined, true);
- } else {
- this.$set(obj, undefined, true);
- }
- }
-
- // Function defaults get applied **after** setting initial values so they
- // see the full doc rather than an empty one, unless they opt out.
- // Re: gh-3781, gh-6155
- if (options.willInit) {
- this.once('init', () => {
- $__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, false, options.skipDefaults, {
- isNew: this.isNew
- });
- });
- } else {
- $__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, false, options.skipDefaults, {
- isNew: this.isNew
- });
- }
-
- this.$__._id = this._id;
- this.$locals = {};
-
- if (!schema.options.strict && obj) {
- const _this = this;
- const keys = Object.keys(this._doc);
-
- keys.forEach(function(key) {
- if (!(key in schema.tree)) {
- defineKey(key, null, _this);
- }
- });
- }
-
- applyQueue(this);
- }
-
- /*!
- * Document exposes the NodeJS event emitter API, so you can use
- * `on`, `once`, etc.
- */
- utils.each(
- ['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
- 'removeAllListeners', 'addListener'],
- function(emitterFn) {
- Document.prototype[emitterFn] = function() {
- return this.$__.emitter[emitterFn].apply(this.$__.emitter, arguments);
- };
- });
-
- Document.prototype.constructor = Document;
-
- /**
- * The documents schema.
- *
- * @api public
- * @property schema
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.schema;
-
- /**
- * Empty object that you can use for storing properties on the document. This
- * is handy for passing data to middleware without conflicting with Mongoose
- * internals.
- *
- * ####Example:
- *
- * schema.pre('save', function() {
- * // Mongoose will set `isNew` to `false` if `save()` succeeds
- * this.$locals.wasNew = this.isNew;
- * });
- *
- * schema.post('save', function() {
- * // Prints true if `isNew` was set before `save()`
- * console.log(this.$locals.wasNew);
- * });
- *
- * @api public
- * @property $locals
- * @memberOf Document
- * @instance
- */
-
- Object.defineProperty(Document.prototype, '$locals', {
- configurable: false,
- enumerable: false,
- writable: true
- });
-
- /**
- * Boolean flag specifying if the document is new.
- *
- * @api public
- * @property isNew
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.isNew;
-
- /**
- * The string version of this documents _id.
- *
- * ####Note:
- *
- * This getter exists on all documents by default. The getter can be disabled by setting the `id` [option](/docs/guide.html#id) of its `Schema` to false at construction time.
- *
- * new Schema({ name: String }, { id: false });
- *
- * @api public
- * @see Schema options /docs/guide.html#options
- * @property id
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.id;
-
- /**
- * Hash containing current validation errors.
- *
- * @api public
- * @property errors
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.errors;
-
- /*!
- * ignore
- */
-
- function $__hasIncludedChildren(fields) {
- const hasIncludedChildren = {};
- const keys = Object.keys(fields);
- for (let j = 0; j < keys.length; ++j) {
- const parts = keys[j].split('.');
- const c = [];
- for (let k = 0; k < parts.length; ++k) {
- c.push(parts[k]);
- hasIncludedChildren[c.join('.')] = 1;
- }
- }
-
- return hasIncludedChildren;
- }
-
- /*!
- * ignore
- */
-
- function $__applyDefaults(doc, fields, skipId, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip) {
- const paths = Object.keys(doc.schema.paths);
- const plen = paths.length;
-
- for (let i = 0; i < plen; ++i) {
- let def;
- let curPath = '';
- const p = paths[i];
-
- if (p === '_id' && skipId) {
- continue;
- }
-
- const type = doc.schema.paths[p];
- const path = p.split('.');
- const len = path.length;
- let included = false;
- let doc_ = doc._doc;
-
- for (let j = 0; j < len; ++j) {
- if (doc_ == null) {
- break;
- }
-
- const piece = path[j];
- curPath += (!curPath.length ? '' : '.') + piece;
-
- if (exclude === true) {
- if (curPath in fields) {
- break;
- }
- } else if (exclude === false && fields && !included) {
- if (curPath in fields) {
- included = true;
- } else if (!hasIncludedChildren[curPath]) {
- break;
- }
- }
-
- if (j === len - 1) {
- if (doc_[piece] !== void 0) {
- break;
- }
-
- if (typeof type.defaultValue === 'function') {
- if (!type.defaultValue.$runBeforeSetters && isBeforeSetters) {
- break;
- }
- if (type.defaultValue.$runBeforeSetters && !isBeforeSetters) {
- break;
- }
- } else if (!isBeforeSetters) {
- // Non-function defaults should always run **before** setters
- continue;
- }
-
- if (pathsToSkip && pathsToSkip[curPath]) {
- break;
- }
-
- if (fields && exclude !== null) {
- if (exclude === true) {
- // apply defaults to all non-excluded fields
- if (p in fields) {
- continue;
- }
-
- def = type.getDefault(doc, false);
- if (typeof def !== 'undefined') {
- doc_[piece] = def;
- doc.$__.activePaths.default(p);
- }
- } else if (included) {
- // selected field
- def = type.getDefault(doc, false);
- if (typeof def !== 'undefined') {
- doc_[piece] = def;
- doc.$__.activePaths.default(p);
- }
- }
- } else {
- def = type.getDefault(doc, false);
- if (typeof def !== 'undefined') {
- doc_[piece] = def;
- doc.$__.activePaths.default(p);
- }
- }
- } else {
- doc_ = doc_[piece];
- }
- }
- }
- }
-
- /**
- * Builds the default doc structure
- *
- * @param {Object} obj
- * @param {Object} [fields]
- * @param {Boolean} [skipId]
- * @api private
- * @method $__buildDoc
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__buildDoc = function(obj, fields, skipId, exclude, hasIncludedChildren) {
- const doc = {};
-
- const paths = Object.keys(this.schema.paths).
- // Don't build up any paths that are underneath a map, we don't know
- // what the keys will be
- filter(p => !p.includes('$*'));
- const plen = paths.length;
- let ii = 0;
-
- for (; ii < plen; ++ii) {
- const p = paths[ii];
-
- if (p === '_id') {
- if (skipId) {
- continue;
- }
- if (obj && '_id' in obj) {
- continue;
- }
- }
-
- const path = p.split('.');
- const len = path.length;
- const last = len - 1;
- let curPath = '';
- let doc_ = doc;
- let included = false;
-
- for (let i = 0; i < len; ++i) {
- const piece = path[i];
-
- curPath += (!curPath.length ? '' : '.') + piece;
-
- // support excluding intermediary levels
- if (exclude === true) {
- if (curPath in fields) {
- break;
- }
- } else if (exclude === false && fields && !included) {
- if (curPath in fields) {
- included = true;
- } else if (!hasIncludedChildren[curPath]) {
- break;
- }
- }
-
- if (i < last) {
- doc_ = doc_[piece] || (doc_[piece] = {});
- }
- }
- }
-
- this._doc = doc;
- };
-
- /*!
- * Converts to POJO when you use the document for querying
- */
-
- Document.prototype.toBSON = function() {
- return this.toObject(internalToObjectOptions);
- };
-
- /**
- * Initializes the document without setters or marking anything modified.
- *
- * Called internally after a document is returned from mongodb. Normally,
- * you do **not** need to call this function on your own.
- *
- * This function triggers `init` [middleware](/docs/middleware.html).
- * Note that `init` hooks are [synchronous](/docs/middleware.html#synchronous).
- *
- * @param {Object} doc document returned by mongo
- * @api public
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.init = function(doc, opts, fn) {
- if (typeof opts === 'function') {
- fn = opts;
- opts = null;
- }
-
- this.$__init(doc, opts);
-
- if (fn) {
- fn(null, this);
- }
-
- return this;
- };
-
- /*!
- * ignore
- */
-
- Document.prototype.$__init = function(doc, opts) {
- this.isNew = false;
- this.$init = true;
-
- // handle docs with populated paths
- // If doc._id is not null or undefined
- if (doc._id !== null && doc._id !== undefined &&
- opts && opts.populated && opts.populated.length) {
- const id = String(doc._id);
- for (let i = 0; i < opts.populated.length; ++i) {
- const item = opts.populated[i];
- if (item.isVirtual) {
- this.populated(item.path, utils.getValue(item.path, doc), item);
- } else {
- this.populated(item.path, item._docs[id], item);
- }
- }
- }
-
- init(this, doc, this._doc);
-
- this.emit('init', this);
- this.constructor.emit('init', this);
-
- this.$__._id = this._id;
-
- return this;
- };
-
- /*!
- * Init helper.
- *
- * @param {Object} self document instance
- * @param {Object} obj raw mongodb doc
- * @param {Object} doc object we are initializing
- * @api private
- */
-
- function init(self, obj, doc, prefix) {
- prefix = prefix || '';
-
- const keys = Object.keys(obj);
- const len = keys.length;
- let schema;
- let path;
- let i;
- let index = 0;
-
- while (index < len) {
- _init(index++);
- }
-
- function _init(index) {
- i = keys[index];
- path = prefix + i;
- schema = self.schema.path(path);
-
- // Should still work if not a model-level discriminator, but should not be
- // necessary. This is *only* to catch the case where we queried using the
- // base model and the discriminated model has a projection
- if (self.schema.$isRootDiscriminator && !self.isSelected(path)) {
- return;
- }
-
- if (!schema && utils.isPOJO(obj[i])) {
- // assume nested object
- if (!doc[i]) {
- doc[i] = {};
- }
- init(self, obj[i], doc[i], path + '.');
- } else if (!schema) {
- doc[i] = obj[i];
- } else {
- if (obj[i] === null) {
- doc[i] = null;
- } else if (obj[i] !== undefined) {
- const intCache = obj[i].$__ || {};
- const wasPopulated = intCache.wasPopulated || null;
- if (schema && !wasPopulated) {
- try {
- doc[i] = schema.cast(obj[i], self, true);
- } catch (e) {
- self.invalidate(e.path, new ValidatorError({
- path: e.path,
- message: e.message,
- type: 'cast',
- value: e.value
- }));
- }
- } else {
- doc[i] = obj[i];
- }
- }
- // mark as hydrated
- if (!self.isModified(path)) {
- self.$__.activePaths.init(path);
- }
- }
- }
- }
-
- /**
- * Sends an update command with this document `_id` as the query selector.
- *
- * ####Example:
- *
- * weirdCar.update({$inc: {wheels:1}}, { w: 1 }, callback);
- *
- * ####Valid options:
- *
- * - same as in [Model.update](#model_Model.update)
- *
- * @see Model.update #model_Model.update
- * @param {Object} doc
- * @param {Object} options
- * @param {Function} callback
- * @return {Query}
- * @api public
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.update = function update() {
- const args = utils.args(arguments);
- args.unshift({_id: this._id});
- const query = this.constructor.update.apply(this.constructor, args);
-
- if (this.$session() != null) {
- if (!('session' in query.options)) {
- query.options.session = this.$session();
- }
- }
-
- return query;
- };
-
- /**
- * Sends an updateOne command with this document `_id` as the query selector.
- *
- * ####Example:
- *
- * weirdCar.updateOne({$inc: {wheels:1}}, { w: 1 }, callback);
- *
- * ####Valid options:
- *
- * - same as in [Model.updateOne](#model_Model.updateOne)
- *
- * @see Model.updateOne #model_Model.updateOne
- * @param {Object} doc
- * @param {Object} options
- * @param {Function} callback
- * @return {Query}
- * @api public
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.updateOne = function updateOne(doc, options, callback) {
- const query = this.constructor.updateOne({_id: this._id}, doc, options);
- query._pre(cb => {
- this.constructor._middleware.execPre('updateOne', this, [], cb);
- });
- query._post(cb => {
- this.constructor._middleware.execPost('updateOne', this, [], {}, cb);
- });
-
- if (this.$session() != null) {
- if (!('session' in query.options)) {
- query.options.session = this.$session();
- }
- }
-
- if (callback != null) {
- return query.exec(callback);
- }
-
- return query;
- };
-
- /**
- * Sends a replaceOne command with this document `_id` as the query selector.
- *
- * ####Valid options:
- *
- * - same as in [Model.replaceOne](#model_Model.replaceOne)
- *
- * @see Model.replaceOne #model_Model.replaceOne
- * @param {Object} doc
- * @param {Object} options
- * @param {Function} callback
- * @return {Query}
- * @api public
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.replaceOne = function replaceOne() {
- const args = utils.args(arguments);
- args.unshift({ _id: this._id });
- return this.constructor.replaceOne.apply(this.constructor, args);
- };
-
- /**
- * Getter/setter around the session associated with this document. Used to
- * automatically set `session` if you `save()` a doc that you got from a
- * query with an associated session.
- *
- * ####Example:
- *
- * const session = MyModel.startSession();
- * const doc = await MyModel.findOne().session(session);
- * doc.$session() === session; // true
- * doc.$session(null);
- * doc.$session() === null; // true
- *
- * If this is a top-level document, setting the session propagates to all child
- * docs.
- *
- * @param {ClientSession} [session] overwrite the current session
- * @return {ClientSession}
- * @method $session
- * @api public
- * @memberOf Document
- */
-
- Document.prototype.$session = function $session(session) {
- if (arguments.length === 0) {
- return this.$__.session;
- }
- this.$__.session = session;
-
- if (!this.ownerDocument) {
- const subdocs = this.$__getAllSubdocs();
- for (const child of subdocs) {
- child.$session(session);
- }
- }
-
- return session;
- };
-
- /**
- * Overwrite all values in this document with the values of `obj`, except
- * for immutable properties. Behaves similarly to `set()`, except for it
- * unsets all properties that aren't in `obj`.
- *
- * @param {Object} obj the object to overwrite this document with
- * @method overwrite
- * @name overwrite
- * @memberOf Document
- * @instance
- * @api public
- */
-
- Document.prototype.overwrite = function overwrite(obj) {
- const keys = Array.from(new Set(Object.keys(this._doc).concat(Object.keys(obj))));
-
- for (const key of keys) {
- if (key === '_id') {
- continue;
- }
- // Explicitly skip version key
- if (this.schema.options.versionKey && key === this.schema.options.versionKey) {
- continue;
- }
- this.$set(key, obj[key]);
- }
-
- return this;
- };
-
- /**
- * Alias for `set()`, used internally to avoid conflicts
- *
- * @param {String|Object} path path or object of key/vals to set
- * @param {Any} val the value to set
- * @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for "on-the-fly" attributes
- * @param {Object} [options] optionally specify options that modify the behavior of the set
- * @method $set
- * @name $set
- * @memberOf Document
- * @instance
- * @api public
- */
-
- Document.prototype.$set = function $set(path, val, type, options) {
- if (utils.isPOJO(type)) {
- options = type;
- type = undefined;
- }
-
- options = options || {};
- const merge = options.merge;
- const adhoc = type && type !== true;
- const constructing = type === true;
- let adhocs;
- let keys;
- let i = 0;
- let pathtype;
- let key;
- let prefix;
-
- const strict = 'strict' in options
- ? options.strict
- : this.$__.strictMode;
-
- if (adhoc) {
- adhocs = this.$__.adhocPaths || (this.$__.adhocPaths = {});
- adhocs[path] = this.schema.interpretAsType(path, type, this.schema.options);
- }
-
- if (typeof path !== 'string') {
- // new Document({ key: val })
- if (path === null || path === void 0) {
- const _ = path;
- path = val;
- val = _;
- } else {
- prefix = val ? val + '.' : '';
-
- if (path instanceof Document) {
- if (path.$__isNested) {
- path = path.toObject();
- } else {
- path = path._doc;
- }
- }
-
- keys = Object.keys(path);
- const len = keys.length;
-
- if (len === 0 && !this.schema.options.minimize) {
- if (val) {
- this.$set(val, {});
- }
- return this;
- }
-
- while (i < len) {
- _handleIndex.call(this, i++);
- }
-
- return this;
- }
- } else {
- this.$__.$setCalled.add(path);
- }
-
- function _handleIndex(i) {
- key = keys[i];
- const pathName = prefix + key;
- pathtype = this.schema.pathType(pathName);
-
- // On initial set, delete any nested keys if we're going to overwrite
- // them to ensure we keep the user's key order.
- if (type === true &&
- !prefix &&
- path[key] != null &&
- pathtype === 'nested' &&
- this._doc[key] != null &&
- Object.keys(this._doc[key]).length === 0) {
- delete this._doc[key];
- }
-
- if (typeof path[key] === 'object' &&
- !utils.isNativeObject(path[key]) &&
- path[key] != null &&
- pathtype !== 'virtual' &&
- pathtype !== 'real' &&
- !(this.$__path(pathName) instanceof MixedSchema) &&
- !(this.schema.paths[pathName] &&
- this.schema.paths[pathName].options &&
- this.schema.paths[pathName].options.ref)) {
- this.$__.$setCalled.add(prefix + key);
- this.$set(path[key], prefix + key, constructing);
- } else if (strict) {
- // Don't overwrite defaults with undefined keys (gh-3981)
- if (constructing && path[key] === void 0 &&
- this.get(key) !== void 0) {
- return;
- }
-
- if (pathtype === 'adhocOrUndefined') {
- pathtype = getEmbeddedDiscriminatorPath(this, pathName, { typeOnly: true });
- }
-
- if (pathtype === 'real' || pathtype === 'virtual') {
- // Check for setting single embedded schema to document (gh-3535)
- let p = path[key];
- if (this.schema.paths[pathName] &&
- this.schema.paths[pathName].$isSingleNested &&
- path[key] instanceof Document) {
- p = p.toObject({ virtuals: false, transform: false });
- }
- this.$set(prefix + key, p, constructing);
- } else if (pathtype === 'nested' && path[key] instanceof Document) {
- this.$set(prefix + key,
- path[key].toObject({transform: false}), constructing);
- } else if (strict === 'throw') {
- if (pathtype === 'nested') {
- throw new ObjectExpectedError(key, path[key]);
- } else {
- throw new StrictModeError(key);
- }
- }
- } else if (path[key] !== void 0) {
- this.$set(prefix + key, path[key], constructing);
- }
- }
-
- let pathType = this.schema.pathType(path);
- if (pathType === 'adhocOrUndefined') {
- pathType = getEmbeddedDiscriminatorPath(this, path, { typeOnly: true });
- }
-
- // Assume this is a Mongoose document that was copied into a POJO using
- // `Object.assign()` or `{...doc}`
- if (utils.isPOJO(val) && val.$__ != null && val._doc != null) {
- val = val._doc;
- }
-
- if (pathType === 'nested' && val) {
- if (typeof val === 'object' && val != null) {
- if (!merge) {
- this.$__setValue(path, null);
- cleanModifiedSubpaths(this, path);
- } else {
- return this.$set(val, path, constructing);
- }
-
- const keys = Object.keys(val);
- this.$__setValue(path, {});
- for (const key of keys) {
- this.$set(path + '.' + key, val[key], constructing);
- }
- this.markModified(path);
- cleanModifiedSubpaths(this, path, { skipDocArrays: true });
- return this;
- }
- this.invalidate(path, new MongooseError.CastError('Object', val, path));
- return this;
- }
-
- let schema;
- const parts = path.split('.');
-
- // Might need to change path for top-level alias
- if (typeof this.schema.aliases[parts[0]] == 'string') {
- parts[0] = this.schema.aliases[parts[0]];
- }
-
- if (pathType === 'adhocOrUndefined' && strict) {
- // check for roots that are Mixed types
- let mixed;
-
- for (i = 0; i < parts.length; ++i) {
- const subpath = parts.slice(0, i + 1).join('.');
-
- // If path is underneath a virtual, bypass everything and just set it.
- if (i + 1 < parts.length && this.schema.pathType(subpath) === 'virtual') {
- mpath.set(path, val, this);
- return this;
- }
-
- schema = this.schema.path(subpath);
- if (schema == null) {
- continue;
- }
-
- if (schema instanceof MixedSchema) {
- // allow changes to sub paths of mixed types
- mixed = true;
- break;
- }
- }
-
- if (schema == null) {
- // Check for embedded discriminators
- schema = getEmbeddedDiscriminatorPath(this, path);
- }
-
- if (!mixed && !schema) {
- if (strict === 'throw') {
- throw new StrictModeError(path);
- }
- return this;
- }
- } else if (pathType === 'virtual') {
- schema = this.schema.virtualpath(path);
- schema.applySetters(val, this);
- return this;
- } else {
- schema = this.$__path(path);
- }
-
- // gh-4578, if setting a deeply nested path that doesn't exist yet, create it
- let cur = this._doc;
- let curPath = '';
- for (i = 0; i < parts.length - 1; ++i) {
- cur = cur[parts[i]];
- curPath += (curPath.length > 0 ? '.' : '') + parts[i];
- if (!cur) {
- this.$set(curPath, {});
- // Hack re: gh-5800. If nested field is not selected, it probably exists
- // so `MongoError: cannot use the part (nested of nested.num) to
- // traverse the element ({nested: null})` is not likely. If user gets
- // that error, its their fault for now. We should reconsider disallowing
- // modifying not selected paths for 6.x
- if (!this.isSelected(curPath)) {
- this.unmarkModified(curPath);
- }
- cur = this.$__getValue(curPath);
- }
- }
-
- let pathToMark;
-
- // When using the $set operator the path to the field must already exist.
- // Else mongodb throws: "LEFT_SUBFIELD only supports Object"
-
- if (parts.length <= 1) {
- pathToMark = path;
- } else {
- for (i = 0; i < parts.length; ++i) {
- const subpath = parts.slice(0, i + 1).join('.');
- if (this.get(subpath) === null) {
- pathToMark = subpath;
- break;
- }
- }
-
- if (!pathToMark) {
- pathToMark = path;
- }
- }
-
- // if this doc is being constructed we should not trigger getters
- const priorVal = (() => {
- if (this.$__.$options.priorDoc != null) {
- return this.$__.$options.priorDoc.$__getValue(path);
- }
- if (constructing) {
- return void 0;
- }
- return this.$__getValue(path);
- })();
-
- if (!schema) {
- this.$__set(pathToMark, path, constructing, parts, schema, val, priorVal);
- return this;
- }
-
- let shouldSet = true;
- try {
- // If the user is trying to set a ref path to a document with
- // the correct model name, treat it as populated
- const refMatches = (() => {
- if (schema.options == null) {
- return false;
- }
- if (!(val instanceof Document)) {
- return false;
- }
- const model = val.constructor;
-
- // Check ref
- const ref = schema.options.ref;
- if (ref != null && (ref === model.modelName || ref === model.baseModelName)) {
- return true;
- }
-
- // Check refPath
- const refPath = schema.options.refPath;
- if (refPath == null) {
- return false;
- }
- const modelName = val.get(refPath);
- if (modelName === model.modelName || modelName === model.baseModelName) {
- return true;
- }
- return false;
- })();
-
- let didPopulate = false;
- if (refMatches && val instanceof Document) {
- if (this.ownerDocument) {
- this.ownerDocument().populated(this.$__fullPath(path),
- val._id, { [populateModelSymbol]: val.constructor });
- } else {
- this.populated(path, val._id, { [populateModelSymbol]: val.constructor });
- }
- didPopulate = true;
- }
-
- let popOpts;
- if (schema.options &&
- Array.isArray(schema.options[this.schema.options.typeKey]) &&
- schema.options[this.schema.options.typeKey].length &&
- schema.options[this.schema.options.typeKey][0].ref &&
- Array.isArray(val) &&
- val.length > 0 &&
- val[0] instanceof Document &&
- val[0].constructor.modelName &&
- (schema.options[this.schema.options.typeKey][0].ref === val[0].constructor.baseModelName || schema.options[this.schema.options.typeKey][0].ref === val[0].constructor.modelName)) {
- if (this.ownerDocument) {
- popOpts = { [populateModelSymbol]: val[0].constructor };
- this.ownerDocument().populated(this.$__fullPath(path),
- val.map(function(v) { return v._id; }), popOpts);
- } else {
- popOpts = { [populateModelSymbol]: val[0].constructor };
- this.populated(path, val.map(function(v) { return v._id; }), popOpts);
- }
- didPopulate = true;
- }
-
- // If this path is underneath a single nested schema, we'll call the setter
- // later in `$__set()` because we don't take `_doc` when we iterate through
- // a single nested doc. That's to make sure we get the correct context.
- // Otherwise we would double-call the setter, see gh-7196.
- if (this.schema.singleNestedPaths[path] == null) {
- val = schema.applySetters(val, this, false, priorVal);
- }
-
- if (!didPopulate && this.$__.populated) {
- delete this.$__.populated[path];
- }
-
- this.$markValid(path);
- } catch (e) {
- this.invalidate(path,
- new MongooseError.CastError(schema.instance, val, path, e));
- shouldSet = false;
- }
-
- if (shouldSet) {
- this.$__set(pathToMark, path, constructing, parts, schema, val, priorVal);
- }
-
- if (schema.$isSingleNested && (this.isDirectModified(path) || val == null)) {
- cleanModifiedSubpaths(this, path);
- }
-
- return this;
- };
-
- /**
- * Sets the value of a path, or many paths.
- *
- * ####Example:
- *
- * // path, value
- * doc.set(path, value)
- *
- * // object
- * doc.set({
- * path : value
- * , path2 : {
- * path : value
- * }
- * })
- *
- * // on-the-fly cast to number
- * doc.set(path, value, Number)
- *
- * // on-the-fly cast to string
- * doc.set(path, value, String)
- *
- * // changing strict mode behavior
- * doc.set(path, value, { strict: false });
- *
- * @param {String|Object} path path or object of key/vals to set
- * @param {Any} val the value to set
- * @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for "on-the-fly" attributes
- * @param {Object} [options] optionally specify options that modify the behavior of the set
- * @api public
- * @method set
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.set = Document.prototype.$set;
-
- /**
- * Determine if we should mark this change as modified.
- *
- * @return {Boolean}
- * @api private
- * @method $__shouldModify
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__shouldModify = function(pathToMark, path, constructing, parts, schema, val, priorVal) {
- if (this.isNew) {
- return true;
- }
-
- // Re: the note about gh-7196, `val` is the raw value without casting or
- // setters if the full path is under a single nested subdoc because we don't
- // want to double run setters. So don't set it as modified. See gh-7264.
- if (this.schema.singleNestedPaths[path] != null) {
- return false;
- }
-
- if (val === void 0 && !this.isSelected(path)) {
- // when a path is not selected in a query, its initial
- // value will be undefined.
- return true;
- }
-
- if (val === void 0 && path in this.$__.activePaths.states.default) {
- // we're just unsetting the default value which was never saved
- return false;
- }
-
- // gh-3992: if setting a populated field to a doc, don't mark modified
- // if they have the same _id
- if (this.populated(path) &&
- val instanceof Document &&
- deepEqual(val._id, priorVal)) {
- return false;
- }
-
- if (!deepEqual(val, priorVal || this.get(path))) {
- return true;
- }
-
- if (!constructing &&
- val !== null &&
- val !== undefined &&
- path in this.$__.activePaths.states.default &&
- deepEqual(val, schema.getDefault(this, constructing))) {
- // a path with a default was $unset on the server
- // and the user is setting it to the same value again
- return true;
- }
- return false;
- };
-
- /**
- * Handles the actual setting of the value and marking the path modified if appropriate.
- *
- * @api private
- * @method $__set
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__set = function(pathToMark, path, constructing, parts, schema, val, priorVal) {
- Embedded = Embedded || require('./types/embedded');
-
- const shouldModify = this.$__shouldModify(pathToMark, path, constructing, parts,
- schema, val, priorVal);
- const _this = this;
-
- if (shouldModify) {
- this.markModified(pathToMark);
-
- // handle directly setting arrays (gh-1126)
- MongooseArray || (MongooseArray = require('./types/array'));
- if (val && val.isMongooseArray) {
- val._registerAtomic('$set', val);
-
- // Update embedded document parent references (gh-5189)
- if (val.isMongooseDocumentArray) {
- val.forEach(function(item) {
- item && item.__parentArray && (item.__parentArray = val);
- });
- }
-
- // Small hack for gh-1638: if we're overwriting the entire array, ignore
- // paths that were modified before the array overwrite
- this.$__.activePaths.forEach(function(modifiedPath) {
- if (modifiedPath.startsWith(path + '.')) {
- _this.$__.activePaths.ignore(modifiedPath);
- }
- });
- }
- }
-
- let obj = this._doc;
- let i = 0;
- const l = parts.length;
- let cur = '';
-
- for (; i < l; i++) {
- const next = i + 1;
- const last = next === l;
- cur += (cur ? '.' + parts[i] : parts[i]);
- if (specialProperties.has(parts[i])) {
- return;
- }
-
- if (last) {
- if (obj instanceof Map) {
- obj.set(parts[i], val);
- } else {
- obj[parts[i]] = val;
- }
- } else {
- if (utils.isPOJO(obj[parts[i]])) {
- obj = obj[parts[i]];
- } else if (obj[parts[i]] && obj[parts[i]] instanceof Embedded) {
- obj = obj[parts[i]];
- } else if (obj[parts[i]] && obj[parts[i]].$isSingleNested) {
- obj = obj[parts[i]];
- } else if (obj[parts[i]] && Array.isArray(obj[parts[i]])) {
- obj = obj[parts[i]];
- } else {
- obj[parts[i]] = obj[parts[i]] || {};
- obj = obj[parts[i]];
- }
- }
- }
- };
-
- /**
- * Gets a raw value from a path (no getters)
- *
- * @param {String} path
- * @api private
- */
-
- Document.prototype.$__getValue = function(path) {
- return utils.getValue(path, this._doc);
- };
-
- /**
- * Sets a raw value for a path (no casting, setters, transformations)
- *
- * @param {String} path
- * @param {Object} value
- * @api private
- */
-
- Document.prototype.$__setValue = function(path, val) {
- utils.setValue(path, val, this._doc);
- return this;
- };
-
- /**
- * Returns the value of a path.
- *
- * ####Example
- *
- * // path
- * doc.get('age') // 47
- *
- * // dynamic casting to a string
- * doc.get('age', String) // "47"
- *
- * @param {String} path
- * @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for on-the-fly attributes
- * @param {Object} [options]
- * @param {Boolean} [options.virtuals=false] Apply virtuals before getting this path
- * @param {Boolean} [options.getters=true] If false, skip applying getters and just get the raw value
- * @api public
- */
-
- Document.prototype.get = function(path, type, options) {
- let adhoc;
- options = options || {};
- if (type) {
- adhoc = this.schema.interpretAsType(path, type, this.schema.options);
- }
-
- const schema = this.$__path(path) || this.schema.virtualpath(path);
- const pieces = path.split('.');
- let obj = this._doc;
-
- if (schema instanceof VirtualType) {
- if (schema.getters.length === 0) {
- return void 0;
- }
- return schema.applyGetters(null, this);
- }
-
- // Might need to change path for top-level alias
- if (typeof this.schema.aliases[pieces[0]] == 'string') {
- pieces[0] = this.schema.aliases[pieces[0]];
- }
-
- for (let i = 0, l = pieces.length; i < l; i++) {
- if (obj && obj._doc) {
- obj = obj._doc;
- }
-
- if (obj == null) {
- obj = void 0;
- } else if (obj instanceof Map) {
- obj = obj.get(pieces[i]);
- } else if (i === l - 1) {
- obj = utils.getValue(pieces[i], obj);
- } else {
- obj = obj[pieces[i]];
- }
- }
-
- if (adhoc) {
- obj = adhoc.cast(obj);
- }
-
- if (schema != null && options.getters !== false) {
- obj = schema.applyGetters(obj, this);
- } else if (this.schema.nested[path] && options.virtuals) {
- // Might need to apply virtuals if this is a nested path
- return applyGetters(this, utils.clone(obj) || {}, 'virtuals', { path: path });
- }
-
- return obj;
- };
-
- /*!
- * ignore
- */
-
- Document.prototype[getSymbol] = Document.prototype.get;
-
- /**
- * Returns the schematype for the given `path`.
- *
- * @param {String} path
- * @api private
- * @method $__path
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__path = function(path) {
- const adhocs = this.$__.adhocPaths;
- const adhocType = adhocs && adhocs.hasOwnProperty(path) ? adhocs[path] : null;
-
- if (adhocType) {
- return adhocType;
- }
- return this.schema.path(path);
- };
-
- /**
- * Marks the path as having pending changes to write to the db.
- *
- * _Very helpful when using [Mixed](./schematypes.html#mixed) types._
- *
- * ####Example:
- *
- * doc.mixed.type = 'changed';
- * doc.markModified('mixed.type');
- * doc.save() // changes to mixed.type are now persisted
- *
- * @param {String} path the path to mark modified
- * @param {Document} [scope] the scope to run validators with
- * @api public
- */
-
- Document.prototype.markModified = function(path, scope) {
- this.$__.activePaths.modify(path);
- if (scope != null && !this.ownerDocument) {
- this.$__.pathsToScopes[path] = scope;
- }
- };
-
- /**
- * Clears the modified state on the specified path.
- *
- * ####Example:
- *
- * doc.foo = 'bar';
- * doc.unmarkModified('foo');
- * doc.save(); // changes to foo will not be persisted
- *
- * @param {String} path the path to unmark modified
- * @api public
- */
-
- Document.prototype.unmarkModified = function(path) {
- this.$__.activePaths.init(path);
- delete this.$__.pathsToScopes[path];
- };
-
- /**
- * Don't run validation on this path or persist changes to this path.
- *
- * ####Example:
- *
- * doc.foo = null;
- * doc.$ignore('foo');
- * doc.save(); // changes to foo will not be persisted and validators won't be run
- *
- * @memberOf Document
- * @instance
- * @method $ignore
- * @param {String} path the path to ignore
- * @api public
- */
-
- Document.prototype.$ignore = function(path) {
- this.$__.activePaths.ignore(path);
- };
-
- /**
- * Returns the list of paths that have been directly modified. A direct
- * modified path is a path that you explicitly set, whether via `doc.foo = 'bar'`,
- * `Object.assign(doc, { foo: 'bar' })`, or `doc.set('foo', 'bar')`.
- *
- * A path `a` may be in `modifiedPaths()` but not in `directModifiedPaths()`
- * because a child of `a` was directly modified.
- *
- * ####Example
- * const schema = new Schema({ foo: String, nested: { bar: String } });
- * const Model = mongoose.model('Test', schema);
- * await Model.create({ foo: 'original', nested: { bar: 'original' } });
- *
- * const doc = await Model.findOne();
- * doc.nested.bar = 'modified';
- * doc.directModifiedPaths(); // ['nested.bar']
- * doc.modifiedPaths(); // ['nested', 'nested.bar']
- *
- * @return {Array}
- * @api public
- */
-
- Document.prototype.directModifiedPaths = function() {
- return Object.keys(this.$__.activePaths.states.modify);
- };
-
- /**
- * Returns true if the given path is nullish or only contains empty objects.
- * Useful for determining whether this subdoc will get stripped out by the
- * [minimize option](/docs/guide.html#minimize).
- *
- * ####Example:
- * const schema = new Schema({ nested: { foo: String } });
- * const Model = mongoose.model('Test', schema);
- * const doc = new Model({});
- * doc.$isEmpty('nested'); // true
- * doc.nested.$isEmpty(); // true
- *
- * doc.nested.foo = 'bar';
- * doc.$isEmpty('nested'); // false
- * doc.nested.$isEmpty(); // false
- *
- * @memberOf Document
- * @instance
- * @api public
- * @method $isEmpty
- * @return {Boolean}
- */
-
- Document.prototype.$isEmpty = function(path) {
- const isEmptyOptions = {
- minimize: true,
- virtuals: false,
- getters: false,
- transform: false
- };
-
- if (arguments.length > 0) {
- const v = this.get(path);
- if (v == null) {
- return true;
- }
- if (typeof v !== 'object') {
- return false;
- }
- if (utils.isPOJO(v)) {
- return _isEmpty(v);
- }
- return Object.keys(v.toObject(isEmptyOptions)).length === 0;
- }
-
- return Object.keys(this.toObject(isEmptyOptions)).length === 0;
- };
-
- function _isEmpty(v) {
- if (v == null) {
- return true;
- }
- if (typeof v !== 'object' || Array.isArray(v)) {
- return false;
- }
- for (const key of Object.keys(v)) {
- if (!_isEmpty(v[key])) {
- return false;
- }
- }
- return true;
- }
-
- /**
- * Returns the list of paths that have been modified.
- *
- * @param {Object} [options]
- * @param {Boolean} [options.includeChildren=false] if true, returns children of modified paths as well. For example, if false, the list of modified paths for `doc.colors = { primary: 'blue' };` will **not** contain `colors.primary`. If true, `modifiedPaths()` will return an array that contains `colors.primary`.
- * @return {Array}
- * @api public
- */
-
- Document.prototype.modifiedPaths = function(options) {
- options = options || {};
- const directModifiedPaths = Object.keys(this.$__.activePaths.states.modify);
- const _this = this;
- return directModifiedPaths.reduce(function(list, path) {
- const parts = path.split('.');
- list = list.concat(parts.reduce(function(chains, part, i) {
- return chains.concat(parts.slice(0, i).concat(part).join('.'));
- }, []).filter(function(chain) {
- return (list.indexOf(chain) === -1);
- }));
-
- if (!options.includeChildren) {
- return list;
- }
-
- let cur = _this.get(path);
- if (cur != null && typeof cur === 'object') {
- if (cur._doc) {
- cur = cur._doc;
- }
- if (Array.isArray(cur)) {
- const len = cur.length;
- for (let i = 0; i < len; ++i) {
- if (list.indexOf(path + '.' + i) === -1) {
- list.push(path + '.' + i);
- if (cur[i] != null && cur[i].$__) {
- const modified = cur[i].modifiedPaths();
- for (const childPath of modified) {
- list.push(path + '.' + i + '.' + childPath);
- }
- }
- }
- }
- } else {
- Object.keys(cur).
- filter(function(key) {
- return list.indexOf(path + '.' + key) === -1;
- }).
- forEach(function(key) {
- list.push(path + '.' + key);
- });
- }
- }
-
- return list;
- }, []);
- };
-
- /**
- * Returns true if this document was modified, else false.
- *
- * If `path` is given, checks if a path or any full path containing `path` as part of its path chain has been modified.
- *
- * ####Example
- *
- * doc.set('documents.0.title', 'changed');
- * doc.isModified() // true
- * doc.isModified('documents') // true
- * doc.isModified('documents.0.title') // true
- * doc.isModified('documents otherProp') // true
- * doc.isDirectModified('documents') // false
- *
- * @param {String} [path] optional
- * @return {Boolean}
- * @api public
- */
-
- Document.prototype.isModified = function(paths, modifiedPaths) {
- if (paths) {
- if (!Array.isArray(paths)) {
- paths = paths.split(' ');
- }
- const modified = modifiedPaths || this.modifiedPaths();
- const directModifiedPaths = Object.keys(this.$__.activePaths.states.modify);
- const isModifiedChild = paths.some(function(path) {
- return !!~modified.indexOf(path);
- });
- return isModifiedChild || paths.some(function(path) {
- return directModifiedPaths.some(function(mod) {
- return mod === path || path.startsWith(mod + '.');
- });
- });
- }
- return this.$__.activePaths.some('modify');
- };
-
- /**
- * Checks if a path is set to its default.
- *
- * ####Example
- *
- * MyModel = mongoose.model('test', { name: { type: String, default: 'Val '} });
- * var m = new MyModel();
- * m.$isDefault('name'); // true
- *
- * @memberOf Document
- * @instance
- * @method $isDefault
- * @param {String} [path]
- * @return {Boolean}
- * @api public
- */
-
- Document.prototype.$isDefault = function(path) {
- return (path in this.$__.activePaths.states.default);
- };
-
- /**
- * Getter/setter, determines whether the document was removed or not.
- *
- * ####Example:
- * product.remove(function (err, product) {
- * product.$isDeleted(); // true
- * product.remove(); // no-op, doesn't send anything to the db
- *
- * product.$isDeleted(false);
- * product.$isDeleted(); // false
- * product.remove(); // will execute a remove against the db
- * })
- *
- * @param {Boolean} [val] optional, overrides whether mongoose thinks the doc is deleted
- * @return {Boolean} whether mongoose thinks this doc is deleted.
- * @method $isDeleted
- * @memberOf Document
- * @instance
- * @api public
- */
-
- Document.prototype.$isDeleted = function(val) {
- if (arguments.length === 0) {
- return !!this.$__.isDeleted;
- }
-
- this.$__.isDeleted = !!val;
- return this;
- };
-
- /**
- * Returns true if `path` was directly set and modified, else false.
- *
- * ####Example
- *
- * doc.set('documents.0.title', 'changed');
- * doc.isDirectModified('documents.0.title') // true
- * doc.isDirectModified('documents') // false
- *
- * @param {String} path
- * @return {Boolean}
- * @api public
- */
-
- Document.prototype.isDirectModified = function(path) {
- return (path in this.$__.activePaths.states.modify);
- };
-
- /**
- * Checks if `path` was initialized.
- *
- * @param {String} path
- * @return {Boolean}
- * @api public
- */
-
- Document.prototype.isInit = function(path) {
- return (path in this.$__.activePaths.states.init);
- };
-
- /**
- * Checks if `path` was selected in the source query which initialized this document.
- *
- * ####Example
- *
- * Thing.findOne().select('name').exec(function (err, doc) {
- * doc.isSelected('name') // true
- * doc.isSelected('age') // false
- * })
- *
- * @param {String} path
- * @return {Boolean}
- * @api public
- */
-
- Document.prototype.isSelected = function isSelected(path) {
- if (this.$__.selected) {
- if (path === '_id') {
- return this.$__.selected._id !== 0;
- }
-
- const paths = Object.keys(this.$__.selected);
- let i = paths.length;
- let inclusive = null;
- let cur;
-
- if (i === 1 && paths[0] === '_id') {
- // only _id was selected.
- return this.$__.selected._id === 0;
- }
-
- while (i--) {
- cur = paths[i];
- if (cur === '_id') {
- continue;
- }
- if (!isDefiningProjection(this.$__.selected[cur])) {
- continue;
- }
- inclusive = !!this.$__.selected[cur];
- break;
- }
-
- if (inclusive === null) {
- return true;
- }
-
- if (path in this.$__.selected) {
- return inclusive;
- }
-
- i = paths.length;
- const pathDot = path + '.';
-
- while (i--) {
- cur = paths[i];
- if (cur === '_id') {
- continue;
- }
-
- if (cur.startsWith(pathDot)) {
- return inclusive || cur !== pathDot;
- }
-
- if (pathDot.startsWith(cur + '.')) {
- return inclusive;
- }
- }
-
- return !inclusive;
- }
-
- return true;
- };
-
- /**
- * Checks if `path` was explicitly selected. If no projection, always returns
- * true.
- *
- * ####Example
- *
- * Thing.findOne().select('nested.name').exec(function (err, doc) {
- * doc.isDirectSelected('nested.name') // true
- * doc.isDirectSelected('nested.otherName') // false
- * doc.isDirectSelected('nested') // false
- * })
- *
- * @param {String} path
- * @return {Boolean}
- * @api public
- */
-
- Document.prototype.isDirectSelected = function isDirectSelected(path) {
- if (this.$__.selected) {
- if (path === '_id') {
- return this.$__.selected._id !== 0;
- }
-
- const paths = Object.keys(this.$__.selected);
- let i = paths.length;
- let inclusive = null;
- let cur;
-
- if (i === 1 && paths[0] === '_id') {
- // only _id was selected.
- return this.$__.selected._id === 0;
- }
-
- while (i--) {
- cur = paths[i];
- if (cur === '_id') {
- continue;
- }
- if (!isDefiningProjection(this.$__.selected[cur])) {
- continue;
- }
- inclusive = !!this.$__.selected[cur];
- break;
- }
-
- if (inclusive === null) {
- return true;
- }
-
- if (path in this.$__.selected) {
- return inclusive;
- }
-
- return !inclusive;
- }
-
- return true;
- };
-
- /**
- * Executes registered validation rules for this document.
- *
- * ####Note:
- *
- * This method is called `pre` save and if a validation rule is violated, [save](#model_Model-save) is aborted and the error is returned to your `callback`.
- *
- * ####Example:
- *
- * doc.validate(function (err) {
- * if (err) handleError(err);
- * else // validation passed
- * });
- *
- * @param {Object} optional options internal options
- * @param {Function} callback optional callback called after validation completes, passing an error if one occurred
- * @return {Promise} Promise
- * @api public
- */
-
- Document.prototype.validate = function(options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = null;
- }
-
- return utils.promiseOrCallback(callback, cb => this.$__validate(options, function(error) {
- cb(error);
- }), this.constructor.events);
- };
-
- /*!
- * ignore
- */
-
- function _evaluateRequiredFunctions(doc) {
- Object.keys(doc.$__.activePaths.states.require).forEach(path => {
- const p = doc.schema.path(path);
-
- if (p != null && typeof p.originalRequiredValue === 'function') {
- doc.$__.cachedRequired[path] = p.originalRequiredValue.call(doc);
- }
- });
- }
-
- /*!
- * ignore
- */
-
- function _getPathsToValidate(doc) {
- let i;
- let len;
- const skipSchemaValidators = {};
-
- _evaluateRequiredFunctions(doc);
-
- // only validate required fields when necessary
- let paths = Object.keys(doc.$__.activePaths.states.require).filter(function(path) {
- if (!doc.isSelected(path) && !doc.isModified(path)) {
- return false;
- }
- if (path in doc.$__.cachedRequired) {
- return doc.$__.cachedRequired[path];
- }
- return true;
- });
-
- paths = paths.concat(Object.keys(doc.$__.activePaths.states.init));
- paths = paths.concat(Object.keys(doc.$__.activePaths.states.modify));
- paths = paths.concat(Object.keys(doc.$__.activePaths.states.default));
-
- if (!doc.ownerDocument) {
- const subdocs = doc.$__getAllSubdocs();
- let subdoc;
- len = subdocs.length;
- const modifiedPaths = doc.modifiedPaths();
- for (i = 0; i < len; ++i) {
- subdoc = subdocs[i];
- if (doc.isModified(subdoc.$basePath, modifiedPaths) &&
- !doc.isDirectModified(subdoc.$basePath) &&
- !doc.$isDefault(subdoc.$basePath)) {
- // Remove child paths for now, because we'll be validating the whole
- // subdoc
- paths = paths.filter(function(p) {
- return p != null && p.indexOf(subdoc.$basePath + '.') !== 0;
- });
- paths.push(subdoc.$basePath);
- skipSchemaValidators[subdoc.$basePath] = true;
- }
- }
- }
-
- // gh-661: if a whole array is modified, make sure to run validation on all
- // the children as well
- len = paths.length;
- for (i = 0; i < len; ++i) {
- const path = paths[i];
-
- const _pathType = doc.schema.path(path);
- if (!_pathType ||
- !_pathType.$isMongooseArray ||
- // To avoid potential performance issues, skip doc arrays whose children
- // are not required. `getPositionalPathType()` may be slow, so avoid
- // it unless we have a case of #6364
- (_pathType.$isMongooseDocumentArray && !get(_pathType, 'schemaOptions.required'))) {
- continue;
- }
-
- const val = doc.$__getValue(path);
- if (val) {
- const numElements = val.length;
- for (let j = 0; j < numElements; ++j) {
- paths.push(path + '.' + j);
- }
- }
- }
-
- const flattenOptions = { skipArrays: true };
- len = paths.length;
- for (i = 0; i < len; ++i) {
- const pathToCheck = paths[i];
- if (doc.schema.nested[pathToCheck]) {
- let _v = doc.$__getValue(pathToCheck);
- if (isMongooseObject(_v)) {
- _v = _v.toObject({ transform: false });
- }
- const flat = flatten(_v, '', flattenOptions);
- const _subpaths = Object.keys(flat).map(function(p) {
- return pathToCheck + '.' + p;
- });
- paths = paths.concat(_subpaths);
- }
- }
-
- len = paths.length;
- for (i = 0; i < len; ++i) {
- const path = paths[i];
- const _pathType = doc.schema.path(path);
- if (!_pathType || !_pathType.$isSchemaMap) {
- continue;
- }
-
- const val = doc.$__getValue(path);
- if (val == null) {
- continue;
- }
- for (const key of val.keys()) {
- paths.push(path + '.' + key);
- }
- }
-
- return [paths, skipSchemaValidators];
- }
-
- /*!
- * ignore
- */
-
- Document.prototype.$__validate = function(options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = null;
- }
-
- const hasValidateModifiedOnlyOption = options &&
- (typeof options === 'object') &&
- ('validateModifiedOnly' in options);
-
- let shouldValidateModifiedOnly;
- if (hasValidateModifiedOnlyOption) {
- shouldValidateModifiedOnly = !!options.validateModifiedOnly;
- } else {
- shouldValidateModifiedOnly = this.schema.options.validateModifiedOnly;
- }
-
- const _this = this;
- const _complete = () => {
- const err = this.$__.validationError;
- this.$__.validationError = undefined;
- this.$__.cachedRequired = {};
- this.emit('validate', _this);
- this.constructor.emit('validate', _this);
- if (err) {
- for (const key in err.errors) {
- // Make sure cast errors persist
- if (!this[documentArrayParent] && err.errors[key] instanceof MongooseError.CastError) {
- this.invalidate(key, err.errors[key]);
- }
- }
-
- return err;
- }
- };
-
- // only validate required fields when necessary
- const pathDetails = _getPathsToValidate(this);
- const paths = shouldValidateModifiedOnly ?
- pathDetails[0].filter((path) => this.isModified(path)) :
- pathDetails[0];
- const skipSchemaValidators = pathDetails[1];
-
- if (paths.length === 0) {
- return process.nextTick(function() {
- const error = _complete();
- if (error) {
- return _this.schema.s.hooks.execPost('validate:error', _this, [ _this], { error: error }, function(error) {
- callback(error);
- });
- }
- callback(null, _this);
- });
- }
-
- const validated = {};
- let total = 0;
-
- const complete = function() {
- const error = _complete();
- if (error) {
- return _this.schema.s.hooks.execPost('validate:error', _this, [ _this], { error: error }, function(error) {
- callback(error);
- });
- }
- callback(null, _this);
- };
-
- const validatePath = function(path) {
- if (path == null || validated[path]) {
- return;
- }
-
- validated[path] = true;
- total++;
-
- process.nextTick(function() {
- const p = _this.schema.path(path);
-
- if (!p) {
- return --total || complete();
- }
-
- // If user marked as invalid or there was a cast error, don't validate
- if (!_this.$isValid(path)) {
- --total || complete();
- return;
- }
-
- const val = _this.$__getValue(path);
- const scope = path in _this.$__.pathsToScopes ?
- _this.$__.pathsToScopes[path] :
- _this;
-
- p.doValidate(val, function(err) {
- if (err && (!p.$isMongooseDocumentArray || err.$isArrayValidatorError)) {
- if (p.$isSingleNested &&
- err.name === 'ValidationError' &&
- p.schema.options.storeSubdocValidationError === false) {
- return --total || complete();
- }
- _this.invalidate(path, err, undefined, true);
- }
- --total || complete();
- }, scope, { skipSchemaValidators: skipSchemaValidators[path], path: path });
- });
- };
-
- const numPaths = paths.length;
- for (let i = 0; i < numPaths; ++i) {
- validatePath(paths[i]);
- }
- };
-
- /**
- * Executes registered validation rules (skipping asynchronous validators) for this document.
- *
- * ####Note:
- *
- * This method is useful if you need synchronous validation.
- *
- * ####Example:
- *
- * var err = doc.validateSync();
- * if ( err ){
- * handleError( err );
- * } else {
- * // validation passed
- * }
- *
- * @param {Array|string} pathsToValidate only validate the given paths
- * @return {ValidationError|undefined} ValidationError if there are errors during validation, or undefined if there is no error.
- * @api public
- */
-
- Document.prototype.validateSync = function(pathsToValidate, options) {
- const _this = this;
-
- const hasValidateModifiedOnlyOption = options &&
- (typeof options === 'object') &&
- ('validateModifiedOnly' in options);
-
- let shouldValidateModifiedOnly;
- if (hasValidateModifiedOnlyOption) {
- shouldValidateModifiedOnly = !!options.validateModifiedOnly;
- } else {
- shouldValidateModifiedOnly = this.schema.options.validateModifiedOnly;
- }
-
- if (typeof pathsToValidate === 'string') {
- pathsToValidate = pathsToValidate.split(' ');
- }
-
- // only validate required fields when necessary
- const pathDetails = _getPathsToValidate(this);
- let paths = shouldValidateModifiedOnly ?
- pathDetails[0].filter((path) => this.isModified(path)) :
- pathDetails[0];
- const skipSchemaValidators = pathDetails[1];
-
- if (pathsToValidate && pathsToValidate.length) {
- const tmp = [];
- for (let i = 0; i < paths.length; ++i) {
- if (pathsToValidate.indexOf(paths[i]) !== -1) {
- tmp.push(paths[i]);
- }
- }
- paths = tmp;
- }
-
- const validating = {};
-
- paths.forEach(function(path) {
- if (validating[path]) {
- return;
- }
-
- validating[path] = true;
-
- const p = _this.schema.path(path);
- if (!p) {
- return;
- }
- if (!_this.$isValid(path)) {
- return;
- }
-
- const val = _this.$__getValue(path);
- const err = p.doValidateSync(val, _this, {
- skipSchemaValidators: skipSchemaValidators[path],
- path: path
- });
- if (err && (!p.$isMongooseDocumentArray || err.$isArrayValidatorError)) {
- if (p.$isSingleNested &&
- err.name === 'ValidationError' &&
- p.schema.options.storeSubdocValidationError === false) {
- return;
- }
- _this.invalidate(path, err, undefined, true);
- }
- });
-
- const err = _this.$__.validationError;
- _this.$__.validationError = undefined;
- _this.emit('validate', _this);
- _this.constructor.emit('validate', _this);
-
- if (err) {
- for (const key in err.errors) {
- // Make sure cast errors persist
- if (err.errors[key] instanceof MongooseError.CastError) {
- _this.invalidate(key, err.errors[key]);
- }
- }
- }
-
- return err;
- };
-
- /**
- * Marks a path as invalid, causing validation to fail.
- *
- * The `errorMsg` argument will become the message of the `ValidationError`.
- *
- * The `value` argument (if passed) will be available through the `ValidationError.value` property.
- *
- * doc.invalidate('size', 'must be less than 20', 14);
-
- * doc.validate(function (err) {
- * console.log(err)
- * // prints
- * { message: 'Validation failed',
- * name: 'ValidationError',
- * errors:
- * { size:
- * { message: 'must be less than 20',
- * name: 'ValidatorError',
- * path: 'size',
- * type: 'user defined',
- * value: 14 } } }
- * })
- *
- * @param {String} path the field to invalidate
- * @param {String|Error} errorMsg the error which states the reason `path` was invalid
- * @param {Object|String|Number|any} value optional invalid value
- * @param {String} [kind] optional `kind` property for the error
- * @return {ValidationError} the current ValidationError, with all currently invalidated paths
- * @api public
- */
-
- Document.prototype.invalidate = function(path, err, val, kind) {
- if (!this.$__.validationError) {
- this.$__.validationError = new ValidationError(this);
- }
-
- if (this.$__.validationError.errors[path]) {
- return;
- }
-
- if (!err || typeof err === 'string') {
- err = new ValidatorError({
- path: path,
- message: err,
- type: kind || 'user defined',
- value: val
- });
- }
-
- if (this.$__.validationError === err) {
- return this.$__.validationError;
- }
-
- this.$__.validationError.addError(path, err);
- return this.$__.validationError;
- };
-
- /**
- * Marks a path as valid, removing existing validation errors.
- *
- * @param {String} path the field to mark as valid
- * @api public
- * @memberOf Document
- * @instance
- * @method $markValid
- */
-
- Document.prototype.$markValid = function(path) {
- if (!this.$__.validationError || !this.$__.validationError.errors[path]) {
- return;
- }
-
- delete this.$__.validationError.errors[path];
- if (Object.keys(this.$__.validationError.errors).length === 0) {
- this.$__.validationError = null;
- }
- };
-
- /**
- * Saves this document.
- *
- * ####Example:
- *
- * product.sold = Date.now();
- * product.save(function (err, product) {
- * if (err) ..
- * })
- *
- * The callback will receive two parameters
- *
- * 1. `err` if an error occurred
- * 2. `product` which is the saved `product`
- *
- * As an extra measure of flow control, save will return a Promise.
- * ####Example:
- * product.save().then(function(product) {
- * ...
- * });
- *
- * @param {Object} [options] options optional options
- * @param {Object} [options.safe] (DEPRECATED) overrides [schema's safe option](http://mongoosejs.com//docs/guide.html#safe)
- * @param {Boolean} [options.validateBeforeSave] set to false to save without validating.
- * @param {Function} [fn] optional callback
- * @method save
- * @memberOf Document
- * @instance
- * @return {Promise|undefined} Returns undefined if used with callback or a Promise otherwise.
- * @api public
- * @see middleware http://mongoosejs.com/docs/middleware.html
- */
-
- /**
- * Checks if a path is invalid
- *
- * @param {String} path the field to check
- * @method $isValid
- * @memberOf Document
- * @instance
- * @api private
- */
-
- Document.prototype.$isValid = function(path) {
- return !this.$__.validationError || !this.$__.validationError.errors[path];
- };
-
- /**
- * Resets the internal modified state of this document.
- *
- * @api private
- * @return {Document}
- * @method $__reset
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__reset = function reset() {
- let _this = this;
- DocumentArray || (DocumentArray = require('./types/documentarray'));
-
- this.$__.activePaths
- .map('init', 'modify', function(i) {
- return _this.$__getValue(i);
- })
- .filter(function(val) {
- return val && val instanceof Array && val.isMongooseDocumentArray && val.length;
- })
- .forEach(function(array) {
- let i = array.length;
- while (i--) {
- const doc = array[i];
- if (!doc) {
- continue;
- }
- doc.$__reset();
- }
-
- _this.$__.activePaths.init(array.$path());
-
- array[arrayAtomicsSymbol] = {};
- });
-
- this.$__.activePaths.
- map('init', 'modify', function(i) {
- return _this.$__getValue(i);
- }).
- filter(function(val) {
- return val && val.$isSingleNested;
- }).
- forEach(function(doc) {
- doc.$__reset();
- _this.$__.activePaths.init(doc.$basePath);
- });
-
- // clear atomics
- this.$__dirty().forEach(function(dirt) {
- const type = dirt.value;
-
- if (type && type[arrayAtomicsSymbol]) {
- type[arrayAtomicsSymbol] = {};
- }
- });
-
- // Clear 'dirty' cache
- this.$__.activePaths.clear('modify');
- this.$__.activePaths.clear('default');
- this.$__.validationError = undefined;
- this.errors = undefined;
- _this = this;
- this.schema.requiredPaths().forEach(function(path) {
- _this.$__.activePaths.require(path);
- });
-
- return this;
- };
-
- /**
- * Returns this documents dirty paths / vals.
- *
- * @api private
- * @method $__dirty
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__dirty = function() {
- const _this = this;
-
- let all = this.$__.activePaths.map('modify', function(path) {
- return {
- path: path,
- value: _this.$__getValue(path),
- schema: _this.$__path(path)
- };
- });
-
- // gh-2558: if we had to set a default and the value is not undefined,
- // we have to save as well
- all = all.concat(this.$__.activePaths.map('default', function(path) {
- if (path === '_id' || _this.$__getValue(path) == null) {
- return;
- }
- return {
- path: path,
- value: _this.$__getValue(path),
- schema: _this.$__path(path)
- };
- }));
-
- // Sort dirty paths in a flat hierarchy.
- all.sort(function(a, b) {
- return (a.path < b.path ? -1 : (a.path > b.path ? 1 : 0));
- });
-
- // Ignore "foo.a" if "foo" is dirty already.
- const minimal = [];
- let lastPath;
- let top;
-
- all.forEach(function(item) {
- if (!item) {
- return;
- }
- if (lastPath == null || item.path.indexOf(lastPath) !== 0) {
- lastPath = item.path + '.';
- minimal.push(item);
- top = item;
- } else if (top != null &&
- top.value != null &&
- top.value[arrayAtomicsSymbol] != null &&
- top.value.hasAtomics()) {
- // special case for top level MongooseArrays
- // the `top` array itself and a sub path of `top` are being modified.
- // the only way to honor all of both modifications is through a $set
- // of entire array.
- top.value[arrayAtomicsSymbol] = {};
- top.value[arrayAtomicsSymbol].$set = top.value;
- }
- });
-
- top = lastPath = null;
- return minimal;
- };
-
- /**
- * Assigns/compiles `schema` into this documents prototype.
- *
- * @param {Schema} schema
- * @api private
- * @method $__setSchema
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__setSchema = function(schema) {
- schema.plugin(idGetter, { deduplicate: true });
- compile(schema.tree, this, undefined, schema.options);
-
- // Apply default getters if virtual doesn't have any (gh-6262)
- for (const key of Object.keys(schema.virtuals)) {
- schema.virtuals[key]._applyDefaultGetters();
- }
-
- this.schema = schema;
- this[documentSchemaSymbol] = schema;
- };
-
-
- /**
- * Get active path that were changed and are arrays
- *
- * @api private
- * @method $__getArrayPathsToValidate
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__getArrayPathsToValidate = function() {
- DocumentArray || (DocumentArray = require('./types/documentarray'));
-
- // validate all document arrays.
- return this.$__.activePaths
- .map('init', 'modify', function(i) {
- return this.$__getValue(i);
- }.bind(this))
- .filter(function(val) {
- return val && val instanceof Array && val.isMongooseDocumentArray && val.length;
- }).reduce(function(seed, array) {
- return seed.concat(array);
- }, [])
- .filter(function(doc) {
- return doc;
- });
- };
-
-
- /**
- * Get all subdocs (by bfs)
- *
- * @api private
- * @method $__getAllSubdocs
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__getAllSubdocs = function() {
- DocumentArray || (DocumentArray = require('./types/documentarray'));
- Embedded = Embedded || require('./types/embedded');
-
- function docReducer(doc, seed, path) {
- let val = doc;
- if (path) {
- if (doc instanceof Document && doc[documentSchemaSymbol].paths[path]) {
- val = doc._doc[path];
- } else {
- val = doc[path];
- }
- }
- if (val instanceof Embedded) {
- seed.push(val);
- } else if (val instanceof Map) {
- seed = Array.from(val.keys()).reduce(function(seed, path) {
- return docReducer(val.get(path), seed, null);
- }, seed);
- } else if (val && val.$isSingleNested) {
- seed = Object.keys(val._doc).reduce(function(seed, path) {
- return docReducer(val._doc, seed, path);
- }, seed);
- seed.push(val);
- } else if (val && val.isMongooseDocumentArray) {
- val.forEach(function _docReduce(doc) {
- if (!doc || !doc._doc) {
- return;
- }
- if (doc instanceof Embedded) {
- seed.push(doc);
- }
- seed = Object.keys(doc._doc).reduce(function(seed, path) {
- return docReducer(doc._doc, seed, path);
- }, seed);
- });
- } else if (val instanceof Document && val.$__isNested) {
- seed = Object.keys(val).reduce(function(seed, path) {
- return docReducer(val, seed, path);
- }, seed);
- }
- return seed;
- }
-
- const _this = this;
- const subDocs = Object.keys(this._doc).reduce(function(seed, path) {
- return docReducer(_this, seed, path);
- }, []);
-
- return subDocs;
- };
-
- /*!
- * Runs queued functions
- */
-
- function applyQueue(doc) {
- const q = doc.schema && doc.schema.callQueue;
- if (!q.length) {
- return;
- }
- let pair;
-
- for (let i = 0; i < q.length; ++i) {
- pair = q[i];
- if (pair[0] !== 'pre' && pair[0] !== 'post' && pair[0] !== 'on') {
- doc[pair[0]].apply(doc, pair[1]);
- }
- }
- }
-
- /*!
- * ignore
- */
-
- Document.prototype.$__handleReject = function handleReject(err) {
- // emit on the Model if listening
- if (this.listeners('error').length) {
- this.emit('error', err);
- } else if (this.constructor.listeners && this.constructor.listeners('error').length) {
- this.constructor.emit('error', err);
- } else if (this.listeners && this.listeners('error').length) {
- this.emit('error', err);
- }
- };
-
- /**
- * Internal helper for toObject() and toJSON() that doesn't manipulate options
- *
- * @api private
- * @method $toObject
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$toObject = function(options, json) {
- let defaultOptions = {
- transform: true,
- flattenDecimals: true
- };
-
- const path = json ? 'toJSON' : 'toObject';
- const baseOptions = get(this, 'constructor.base.options.' + path, {});
- const schemaOptions = get(this, 'schema.options', {});
- // merge base default options with Schema's set default options if available.
- // `clone` is necessary here because `utils.options` directly modifies the second input.
- defaultOptions = utils.options(defaultOptions, clone(baseOptions));
- defaultOptions = utils.options(defaultOptions, clone(schemaOptions[path] || {}));
-
- // If options do not exist or is not an object, set it to empty object
- options = utils.isPOJO(options) ? clone(options) : {};
-
- if (!('flattenMaps' in options)) {
- options.flattenMaps = defaultOptions.flattenMaps;
- }
-
- let _minimize;
- if (options.minimize != null) {
- _minimize = options.minimize;
- } else if (defaultOptions.minimize != null) {
- _minimize = defaultOptions.minimize;
- } else {
- _minimize = schemaOptions.minimize;
- }
-
- // The original options that will be passed to `clone()`. Important because
- // `clone()` will recursively call `$toObject()` on embedded docs, so we
- // need the original options the user passed in, plus `_isNested` and
- // `_parentOptions` for checking whether we need to depopulate.
- const cloneOptions = Object.assign(utils.clone(options), {
- _isNested: true,
- json: json,
- minimize: _minimize
- });
-
- const depopulate = options.depopulate ||
- get(options, '_parentOptions.depopulate', false);
- // _isNested will only be true if this is not the top level document, we
- // should never depopulate
- if (depopulate && options._isNested && this.$__.wasPopulated) {
- // populated paths that we set to a document
- return clone(this._id, cloneOptions);
- }
-
- // merge default options with input options.
- options = utils.options(defaultOptions, options);
- options._isNested = true;
- options.json = json;
- options.minimize = _minimize;
-
- cloneOptions._parentOptions = options;
-
- // remember the root transform function
- // to save it from being overwritten by sub-transform functions
- const originalTransform = options.transform;
-
- let ret = clone(this._doc, cloneOptions) || {};
-
- if (options.getters) {
- applyGetters(this, ret, 'paths', cloneOptions);
- // applyGetters for paths will add nested empty objects;
- // if minimize is set, we need to remove them.
- if (options.minimize) {
- ret = minimize(ret) || {};
- }
- }
-
- if (options.virtuals || options.getters && options.virtuals !== false) {
- applyGetters(this, ret, 'virtuals', cloneOptions);
- }
-
- if (options.versionKey === false && this.schema.options.versionKey) {
- delete ret[this.schema.options.versionKey];
- }
-
- let transform = options.transform;
-
- // In the case where a subdocument has its own transform function, we need to
- // check and see if the parent has a transform (options.transform) and if the
- // child schema has a transform (this.schema.options.toObject) In this case,
- // we need to adjust options.transform to be the child schema's transform and
- // not the parent schema's
- if (transform === true || (schemaOptions.toObject && transform)) {
- const opts = options.json ? schemaOptions.toJSON : schemaOptions.toObject;
-
- if (opts) {
- transform = (typeof options.transform === 'function' ? options.transform : opts.transform);
- }
- } else {
- options.transform = originalTransform;
- }
-
- if (typeof transform === 'function') {
- const xformed = transform(this, ret, options);
- if (typeof xformed !== 'undefined') {
- ret = xformed;
- }
- }
-
- return ret;
- };
-
- /**
- * Converts this document into a plain javascript object, ready for storage in MongoDB.
- *
- * Buffers are converted to instances of [mongodb.Binary](http://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html) for proper storage.
- *
- * ####Options:
- *
- * - `getters` apply all getters (path and virtual getters), defaults to false
- * - `virtuals` apply virtual getters (can override `getters` option), defaults to false
- * - `minimize` remove empty objects (defaults to true)
- * - `transform` a transform function to apply to the resulting document before returning
- * - `depopulate` depopulate any populated paths, replacing them with their original refs (defaults to false)
- * - `versionKey` whether to include the version key (defaults to true)
- *
- * ####Getters/Virtuals
- *
- * Example of only applying path getters
- *
- * doc.toObject({ getters: true, virtuals: false })
- *
- * Example of only applying virtual getters
- *
- * doc.toObject({ virtuals: true })
- *
- * Example of applying both path and virtual getters
- *
- * doc.toObject({ getters: true })
- *
- * To apply these options to every document of your schema by default, set your [schemas](#schema_Schema) `toObject` option to the same argument.
- *
- * schema.set('toObject', { virtuals: true })
- *
- * ####Transform
- *
- * We may need to perform a transformation of the resulting object based on some criteria, say to remove some sensitive information or return a custom object. In this case we set the optional `transform` function.
- *
- * Transform functions receive three arguments
- *
- * function (doc, ret, options) {}
- *
- * - `doc` The mongoose document which is being converted
- * - `ret` The plain object representation which has been converted
- * - `options` The options in use (either schema options or the options passed inline)
- *
- * ####Example
- *
- * // specify the transform schema option
- * if (!schema.options.toObject) schema.options.toObject = {};
- * schema.options.toObject.transform = function (doc, ret, options) {
- * // remove the _id of every document before returning the result
- * delete ret._id;
- * return ret;
- * }
- *
- * // without the transformation in the schema
- * doc.toObject(); // { _id: 'anId', name: 'Wreck-it Ralph' }
- *
- * // with the transformation
- * doc.toObject(); // { name: 'Wreck-it Ralph' }
- *
- * With transformations we can do a lot more than remove properties. We can even return completely new customized objects:
- *
- * if (!schema.options.toObject) schema.options.toObject = {};
- * schema.options.toObject.transform = function (doc, ret, options) {
- * return { movie: ret.name }
- * }
- *
- * // without the transformation in the schema
- * doc.toObject(); // { _id: 'anId', name: 'Wreck-it Ralph' }
- *
- * // with the transformation
- * doc.toObject(); // { movie: 'Wreck-it Ralph' }
- *
- * _Note: if a transform function returns `undefined`, the return value will be ignored._
- *
- * Transformations may also be applied inline, overridding any transform set in the options:
- *
- * function xform (doc, ret, options) {
- * return { inline: ret.name, custom: true }
- * }
- *
- * // pass the transform as an inline option
- * doc.toObject({ transform: xform }); // { inline: 'Wreck-it Ralph', custom: true }
- *
- * If you want to skip transformations, use `transform: false`:
- *
- * if (!schema.options.toObject) schema.options.toObject = {};
- * schema.options.toObject.hide = '_id';
- * schema.options.toObject.transform = function (doc, ret, options) {
- * if (options.hide) {
- * options.hide.split(' ').forEach(function (prop) {
- * delete ret[prop];
- * });
- * }
- * return ret;
- * }
- *
- * var doc = new Doc({ _id: 'anId', secret: 47, name: 'Wreck-it Ralph' });
- * doc.toObject(); // { secret: 47, name: 'Wreck-it Ralph' }
- * doc.toObject({ hide: 'secret _id', transform: false });// { _id: 'anId', secret: 47, name: 'Wreck-it Ralph' }
- * doc.toObject({ hide: 'secret _id', transform: true }); // { name: 'Wreck-it Ralph' }
- *
- * Transforms are applied _only to the document and are not applied to sub-documents_.
- *
- * Transforms, like all of these options, are also available for `toJSON`. See [this guide to `JSON.stringify()`](https://thecodebarbarian.com/the-80-20-guide-to-json-stringify-in-javascript.html) to learn why `toJSON()` and `toObject()` are separate functions.
- *
- * See [schema options](/docs/guide.html#toObject) for some more details.
- *
- * _During save, no custom options are applied to the document before being sent to the database._
- *
- * @param {Object} [options]
- * @param {Boolean} [options.getters=false] if true, apply all getters, including virtuals
- * @param {Boolean} [options.virtuals=false] if true, apply virtuals. Use `{ getters: true, virtuals: false }` to just apply getters, not virtuals
- * @param {Boolean} [options.minimize=true] if true, omit any empty objects from the output
- * @param {Function|null} [options.transform=null] if set, mongoose will call this function to allow you to transform the returned object
- * @param {Boolean} [options.depopulate=false] if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths.
- * @param {Boolean} [options.versionKey=true] if false, exclude the version key (`__v` by default) from the output
- * @param {Boolean} [options.flattenMaps=false] if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`.
- * @return {Object} js object
- * @see mongodb.Binary http://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html
- * @api public
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.toObject = function(options) {
- return this.$toObject(options);
- };
-
- /*!
- * Minimizes an object, removing undefined values and empty objects
- *
- * @param {Object} object to minimize
- * @return {Object}
- */
-
- function minimize(obj) {
- const keys = Object.keys(obj);
- let i = keys.length;
- let hasKeys;
- let key;
- let val;
-
- while (i--) {
- key = keys[i];
- val = obj[key];
-
- if (utils.isObject(val) && !Buffer.isBuffer(val)) {
- obj[key] = minimize(val);
- }
-
- if (undefined === obj[key]) {
- delete obj[key];
- continue;
- }
-
- hasKeys = true;
- }
-
- return hasKeys
- ? obj
- : undefined;
- }
-
- /*!
- * Applies virtuals properties to `json`.
- *
- * @param {Document} self
- * @param {Object} json
- * @param {String} type either `virtuals` or `paths`
- * @return {Object} `json`
- */
-
- function applyGetters(self, json, type, options) {
- const schema = self.schema;
- const paths = Object.keys(schema[type]);
- let i = paths.length;
- const numPaths = i;
- let path;
- let assignPath;
- let cur = self._doc;
- let v;
-
- if (!cur) {
- return json;
- }
-
- if (type === 'virtuals') {
- options = options || {};
- for (i = 0; i < numPaths; ++i) {
- path = paths[i];
- // We may be applying virtuals to a nested object, for example if calling
- // `doc.nestedProp.toJSON()`. If so, the path we assign to, `assignPath`,
- // will be a trailing substring of the `path`.
- assignPath = path;
- if (options.path != null) {
- if (!path.startsWith(options.path + '.')) {
- continue;
- }
- assignPath = path.substr(options.path.length + 1);
- }
- const parts = assignPath.split('.');
- v = clone(self.get(path), options);
- if (v === void 0) {
- continue;
- }
- const plen = parts.length;
- cur = json;
- for (let j = 0; j < plen - 1; ++j) {
- cur[parts[j]] = cur[parts[j]] || {};
- cur = cur[parts[j]];
- }
- cur[parts[plen - 1]] = v;
- }
-
- return json;
- }
-
- while (i--) {
- path = paths[i];
-
- const parts = path.split('.');
- const plen = parts.length;
- const last = plen - 1;
- let branch = json;
- let part;
- cur = self._doc;
-
- for (let ii = 0; ii < plen; ++ii) {
- part = parts[ii];
- v = cur[part];
- if (ii === last) {
- const val = self.get(path);
- // Ignore single nested docs: getters will run because of `clone()`
- // before `applyGetters()` in `$toObject()`. Quirk because single
- // nested subdocs are hydrated docs in `_doc` as opposed to POJOs.
- if (val != null && val.$__ == null) {
- branch[part] = clone(val, options);
- }
- } else if (v == null) {
- if (part in cur) {
- branch[part] = v;
- }
- break;
- } else {
- branch = branch[part] || (branch[part] = {});
- }
- cur = v;
- }
- }
-
- return json;
- }
-
- /**
- * The return value of this method is used in calls to JSON.stringify(doc).
- *
- * This method accepts the same options as [Document#toObject](#document_Document-toObject). To apply the options to every document of your schema by default, set your [schemas](#schema_Schema) `toJSON` option to the same argument.
- *
- * schema.set('toJSON', { virtuals: true })
- *
- * See [schema options](/docs/guide.html#toJSON) for details.
- *
- * @param {Object} options
- * @return {Object}
- * @see Document#toObject #document_Document-toObject
- * @see JSON.stringify() in JavaScript https://thecodebarbarian.com/the-80-20-guide-to-json-stringify-in-javascript.html
- * @api public
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.toJSON = function(options) {
- return this.$toObject(options, true);
- };
-
- /**
- * Helper for console.log
- *
- * @api public
- * @method inspect
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.inspect = function(options) {
- const isPOJO = utils.isPOJO(options);
- let opts;
- if (isPOJO) {
- opts = options;
- opts.minimize = false;
- }
- return this.toObject(opts);
- };
-
- if (inspect.custom) {
- /*!
- * Avoid Node deprecation warning DEP0079
- */
-
- Document.prototype[inspect.custom] = Document.prototype.inspect;
- }
-
- /**
- * Helper for console.log
- *
- * @api public
- * @method toString
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.toString = function() {
- return inspect(this.inspect());
- };
-
- /**
- * Returns true if the Document stores the same data as doc.
- *
- * Documents are considered equal when they have matching `_id`s, unless neither
- * document has an `_id`, in which case this function falls back to using
- * `deepEqual()`.
- *
- * @param {Document} doc a document to compare
- * @return {Boolean}
- * @api public
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.equals = function(doc) {
- if (!doc) {
- return false;
- }
-
- const tid = this.get('_id');
- const docid = doc.get ? doc.get('_id') : doc;
- if (!tid && !docid) {
- return deepEqual(this, doc);
- }
- return tid && tid.equals
- ? tid.equals(docid)
- : tid === docid;
- };
-
- /**
- * Populates document references, executing the `callback` when complete.
- * If you want to use promises instead, use this function with
- * [`execPopulate()`](#document_Document-execPopulate)
- *
- * ####Example:
- *
- * doc
- * .populate('company')
- * .populate({
- * path: 'notes',
- * match: /airline/,
- * select: 'text',
- * model: 'modelName'
- * options: opts
- * }, function (err, user) {
- * assert(doc._id === user._id) // the document itself is passed
- * })
- *
- * // summary
- * doc.populate(path) // not executed
- * doc.populate(options); // not executed
- * doc.populate(path, callback) // executed
- * doc.populate(options, callback); // executed
- * doc.populate(callback); // executed
- * doc.populate(options).execPopulate() // executed, returns promise
- *
- *
- * ####NOTE:
- *
- * Population does not occur unless a `callback` is passed *or* you explicitly
- * call `execPopulate()`.
- * Passing the same path a second time will overwrite the previous path options.
- * See [Model.populate()](#model_Model.populate) for explaination of options.
- *
- * @see Model.populate #model_Model.populate
- * @see Document.execPopulate #document_Document-execPopulate
- * @param {String|Object} [path] The path to populate or an options object
- * @param {Function} [callback] When passed, population is invoked
- * @api public
- * @return {Document} this
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.populate = function populate() {
- if (arguments.length === 0) {
- return this;
- }
-
- const pop = this.$__.populate || (this.$__.populate = {});
- const args = utils.args(arguments);
- let fn;
-
- if (typeof args[args.length - 1] === 'function') {
- fn = args.pop();
- }
-
- // allow `doc.populate(callback)`
- if (args.length) {
- // use hash to remove duplicate paths
- const res = utils.populate.apply(null, args);
- for (let i = 0; i < res.length; ++i) {
- pop[res[i].path] = res[i];
- }
- }
-
- if (fn) {
- const paths = utils.object.vals(pop);
- this.$__.populate = undefined;
- let topLevelModel = this.constructor;
- if (this.$__isNested) {
- topLevelModel = this.$__.scope.constructor;
- const nestedPath = this.$__.nestedPath;
- paths.forEach(function(populateOptions) {
- populateOptions.path = nestedPath + '.' + populateOptions.path;
- });
- }
-
- // Use `$session()` by default if the document has an associated session
- // See gh-6754
- if (this.$session() != null) {
- const session = this.$session();
- paths.forEach(path => {
- if (path.options == null) {
- path.options = { session: session };
- return;
- }
- if (!('session' in path.options)) {
- path.options.session = session;
- }
- });
- }
-
- topLevelModel.populate(this, paths, fn);
- }
-
- return this;
- };
-
- /**
- * Explicitly executes population and returns a promise. Useful for ES2015
- * integration.
- *
- * ####Example:
- *
- * var promise = doc.
- * populate('company').
- * populate({
- * path: 'notes',
- * match: /airline/,
- * select: 'text',
- * model: 'modelName'
- * options: opts
- * }).
- * execPopulate();
- *
- * // summary
- * doc.execPopulate().then(resolve, reject);
- *
- *
- * @see Document.populate #document_Document-populate
- * @api public
- * @param {Function} [callback] optional callback. If specified, a promise will **not** be returned
- * @return {Promise} promise that resolves to the document when population is done
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.execPopulate = function(callback) {
- return utils.promiseOrCallback(callback, cb => {
- this.populate(cb);
- }, this.constructor.events);
- };
-
- /**
- * Gets _id(s) used during population of the given `path`.
- *
- * ####Example:
- *
- * Model.findOne().populate('author').exec(function (err, doc) {
- * console.log(doc.author.name) // Dr.Seuss
- * console.log(doc.populated('author')) // '5144cf8050f071d979c118a7'
- * })
- *
- * If the path was not populated, undefined is returned.
- *
- * @param {String} path
- * @return {Array|ObjectId|Number|Buffer|String|undefined}
- * @memberOf Document
- * @instance
- * @api public
- */
-
- Document.prototype.populated = function(path, val, options) {
- // val and options are internal
- if (val === null || val === void 0) {
- if (!this.$__.populated) {
- return undefined;
- }
- const v = this.$__.populated[path];
- if (v) {
- return v.value;
- }
- return undefined;
- }
-
- // internal
- if (val === true) {
- if (!this.$__.populated) {
- return undefined;
- }
- return this.$__.populated[path];
- }
-
- this.$__.populated || (this.$__.populated = {});
- this.$__.populated[path] = {value: val, options: options};
-
- // If this was a nested populate, make sure each populated doc knows
- // about its populated children (gh-7685)
- const pieces = path.split('.');
- for (let i = 0; i < pieces.length - 1; ++i) {
- const subpath = pieces.slice(0, i + 1).join('.');
- const subdoc = this.get(subpath);
- if (subdoc != null && subdoc.$__ != null && this.populated(subpath)) {
- const rest = pieces.slice(i + 1).join('.');
- subdoc.populated(rest, val, options);
- // No need to continue because the above recursion should take care of
- // marking the rest of the docs as populated
- break;
- }
- }
-
- return val;
- };
-
- /**
- * Takes a populated field and returns it to its unpopulated state.
- *
- * ####Example:
- *
- * Model.findOne().populate('author').exec(function (err, doc) {
- * console.log(doc.author.name); // Dr.Seuss
- * console.log(doc.depopulate('author'));
- * console.log(doc.author); // '5144cf8050f071d979c118a7'
- * })
- *
- * If the path was not populated, this is a no-op.
- *
- * @param {String} path
- * @return {Document} this
- * @see Document.populate #document_Document-populate
- * @api public
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.depopulate = function(path) {
- if (typeof path === 'string') {
- path = path.split(' ');
- }
- let populatedIds;
- const virtualKeys = this.$$populatedVirtuals ? Object.keys(this.$$populatedVirtuals) : [];
- const populated = get(this, '$__.populated', {});
-
- if (arguments.length === 0) {
- // Depopulate all
- for (let i = 0; i < virtualKeys.length; i++) {
- delete this.$$populatedVirtuals[virtualKeys[i]];
- delete this._doc[virtualKeys[i]];
- delete populated[virtualKeys[i]];
- }
-
- const keys = Object.keys(populated);
-
- for (let i = 0; i < keys.length; i++) {
- populatedIds = this.populated(keys[i]);
- if (!populatedIds) {
- continue;
- }
- delete populated[keys[i]];
- this.$set(keys[i], populatedIds);
- }
- return this;
- }
-
- for (let i = 0; i < path.length; i++) {
- populatedIds = this.populated(path[i]);
- delete populated[path[i]];
-
- if (virtualKeys.indexOf(path[i]) !== -1) {
- delete this.$$populatedVirtuals[path[i]];
- delete this._doc[path[i]];
- } else if (populatedIds) {
- this.$set(path[i], populatedIds);
- }
- }
- return this;
- };
-
-
- /**
- * Returns the full path to this document.
- *
- * @param {String} [path]
- * @return {String}
- * @api private
- * @method $__fullPath
- * @memberOf Document
- * @instance
- */
-
- Document.prototype.$__fullPath = function(path) {
- // overridden in SubDocuments
- return path || '';
- };
-
- /*!
- * Module exports.
- */
-
- Document.ValidationError = ValidationError;
- module.exports = exports = Document;
|