* Query constructor used for building queries. You do not need * to instantiate a `Query` directly. Instead use Model functions like * [`Model.find()`](https://mongoosejs.com/docs/api/model.html#Model.find()). * * #### Example: * * const query = MyModel.find(); // `query` is an instance of
(conditions, options, model, collection)
| 114 | */ |
| 115 | |
| 116 | function Query(conditions, options, model, collection) { |
| 117 | // this stuff is for dealing with custom queries created by #toConstructor |
| 118 | if (!this._mongooseOptions) { |
| 119 | this._mongooseOptions = {}; |
| 120 | } |
| 121 | options = options || {}; |
| 122 | |
| 123 | this._transforms = []; |
| 124 | this._hooks = new Kareem(); |
| 125 | this._execCount = 0; |
| 126 | |
| 127 | // this is the case where we have a CustomQuery, we need to check if we got |
| 128 | // options passed in, and if we did, merge them in |
| 129 | const keys = Object.keys(options); |
| 130 | for (const key of keys) { |
| 131 | this._mongooseOptions[key] = options[key]; |
| 132 | } |
| 133 | |
| 134 | if (collection) { |
| 135 | this.mongooseCollection = collection; |
| 136 | } |
| 137 | |
| 138 | if (model) { |
| 139 | this.model = model; |
| 140 | this.schema = model.schema; |
| 141 | } |
| 142 | |
| 143 | // this is needed because map reduce returns a model that can be queried, but |
| 144 | // all of the queries on said model should be lean |
| 145 | if (this.model?._mapreduce) { |
| 146 | this.lean(); |
| 147 | } |
| 148 | |
| 149 | // inherit mquery |
| 150 | mquery.call(this, null, options); |
| 151 | if (collection) { |
| 152 | this.collection(collection); |
| 153 | } |
| 154 | |
| 155 | if (conditions) { |
| 156 | this.find(conditions); |
| 157 | } |
| 158 | |
| 159 | this.options = this.options || {}; |
| 160 | |
| 161 | // For gh-6880. mquery still needs to support `fields` by default for old |
| 162 | // versions of MongoDB |
| 163 | this.$useProjection = true; |
| 164 | |
| 165 | const collation = this?.schema?.options?.collation || null; |
| 166 | if (collation != null) { |
| 167 | this.options.collation = collation; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Helper function to check if an object is empty or contains only empty objects/arrays |
| 172 | function isEmptyFilter(obj) { |
nothing calls this directly
no test coverage detected