“requête MongoDB” Réponses codées

Sélectionnez la requête dans MongoDB

-- mongo
db.people.find()
--sql
SELECT *
FROM people
Tiny Coders

requête MongoDB

db.inventory.find( {} )
Helpless Hoopoe

requête 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

Requête de la requête de base de données à MongoDB

let userBuilds = [];

    Wishlist.find({}, function (err, builds) {
        if (err)
        {
            console.log("Cannot fetch wishlisted builds");
            return res.redirect('back');
        }

        async.forEachOf(builds, function (build, key, callback) {
            Builds.find({ name: build.name }, function (err, detailBuild) {
                if (err)
                {
                    console.log("Cannot fetch detail from wishlisted builds");
                    return res.redirect('back');
                }

                userBuilds.push(detailBuild[0]);
                callback();
            });
        }, function (err) {
            if (err)
            {
                console.log("Error in rendering page");
                return res.redirect('back');
            }

            return res.render('wishlist', {
                title: "Wishlist",
                email: req.session.email ? req.session.email : undefined,
                builds: userBuilds,
                layout: false
            });
        });
    });
Perfect Porcupine

Où requête MongoDB

db.players.find( { $where: function() {   return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a83e994")} } );
Charming Cowfish

Réponses similaires à “requête MongoDB”

Questions similaires à “requête MongoDB”

Plus de réponses similaires à “requête MongoDB” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code