MCPcopy
hub / github.com/mongodb/node-mongodb-native / makeFindCommand

Function makeFindCommand

src/operations/find.ts:135–246  ·  view source on GitHub ↗
(ns: MongoDBNamespace, filter: Document, options: FindOptions)

Source from the content-addressed store, hash-verified

133}
134
135function makeFindCommand(ns: MongoDBNamespace, filter: Document, options: FindOptions): Document {
136 const findCommand: Document = {
137 find: ns.collection,
138 filter
139 };
140
141 if (options.sort) {
142 findCommand.sort = formatSort(options.sort);
143 }
144
145 if (options.projection) {
146 let projection = options.projection;
147 if (projection && Array.isArray(projection)) {
148 projection = projection.length
149 ? projection.reduce((result, field) => {
150 result[field] = 1;
151 return result;
152 }, {})
153 : { _id: 1 };
154 }
155
156 findCommand.projection = projection;
157 }
158
159 if (options.hint) {
160 findCommand.hint = normalizeHintField(options.hint);
161 }
162
163 if (typeof options.skip === 'number') {
164 findCommand.skip = options.skip;
165 }
166
167 if (typeof options.limit === 'number') {
168 if (options.limit < 0) {
169 findCommand.limit = -options.limit;
170 findCommand.singleBatch = true;
171 } else {
172 findCommand.limit = options.limit;
173 }
174 }
175
176 if (typeof options.batchSize === 'number') {
177 if (options.batchSize < 0) {
178 findCommand.limit = -options.batchSize;
179 } else {
180 if (options.batchSize === options.limit) {
181 // Spec dictates that if these are equal the batchSize should be one more than the
182 // limit to avoid leaving the cursor open.
183 findCommand.batchSize = options.batchSize + 1;
184 } else {
185 findCommand.batchSize = options.batchSize;
186 }
187 }
188 }
189
190 if (typeof options.singleBatch === 'boolean') {
191 findCommand.singleBatch = options.singleBatch;
192 }

Callers 1

buildCommandDocumentMethod · 0.85

Calls 2

formatSortFunction · 0.90
normalizeHintFieldFunction · 0.90

Tested by

no test coverage detected