Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  41] [ 1]  / answers: 1 / hits: 17367  / 5 Years ago, mon, february 18, 2019, 12:00:00

I'm trying to find an object in my database by a nested property, I can't seem to find any way to do it. My schema is below and I have shown how I've attempted to query.



var stations = {
Alpha: Number,
Beta: Number
};
var systemSchema = new mongoose.Schema({
name: String,
location: String,
nodes: {
main: stations,
secondary: stations,
tertiary: stations
}
});

var System = mongoose.model(System, systemSchema);

System.findOne({ nodes: { main: {Alpha: 23000}}}, function(err, system){
if(err){console.log(err);}
else{console.log(system);}
});


Every time I run this, nothing gets returned. I was expecting that I would have the corresponding object in my database returned.


More From » mongoose

 Answers
3

Change this



System.findOne({ nodes: { main: {Alpha: 23000}}}, function(err, system){
if(err){console.log(err);}
else{console.log(system);}
});


to



 System.findOne({ 'nodes.main.Alpha': 23000}, function(err, system){
if(err){console.log(err);}
else{console.log(system);}
});


This will work


[#52574] Wednesday, February 13, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
markusdamienn

Total Points: 167
Total Questions: 119
Total Answers: 93

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;