Ответы пользователя по тегу Node.js
  • Как получить данные из mongoDB из коллекции во внешнюю переменную?

    bodrych
    @bodrych
    dev
    https://docs.mongodb.com/drivers/node/usage-exampl...
    const { MongoClient } = require("mongodb");
    // Replace the uri string with your MongoDB deployment's connection string.
    const uri =
      "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
    const client = new MongoClient(uri);
    async function run() {
      try {
        await client.connect();
        const database = client.db("sample_mflix");
        const movies = database.collection("movies");
        // Query for a movie that has the title 'The Room'
        const query = { title: "The Room" };
        const options = {
          // sort matched documents in descending order by rating
          sort: { rating: -1 },
          // Include only the `title` and `imdb` fields in the returned document
          projection: { _id: 0, title: 1, imdb: 1 },
        };
        const movie = await movies.findOne(query, options);
        // since this method returns the matched document, not a cursor, print it directly
        console.log(movie);
      } finally {
        await client.close();
      }
    }
    run().catch(console.dir);
    Ответ написан
    Комментировать