“dalam permintaan MongoDB” Kode Jawaban

Kueri Mongodb

db.inventory.find( {} )
Helpless Hoopoe

Kueri Mongodb

const Books = require("Books.js")
async getBooks(req, res, next) {
    let query;

    // Copy req.query
    let reqQuery = { ...req.query };

    // Fields to exclude
    const removeFields = ["select", "sort"];

    // Loop over removeFields and delete them from reqQuery
    removeFields.forEach(param => delete reqQuery[param]);

    // Create query string
    let queryStr = JSON.stringify(reqQuery);

    // Create operators ($gt, $gte, etc)
    queryStr = queryStr.replace(
      /\b(gt|gte|lt|lte|in)\b/g,
      match => `$${match}`
    );

    // Find resource
    query = Books.findQuery(JSON.parse(queryStr));

    // Select Fields
    if (req.query.select) {
      const fields = req.query.select.split(",").join(" ");
      query = query.select(fields);
    }

    // Sort
    if (req.query.sort) {
      const sortBy = req.query.sort.split(",").join(" ");
      query = query.sort(sortBy);
    } else {
      query = query.sort("-createdAt");
    }

  
    // Pagination
    const page = parseInt(req.query.page, 10) || 1;
    const limit = parseInt(req.query.limit, 10) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;
    const total = await Books.countDocuments();
    query = query.skip(startIndex).limit(limit);
  
    // Executing query
    let books = await query;
  
  	
    // Pagination result
    const pagination = {};

    if (endIndex < total) {
      pagination.next = {
        page: page + 1,
        limit,
      };
    }

    if (startIndex > 0) {
      pagination.prev = {
        page: page - 1,
        limit,
      };
    }

    res.status(200).json({
      success: true,
      count: books.length,
      pagination,
      data: books,
    });
  
  
  }
minhchiu

dalam permintaan MongoDB

/*
The $in operator selects the documents where the value of a
field equals any value in the specified array.
*/

-- Syntax : { field: { $in: [<value1>, <value2>, ... <valueN> ] } }
db.inventory.find( { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } )
Tiny Coders

Jawaban yang mirip dengan “dalam permintaan MongoDB”

Pertanyaan yang mirip dengan “dalam permintaan MongoDB”

Lebih banyak jawaban terkait untuk “dalam permintaan MongoDB” di Sql

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya