You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Apply a given schema's timestamps to the given POJO
10
+
*
11
+
* @param {Schema} schema
12
+
* @param {Object} obj
13
+
* @param {Object} [options]
14
+
* @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
15
+
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
16
+
*/
17
+
18
+
functionapplyTimestamps(schema,obj,options){
19
+
if(obj==null){
20
+
returnobj;
21
+
}
22
+
23
+
applyTimestampsToChildren(schema,obj,options);
24
+
returnapplyTimestampsToDoc(schema,obj,options);
25
+
}
26
+
27
+
/**
28
+
* Apply timestamps to any subdocuments
29
+
*
30
+
* @param {Schema} schema subdocument schema
31
+
* @param {Object} res subdocument
32
+
* @param {Object} [options]
33
+
* @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
34
+
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
* Apply timestamps to a given document. Does not apply timestamps to subdocuments: use `applyTimestampsToChildren` instead
55
+
*
56
+
* @param {Schema} schema
57
+
* @param {Object} obj
58
+
* @param {Object} [options]
59
+
* @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
60
+
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
* const User = mongoose.model('User', userSchema);
3586
+
*
3587
+
* const obj = { name: 'John' };
3588
+
* User.applyTimestamps(obj);
3589
+
* obj.createdAt; // 2024-06-01T18:00:00.000Z
3590
+
* obj.updatedAt; // 2024-06-01T18:00:00.000Z
3591
+
*
3592
+
* @param {Object} obj object or document to apply virtuals on
3593
+
* @param {Object} [options]
3594
+
* @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
3595
+
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
@@ -2222,6 +2258,7 @@ Query.prototype._unsetCastError = function _unsetCastError() {
2222
2258
* - `strict`: controls how Mongoose handles keys that aren't in the schema for updates. This option is `true` by default, which means Mongoose will silently strip any paths in the update that aren't in the schema. See the [`strict` mode docs](https://mongoosejs.com/docs/guide.html#strict) for more information.
2223
2259
* - `strictQuery`: controls how Mongoose handles keys that aren't in the schema for the query `filter`. This option is `false` by default, which means Mongoose will allow `Model.find({ foo: 'bar' })` even if `foo` is not in the schema. See the [`strictQuery` docs](https://mongoosejs.com/docs/guide.html#strictQuery) for more information.
2224
2260
* - `nearSphere`: use `$nearSphere` instead of `near()`. See the [`Query.prototype.nearSphere()` docs](https://mongoosejs.com/docs/api/query.html#Query.prototype.nearSphere())
2261
+
* - `schemaLevelProjections`: if `false`, Mongoose will not apply schema-level `select: false` or `select: true` for this query
2225
2262
*
2226
2263
* Mongoose maintains a separate object for internal options because
2227
2264
* Mongoose sends `Query.prototype.options` to the MongoDB server, and the
* @param {Boolean} [options.count=false] Only works with populate virtuals. If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), this populate virtual will contain the number of documents rather than the documents themselves when you `populate()`.
2305
2305
* @param {Function|null} [options.get=null] Adds a [getter](https://mongoosejs.com/docs/tutorials/getters-setters.html) to this virtual to transform the populated doc.
2306
2306
* @param {Object|Function} [options.match=null] Apply a default [`match` option to populate](https://mongoosejs.com/docs/populate.html#match), adding an additional filter to the populate query.
2307
+
* @param {Boolean} [options.applyToArray=false] If true and the given `name` is a direct child of an array, apply the virtual to the array rather than the elements.
0 commit comments