10 examples of 'mongoose populate subdocument' in JavaScript

Every line of 'mongoose populate subdocument' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
23function MongooseDocumentArray(values, path, doc) {
24 var arr = [].concat(values);
25 arr._path = path;
26
27 var props = {
28 isMongooseArray: true,
29 isMongooseDocumentArray: true,
30 validators: [],
31 _atomics: {},
32 _schema: void 0,
33 _handlers: void 0
34 };
35
36 // Values always have to be passed to the constructor to initialize, since
37 // otherwise MongooseArray#push will mark the array as modified to the parent.
38 var keysMA = Object.keys(MongooseArray.mixin);
39 var numKeys = keysMA.length;
40 for (var j = 0; j < numKeys; ++j) {
41 arr[keysMA[j]] = MongooseArray.mixin[keysMA[j]];
42 }
43
44 var keysMDA = Object.keys(MongooseDocumentArray.mixin);
45 numKeys = keysMDA.length;
46 for (var i = 0; i < numKeys; ++i) {
47 arr[keysMDA[i]] = MongooseDocumentArray.mixin[keysMDA[i]];
48 }
49
50 var keysP = Object.keys(props);
51 numKeys = keysP.length;
52 for (var k = 0; k < numKeys; ++k) {
53 arr[keysP[k]] = props[keysP[k]];
54 }
55
56 // Because doc comes from the context of another function, doc === global
57 // can happen if there was a null somewhere up the chain (see #3020 && #3034)
58 // RB Jun 17, 2015 updated to check for presence of expected paths instead
59 // to make more proof against unusual node environments
60 if (doc && doc instanceof Document) {
61 arr._parent = doc;
62 arr._schema = doc.schema.path(path);
63 arr._handlers = {
64 isNew: arr.notify('isNew'),
65 save: arr.notify('save')
66 };
67
68 doc.on('save', arr._handlers.save);
69 doc.on('isNew', arr._handlers.isNew);
70 }
71
72 return arr;
73}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

94populate(expr) {
95 const stack = this._schema._parsePopulate(expr);
96 return this._model._populate(this, stack);
97}
26function MongooseDocumentArray (values, path, doc) {
27 var arr = [];
28
29 // Values always have to be passed to the constructor to initialize, since
30 // otherwise MongooseArray#push will mark the array as modified to the parent.
31 arr.push.apply(arr, values);
32 arr.__proto__ = MongooseDocumentArray.prototype;
33
34 arr._atomics = {};
35 arr.validators = [];
36 arr._path = path;
37
38 if (doc) {
39 arr._parent = doc;
40 arr._schema = doc.schema.path(path);
41 doc.on('save', arr.notify('save'));
42 doc.on('isNew', arr.notify('isNew'));
43 }
44
45 return arr;
46};
23function MongooseDocumentArray (values, path, doc) {
24 var arr = [];
25 arr.push.apply(arr, values);
26 arr.__proto__ = MongooseDocumentArray.prototype;
27
28 arr._atomics = {};
29 arr.validators = [];
30 arr._path = path;
31
32 if (doc) {
33 arr._parent = doc;
34 arr._schema = doc.schema.path(path);
35 doc.on('save', arr.notify('save'));
36 doc.on('isNew', arr.notify('isNew'));
37 }
38
39 return arr;
40};
24function MongooseDocumentArray (values, path, doc) {
25 var arr = [].concat(values);
26
27 // Values always have to be passed to the constructor to initialize, since
28 // otherwise MongooseArray#push will mark the array as modified to the parent.
29 utils.decorate( arr, MongooseDocumentArray.mixin );
30 arr.isMongooseArray = true;
31 arr.isMongooseDocumentArray = true;
32
33 arr._atomics = {};
34 arr.validators = [];
35 arr._path = path;
36
37 // Because doc comes from the context of another function, doc === global
38 // can happen if there was a null somewhere up the chain (see #3020 && #3034)
39 // RB Jun 17, 2015 updated to check for presence of expected paths instead
40 // to make more proof against unusual node environments
41 if (doc && doc instanceof Document) {
42 arr._parent = doc;
43 arr._schema = doc.schema.path(path);
44 arr._handlers = {
45 isNew: arr.notify('isNew'),
46 save: arr.notify('save')
47 };
48
49 doc.on('save', arr._handlers.save);
50 doc.on('isNew', arr._handlers.isNew);
51 }
52
53 return arr;
54}
103var populate = function populate(collection) {
104
105 if (_this.populate_keys && _this.populate_keys.length > 0) _this.populate_keys.forEach(function (key) {
106 var ref = collection[key];
107 if (ref) {
108 var db = new LocalDB(ref.__className, 'Array');
109 var temp = db.findOne({ '_id': ref.objectId });
110 collection[key] = temp;
111 }
112 });
113 return collection;
114};
26function MongooseArray(values, path, doc) {
27 var arr = [].concat(values);
28
29 utils.decorate( arr, MongooseArray.mixin );
30 arr.isMongooseArray = true;
31
32 var _options = { enumerable: false, configurable: true, writable: true };
33 var keys = Object.keys(MongooseArray.mixin).
34 concat(['isMongooseArray', 'validators', '_path']);
35 for (var i = 0; i < keys.length; ++i) {
36 Object.defineProperty(arr, keys[i], _options);
37 };
38
39 arr._atomics = {};
40 arr.validators = [];
41 arr._path = path;
42
43 // Because doc comes from the context of another function, doc === global
44 // can happen if there was a null somewhere up the chain (see #3020)
45 // RB Jun 17, 2015 updated to check for presence of expected paths instead
46 // to make more proof against unusual node environments
47 if (doc && doc instanceof Document) {
48 arr._parent = doc;
49 arr._schema = doc.schema.path(path);
50 }
51
52 return arr;
53}
300function _create(ctx, doc, populatedIds, cb) {
301 var instance = helpers.createModel(ctx.query.model, doc, ctx.query._fields);
302 var opts = populatedIds ?
303 { populated: populatedIds } :
304 undefined;
305
306 instance.init(doc, opts, function(err) {
307 if (err) {
308 return cb(err);
309 }
310 cb(null, instance);
311 });
312}
26function Document(obj, schema, fields, skipId, skipInit) {
27 if (!(this instanceof Document)) {
28 return new Document(obj, schema, fields, skipId, skipInit);
29 }
30
31 if (utils.isObject(schema) && !schema.instanceOfSchema) {
32 schema = new Schema(schema);
33 }
34
35 // When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
36 schema = this.schema || schema;
37
38 // Generate ObjectId if it is missing, but it requires a scheme
39 if (!this.schema && schema.options._id) {
40 obj = obj || {};
41
42 if (obj._id === undefined) {
43 obj._id = new ObjectId();
44 }
45 }
46
47 if (!schema) {
48 throw new MongooseError.MissingSchemaError();
49 }
50
51 this.$__setSchema(schema);
52
53 NodeJSDocument.call(this, obj, fields, skipId, skipInit);
54
55 applyHooks(this, schema, { decorateDoc: true });
56
57 // apply methods
58 for (var m in schema.methods) {
59 this[m] = schema.methods[m];
60 }
61 // apply statics
62 for (var s in schema.statics) {
63 this[s] = schema.statics[s];
64 }
65}
173async populate(modelName, data, populates) {
174 try {
175 const model = this.Models[modelName];
176 return await model.populate(data, populates);
177 } catch (e) {
178 return Promise.reject(e);
179 }
180}

Related snippets