I’m trying to do a Rest API with node.js, mongodb, express and mongojs. That is the code so far which works:
app.get('/personen/:suche',function(req,res){
var suche = req.params.suche;
console.log(suche);
db.personen.find(function (err, docs){
console.log(docs);
res.json(docs);
});
});
Now I have all the persons in the db.
The variable “suche “ is a string of 3 char. Now I’d like to ask just for the name in witch this string is a part of the name Example suche = “ max” Output max, Maximillian, maxi , with the rest of the information to this person.
like in sql the operation LIKE, but I miss the Select part were I can say what I want. After some research on the web I think it should be something in this direction, but it doesn’t work.
app.get('/personen/:suche',function(req,res){
var suche = req.params.suche;
console.log(suche);
db.personen.find({name: /suche/},function (err, docs){
console.log(docs);
res.json(docs);
});
});
I’m not sure if the mistake is really in the find function or maybe I miss a cast. I don’t get an error message but an empty docs.
Thanks, in advance for every idea and sorry for the bad English.
PS: if it helps, the db content
> db.personen.find().pretty()
{
"_id" : ObjectId("58b9e43fd90231e4c9bb0b61"),
"vorname" : "max",
"nachname" : "muster",
"nr" : "1111"
}
{
"_id" : ObjectId("58b9e43fd90231e4c9bb0b62"),
"vorname" : "hans",
"nachname" : "müller",
"nr" : "2222"
}
{
"_id" : ObjectId("58b9e43fd90231e4c9bb0b63"),
"vorname" : "friz",
"nachname" : "meier",
"nr" : "3333"
}
{
"_id" : ObjectId("58b9e43fd90231e4c9bb0b64"),
"vorname" : "christoph",
"nachname" : "hugetobler",
"nr" : "4444"
}
{
"_id" : ObjectId("58b9e43fd90231e4c9bb0b65"),
"vorname" : "helmut",
"nachname" : "boss",
"nr" : "5555"
}
{
"_id" : ObjectId("58b9e43fd90231e4c9bb0b66"),
"vorname" : "kevin",
"nachname" : "küffer",
"nr" : "6666"
}
{
"_id" : ObjectId("58b9e43fd90231e4c9bb0b67"),
"vorname" : "maximilian",
"nachname" : "murer",
"nr" : "7777"
}
{
"_id" : ObjectId("58b9e43fd90231e4c9bb0b68"),
"vorname" : "maxi",
"nachname" : "mayer",
"nr" : "8888"
}