user300057 user300057 - 1 month ago 15
AngularJS Question

mongodb search query - returns all results (non matching ones too)

This is a sample from MEAN stack website,
I require the query to return parameters that match ' exactly ' with the input.
Please view the image attached to understand the issue better.

Search Query Function

Any hint on this issue ? (I'm a beginner so please elaborate a little)
-TIA :)




Input for the search from the browser

{ body: { hp: 1, length: 1, diameter: 1, voltage: 1 } }





// mongo schema

var CableSchema = new schema({
body : {
"hp": {
type: Number
},
"length": {
type: Number
},
"diameter": {
type: Number
},
"voltage": {
type: Number
},
"cost": {
type: Number
},

"type": {
type: String,
default: "Cable"
}
}
});

-----------------------------------------------------------
// Result from Search Query obtained in console

[ { body:
{ type: 'Cable',
cost: 1,
voltage: 1,
diameter: 1,
length: 1,
hp: 1 },
__v: 0,
_id: 5820246086d42a3c269ad9f2 },
{ body:
{ type: 'Cable',
cost: 2,
voltage: 2,
diameter: 2,
length: 2,
hp: 2 },
__v: 0,
_id: 5820249086d42a3c269ad9f3 } ]`

Answer

The keys 'hp','length' etc are inside the body object of cable schema. So to refer 'hp' use 'body.hp' in query

Change your query to

        Cable.find({'body.hp' : paseInt(reqHp) , 'body.length' : paseInt(reqLen),
        'body.diameter' : paseInt(reqDia) ,'body.voltage' : paseInt(reqVol)})