MCPcopy Create free account
hub / github.com/CloudBoost/cloudboost / distinct

Function distinct

databases/mongo.js:527–640  ·  view source on GitHub ↗
(appId, collectionName, onKey, query, select, sort, limit, skip)

Source from the content-addressed store, hash-verified

525 },
526
527 distinct(appId, collectionName, onKey, query, select, sort, limit, skip) {
528 const deferred = q.defer();
529
530 try {
531 if (config.mongoDisconnected) {
532 deferred.reject('Database Not Connected');
533 return deferred.promise;
534 }
535
536 const collection = config.mongoClient.db(appId).collection(mongoUtil.collection.getId(appId, collectionName));
537
538 let include = [];
539
540 if (query.$include) {
541 if (query.$include.length > 0) {
542 include = include.concat(query.$include);
543 }
544 }
545
546 // delete $include and $includeList recursively
547 query = _sanitizeQuery(query);
548
549 const keys = {};
550 const indexForDot = onKey.indexOf('.');
551
552 // if DOT in onKey
553 // keys = { beforeDot: { afterDot : "$beforeDot.afterDot"} }
554 // else
555 // keys = { onKey : "$"+onKey }
556 if (indexForDot !== -1) {
557 // not using computed properties as it may not be available in server's nodejs version
558 keys[onKey.slice(0, indexForDot)] = { };
559 keys[onKey.slice(0, indexForDot)][onKey.slice(indexForDot + 1)] = `$${onKey}`;
560 } else keys[onKey] = `$${onKey}`;
561
562 if (!sort || Object.keys(sort).length === 0) sort = { createdAt: 1 };
563
564 if (!query || Object.keys(query).length === 0) {
565 query = {
566 _id: {
567 $exists: true,
568 },
569 };
570 }
571
572 const pipeline = [];
573 pipeline.push({ $match: query });
574 pipeline.push({ $sort: sort });
575
576 // push the distinct aggregation.
577 pipeline.push({
578 $group: {
579 _id: keys,
580 document: {
581 $first: '$$ROOT',
582 },
583 },
584 });

Callers

nothing calls this directly

Calls 2

_sanitizeQueryFunction · 0.85
_deserializeFunction · 0.85

Tested by

no test coverage detected