First I'm sorry if I'm not using the right words or if the question is silly, I'm very new to this and still learning, and this time I wasn't able to find an answer with my search!
I have a MEAN project, and I want to get a value from my database on my front-end (I use Angular 4). So I tried writing a function on Express, but the fact is I don't know how, since I'm not even able to get the value directly in command line in mongo.
My schema has this format:
const userSchema = mongoose.Schema({
username: {
type: String,
required: true,
unique: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
item: {
item1: {
type: Number,
default: 0
},
item2: {
type: Number,
default: 0
},
item3: {
type: Number,
default: 0
}
}
});
Idea is that each user has an inventory with a certain number of each item, and my goal is to fetch this specific number when doing a request with the item name.
But the best I was able to do is db.users.find({}, {item:1}); with which I got the whole list of items with their values, every other things I tried got me an error. So maybe my schema isn't right?
So my question is: How can I only get the item value with Express by passing the item name as parameter, knowing I'm not currently able to get the value directly from mongo?
Knowing the answer will help me too to know how to change this value later on.
Thank you for reading!
[EDIT] This topic was marked as duplicate with this one: How to query nested objects?
But I saw this other topic already, so yes, with:
db.users.find({ item : { item1 : "0" }}).count();
I can get the value, but it doesn't help me at all, because you have to write the value to get the value.... but I want to make a query WITHOUT knowing the value, because this value will change.
Plus, it's more on the Express side that I need advices.
Thank you.