Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

query in express mongoose

//controllers/tourController.js
const Tour = require('./../models/tourModel');
exports.getAllTours = async (req, res) => {
  try {
    const queryObj = { ...req.query };
    const excludeFields = ['page', 'sort', 'limit', 'fields'];
    excludeFields.forEach((el) => delete queryObj[el]);

    //build a query
    const query = Tour.find(queryObj);
    //execute query
    const tours = await query;

    res.status(200).json({
      status: 'sucess',
      results: tours.length,
      data: {
        tours,
      },
    });
  } catch (error) {
    res.status(404).json({
      status: 'fail',
      message: error,
    });
  }
};
Comment

mongoose Query object

model.find({}) instanceof mongoose.Query //true
model.find({}).lean() instanceof mongoose.Query //true
Comment

mongoose query document

1.
model.updateOne(
   { <array>: value ... },
   { <update operator>: { "<array>.$" : value } }
)
db.students.updateOne(
   { _id: 4, "grades.grade": 85 },
   { $set: { "grades.$.std" : 6 } }
)
Comment

PREVIOUS NEXT
Code Example
Javascript :: dom traversal jquery 
Javascript :: dynamic styles in react native 
Javascript :: calculator program in javascript 
Javascript :: react onsubmit get form values 
Javascript :: javasript array 
Javascript :: react color picker 
Javascript :: 206. reverse linked list javascript 
Javascript :: jquery search string for substring 
Javascript :: singleton class in js 
Javascript :: lodash sum array of objects 
Javascript :: path js 
Javascript :: JavaScript (SMonkey 60.2.3) sample 
Javascript :: how to convert string to reverse title case in javascript 
Javascript :: rem api rest 
Javascript :: the document object 
Javascript :: JavaScript for loop Display Numbers from 1 to 5 
Javascript :: javascript + Operator with Numbers 
Javascript :: javascript Set Subset Operation 
Javascript :: javascript Working of multiple yield Statements 
Javascript :: actionscript random randomfunction 
Javascript :: nodejs: Basic: managing file: Read, Write, Create, Delete 
Javascript :: prevent js execution in elementor 
Javascript :: set rotation and origin phaser 
Javascript :: phaser wrap in camera bounds 
Javascript :: append input using js 
Javascript :: JS table with rows that have alternating colours 
Javascript :: javascript concat two htmlcollection 
Javascript :: golang read json file 
Javascript :: what are array methods in javascript 
Javascript :: javascript event.target 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =