top of page

MongoDB Assignment Help | Practice Set 1



In this we will provide the some MongoDB queries and their solution, you need to practice it itself and comment in below section to send different solution from own side.


Check or download Dataset(Assgcars.json) from here.



Here the queries solution using nodejs:


First need to connect mongodb using below MongoClient


const MongoClient = require("mongodb").MongoClient;

//update the connection string.
const url = "mongodb://localhost/mongo-task";

//update database name
const dbName = "mongo-task";

//update collection name (i.e targetted collection)

const collectionName = "cars";

/*Query results are logged on the terminal.*/

Query 1(e)

//Query 1
numberOfColoursByManufacturer = async () => {
  const client = await MongoClient.connect(url, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  }).catch((err) => console.log("Could not connect to MongoDB", err));

  try {
    const db = client.db(dbName);
    const collection = db.collection(collectionName);
    let res = await collection
      .aggregate([
        {
          $unwind: "$Colour",
        },
        {
          $group: {
            _id: "$Manufacturer",
            Colours: { $addToSet: "$Colour" },
          },
        },
        {
          $project: {
            _id: 0,
            Manufacturer: "$_id",
            CountOfColours: { $size: "$Colours" },
          },
        },
      ])
      .toArray();
    console.log(res);
  } catch (err) {
    console.log("Error occured in First Query");
  } finally {
    client.close();
  }
};


Query 2(f)

//Query 2
extrasByManufacturer = async () => {
  const client = await MongoClient.connect(url, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  }).catch((err) => console.log("Could not connect to MongoDB", err));

  try {
    const db = client.db(dbName);
    const collection = db.collection(collectionName);
    let res = await collection
      .aggregate([
        {
          $unwind: "$Extras",
        },
        {
          $group: {
            _id: "$Manufacturer",
            Extras: { $addToSet: "$Extras" },
          },
        },
        {
          $project: {
            _id: 0,
            Manufacturer: "$_id",
            Extras: "$Extras",
          },
        },
      ])
      .toArray();
    console.log(res);
  } catch (err) {
    console.log("Error in Sec Query", err);
  } finally {
    client.close();
  }
};

Query 3(g)

//Query 3
countNumberOfExtras = async () => {
  const client = await MongoClient.connect(url, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  }).catch((err) => console.log("Could not connect to MongoDB", err));

  try {
    const db = client.db(dbName);
    const collection = db.collection(collectionName);
    let res = await collection
      .aggregate([
        {
          $group: {
            _id: "$Model",
            Extras: { $push: "$Extras" },
            Avg: { $avg: { $size: "$Extras" } },
          },
        },
        {
          $project: {
            _id: 0,
            Model: "$_id",
            CountOfExtras: {
              $size: {
                $reduce: {
                  input: "$Extras",
                  initialValue: [],
                  in: { $setUnion: ["$$value", "$$this"] },
                },
              },
            },
            Avg: "$Avg",
          },
        },
      ])
      .toArray();
    console.log(res);
  } catch (err) {
    console.log("Error in Third Query", err);
  } finally {
    client.close();
  }
};

Query 4(h)

//Query 4
lowestValPerManufacturer = async () => {
  const client = await MongoClient.connect(url, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  }).catch((err) => console.log("Could not connect to MongoDB", err));

  try {
    const db = client.db(dbName);
    const collection = db.collection(collectionName);

    const res = await collection
      .aggregate([
        {
          $group: {
            _id: "$Manufacturer",
            price: {
              $min: {
                $add: ["$Price", "$Milage"],
              },
            },
          },
        },
        {
          $project: {
            _id: 0,
            Manufacturer: "$_id",
            LowestPrice: "$price",
          },
        },
      ])
      .toArray();
    console.log(res);
  } catch (err) {
    console.log("Error in Fourth Query", err);
  } finally {
    client.close();
  }
};

// To execute one query comment the others.

async function run() {
  await numberOfColoursByManufacturer();
  //await extrasByManufacturer();
  //await countNumberOfExtras();
  //await lowestValPerManufacturer();
}

run();

3 Comments


https://33win.forsale/ mình bấm vào coi thử cho biết thôi, tại thấy mấy đứa bạn nói suốt. Vừa vào là thấy bố cục kiểu chia khối nhìn khá dễ chịu, lướt xuống không bị “ngợp” vì mỗi đoạn có tiêu đề rõ ràng. Mình có đọc lướt phần họ nói về chuyện minh bạch/pháp lý, cách viết ngắn gọn nên hiểu ý nhanh chứ không vòng vo. Mấy thông tin chính đặt ngay trên trang, không phải mò menu hay bấm qua lại nhiều lần. Nói chung cảm giác như họ sắp xếp nội dung theo từng box riêng, nhìn một vòng là bắt nhịp được, nhất là các heading to và tách đoạn khá rõ trên trang.

Like

https://sunwin20.pink/ mình bấm vào xem thử cho biết thôi, kiểu lướt nhanh chứ không ngồi đọc kỹ. Ấn tượng đầu là giao diện nhìn gọn gàng, chữ dễ nhìn, mấy tiêu đề to rõ nên cuộn xuống không bị lạc. Nội dung họ viết cũng thẳng, nhắc nhiều tới chuyện vận hành ổn định với minh bạch nên đọc qua là hiểu họ đang muốn nói gì, không kiểu lan man. Mình cũng để ý mấy đoạn giới thiệu được chia thành từng khối tách bạch, nên đang ở phần nào nhìn phát biết liền. Nói chung cảm giác trang này làm theo kiểu ưu tiên cho người lướt nhanh, đỡ phải căng mắt tìm thông tin, nhất là cách…

Like

go 88 mình thấy bạn bè nhắc hoài nên tò mò vào xem thử, kiểu lướt cho biết giao diện thôi chứ chưa chơi gì nhiều. Vừa mở lên cái là thấy trang làm khá gọn, chữ với khối nội dung xếp ngay ngắn nên nhìn không bị ngợp. Mình thích nhất là phần danh mục chia rõ ràng, bấm qua lại dễ hiểu, người mới vào cũng không phải đoán “nút này để làm gì”. Tốc độ tải cũng ổn áp, chuyển giữa mấy mục gần như không phải chờ, lúc mạng nhà mình hơi yếu vẫn thấy mượt. Nói chung cảm giác họ tối ưu kiểu web app khá kỹ, thao tác nhanh và ít rối mắt. Nhìn…

Like

REALCODE4YOU

Realcode4you is the one of the best website where you can get all computer science and mathematics related help, we are offering python project help, java project help, Machine learning project help, and other programming language help i.e., C, C++, Data Structure, PHP, ReactJs, NodeJs, React Native and also providing all databases related help.

Hire Us to get Instant help from realcode4you expert with an affordable price.

USEFUL LINKS

Discount

ADDRESS

Noida, Sector 63, India 201301

Follows Us!

  • Facebook
  • Twitter
  • Instagram
  • LinkedIn

OUR CLIENTS BELONGS TO

  • india
  • australia
  • canada
  • hong-kong
  • ireland
  • jordan
  • malaysia
  • new-zealand
  • oman
  • qatar
  • saudi-arabia
  • singapore
  • south-africa
  • uae
  • uk
  • usa

© 2023 IT Services provided by Realcode4you.com

bottom of page