MCPcopy Create free account
hub / github.com/Automattic/mongoose / Document

Function Document

lib/browserDocument.js:30–93  ·  view source on GitHub ↗

* Document constructor. * * @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 * @inh

(obj, schema, fields, skipId, skipInit)

Source from the content-addressed store, hash-verified

28 */
29
30function Document(obj, schema, fields, skipId, skipInit) {
31 if (!(this instanceof Document)) {
32 return new Document(obj, schema, fields, skipId, skipInit);
33 }
34
35
36 if (utils.isObject(schema) && !schema.instanceOfSchema) {
37 schema = new Schema(schema);
38 }
39
40 // When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
41 schema = this.schema || schema;
42
43 // Generate ObjectId if it is missing, but it requires a scheme
44 if (!this.schema && schema.options._id) {
45 obj = obj || {};
46
47 if (obj._id === undefined) {
48 obj._id = new ObjectId();
49 }
50 }
51
52 if (!schema) {
53 throw new MongooseError.MissingSchemaError();
54 }
55
56 this.$__setSchema(schema);
57
58 this.$__ = new InternalCache;
59 this.$__.emitter = new EventEmitter();
60 this.isNew = true;
61 this.errors = undefined;
62
63 if (typeof fields === 'boolean') {
64 this.$__.strictMode = fields;
65 fields = undefined;
66 } else {
67 this.$__.strictMode = this.schema.options && this.schema.options.strict;
68 this.$__.selected = fields;
69 }
70
71 var required = this.schema.requiredPaths();
72 for (var i = 0; i < required.length; ++i) {
73 this.$__.activePaths.require(required[i]);
74 }
75
76 this.$__.emitter.setMaxListeners(0);
77 this._doc = this.$__buildDoc(obj, fields, skipId);
78
79 if (!skipInit && obj) {
80 this.init(obj);
81 }
82
83 this.$__registerHooksFromSchema();
84
85 // apply methods
86 for (var m in schema.methods) {
87 this[m] = schema.methods[m];

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected