PROBLEM: Cant figure out the syntax to get out specific data from my documents
GOAL: I want to have access to my collection and use dot notation to get in to an document and get its information.
I have a database that looks like this:
DatabaseName
Collection1
Collection2
Collection3
In every Collection do i have multiply objects and in that i have data about the object.
I have tried to extract data for my database in over 10 hours and haven´t find the solution. I may have forgotten something from the introduction course or have understand. That is why that i would appriciate som visuals with your answers, code examples and so on. Down below can you see my database and code in VS.
MongoDB
ClashOfClans (database)
… darkelexirTroop (collection)
… elexirTroop (collection)
… superTroops (collection)
Inside darkElexirTroop
minion:
image:"none"
name: "Minion"
preferredTarget: "Everything"
attackType: "Ranged"
housingSpace: 2
movementSpeed: 32
attackSpeed:
value: 1
unit: "s"
darkBarracklvlReq: 5
attackRange:
range: 2.75
value: "tiles"
superTroop: true
superTroopReqLvl: 8
levels: (contain a document per level with info like above)
levelOne:
levelTwo
//it keeps going
img is attached.
My code in VS:
I have two files that has to do with mongoDB. I have followed the node.js guide. I have installed Express, what i know do this not provide any problems or change the way to get the data from the documents?!
FileOne: index.js
Code:
const express = require("express");
const { MongoClient } = require("mongodb");
const uri = require("./db/connection.js");
const app = express();
// const port = 3001;
// const routes = require("./Routes");
// app.use("/", routes);
console.log("connectDB:", uri);
const client = new MongoClient(uri);
const dbName = "ClashOfClans";
const collName = "elexirTroop";
const wholeColl = client.db(dbName).collection(collName);
const connectDB = async () => {
try {
await client.connect();
console.log("Connected: ", dbName);
} catch (err) {
console.error(`Error connecting: ${err}`);
}
};
const documentToFind = { elexirTroop: "barbarian" };
const main = async () => {
try {
await connectDB();
let result = await wholeColl.findOne(documentToFind);
console.log(result);
} catch (err) {
console.error(`Error DID COME UPP:${err}`);
}
};
main();
app.listen(3000, async () => {
await connectDB();
console.log("App is running");
});
module.exports = app;
File two: connection.js
module.exports = uri =
"mongodb+srv://oscarThroedsson:passwordHidden@cluster0.5xvzvb7.mongodb.net/?retryWrites=true&w=majority";
Explanation to the code:
- I use my connection string in connection.js en export it.
- Req it in index.js on line 3
- I declare to varible
- dbName: with the database name.
- collName: with the Collection name.
- I declare the connection for the database and collection in wholeColl
const wholeColl = client.db(dbName).collection(collName);
This part is just russion roulette.
const connectDB = async () => {
try {
await client.connect();
console.log("Connected: ", dbName);
} catch (err) {
console.error(`Error connecting: ${err}`);
}
};
const documentToFind = { elexirTroop: "barbarian" };
const main = async () => {
try {
await connectDB();
let result = await wholeColl.findOne(documentToFind);
console.log(result);
} catch (err) {
console.error(`Error DID COME UPP:${err}`);
}
};
main();
I have also connected the message i get in terminal but the msg is below:
~/Documents/MyOwnProjects/clashOfClanStats/server main node index.js
connectDB: mongodb+srv://oscarThroedsson:passwordHidden@cluster0.5xvzvb7.mongodb.net/?retryWrites=true&w=majority
Connected: ClashOfClans
Connected: ClashOfClans
App is running
null
I dont know what more information that you would want to be able to help me. If can provide any code, please do. But also any docs would be appreciated.