10 examples of 'mongoose find and sort' in JavaScript

Every line of 'mongoose find and sort' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
40findAll(cb) {
41 this.collection.find({}, (err, res) => {
42 if (err) return cb(err);
43 const obj = JSON.parse(JSON.stringify(res));
44 return cb(null, obj);
45 });
46}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

58public findOne(conditions: any) {
59 return (RoomCache.findOne(conditions, {
60 _id: 0,
61 locked: 1,
62 processId: 1,
63 roomId: 1,
64 })) as any as QueryHelpers<RoomListingData>;
65}
88async find(req: FindRequest = { conditions: {} }): Promise<DOC[]> {
89 const collection = await this.collection;
90
91 const conditions = this.toggleId(req.conditions, true);
92 let cursor = collection.find(conditions);
93
94 if (req.projection) {
95 cursor = cursor.project(req.projection);
96 }
97
98 if (req.sort) {
99 cursor = cursor.sort(req.sort);
100 }
101
102 if (req.skip) {
103 cursor = cursor.skip(req.skip);
104 }
105
106 if (req.limit) {
107 cursor = cursor.limit(req.limit);
108 }
109
110 const newDocuments = await cursor.toArray();
111 const results = [];
112
113 for (let document of newDocuments) {
114 document = this.toggleId(document, false);
115 document = await this.invokeEvents(POST_KEY, ['find', 'findMany'], document);
116 results.push(document);
117 }
118
119 return results;
120}
90function find(options, callback) {
91 mongoose.connection.db.collection(options.collection, function(err, collection) {
92 collection.find(options.query).skip(options.offset).limit(options.limit).toArray(callback);
93 });
94}
258function getLast(collection: Collection, callback: Function) {
259 collection.find().sort({
260 date: -1
261 }).limit(1).toArray((error, record) => callback(record[0].date));
262}
40db.projects.findOne({_id : db.ObjectId(id)}, function onFound(err, docs) {
41 if (err) {
42 throw err;
43 }
44
45 if (!docs) {
46 callback(null);
47 return;
48 }
49 callback(docs.assetFolder);
50});
142async pluck(value, key) {
143 const rawLogs = [];
144 this.selectedFields = [];
145 const keyName = key ? key : this.primaryKey;
146 this.select(value, keyName);
147 const query = this.passDataToMongooseQuery(this.getQuery(false, rawLogs), rawLogs);
148 rawLogs.push('.exec()');
149 this.logQuery('pluck', rawLogs.join(''));
150 const result = await query.exec();
151 if (result && !lodash_1.isEmpty(result)) {
152 return result.reduce(function (memo, item) {
153 memo[item[keyName]] = item[value];
154 return memo;
155 }, {});
156 }
157 return {};
158}
43export function getMongoSort(order, columns) {
44 if (!order || !columns) return;
45
46 // TODO support the nested arrays format for sort
47 // and ignore instance functions like "foo()"
48 const sort = [];
49 _.each(order, ({ column: colIndex, dir }) => {
50 const column = columns[colIndex];
51
52 // Sometimes when swapping out new table columns/collection, this will be called once
53 // with the old `order` object but the new `columns`. We protect against that here.
54 if (!column) return;
55
56 const propName = column.data;
57 const orderable = column.orderable;
58 if (typeof propName === 'string' && orderable !== false) {
59 sort.push([propName, dir]);
60 }
61 });
62 return sort;
63};
18function sort(object, collection, options){
19 options = options || {}
20
21 options.type = options.type || 'after'
22 options.key = options.key || options.type
23 options.compareKey = options.compareKey || 'id'
24 if (options.index == null){
25 options.index = collection.indexOf(object)
26 }
27
28 if (object[options.key] && !inCorrectLocation(object, collection, options)){
29
30 var correctIndex = getCorrectIndex(object, collection, options)
31 if (correctIndex != options.index && correctIndex != -1){
32 if (!options.shouldAvoidDuplicates || !isConflict(object, collection[correctIndex], options.key)){
33 // append ...
34 if (correctIndex === collection.length){
35 collection.splice(options.index, 1)
36 collection.push(object)
37
38 // or insert
39 } else {
40 collection.splice(options.index, 1)
41 if (correctIndex > options.index){
42 correctIndex -= 1 // shift up if needed
43 }
44 collection.splice(correctIndex, 0, object)
45 }
46
47 return getChange(collection, correctIndex, options)
48
49 }
50 }
51
52 }
53
54}
51async dbFind(db, id) {
52 return await conn.collection(db).findOne(
53 id
54 );
55}

Related snippets