MongoDB Assignment Help | Practice Set 1
- realcode4you
- Jul 15, 2021
- 2 min read

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();



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.
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…
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…