4

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"
}
  • 1
    http://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like – parwatcodes Mar 05 '17 at 15:19
  • Karl, see [this answer](http://stackoverflow.com/a/31938474/1259510) in the linked duplicate as `suche` is a variable. What you're current attempt is doing is searching for the string `'suche'` itself. – JohnnyHK Mar 07 '17 at 14:20
  • I am also having the same problem... did you find the answer? – maswadkar Apr 27 '18 at 07:24

1 Answers1

1

Duplicate of this mysql-instr-like-operation-in-mongodb

Your query would be like:

db.personen.find({name: /suche/})
Community
  • 1
  • 1
Gaurav Kumar Singh
  • 1,550
  • 3
  • 11
  • 31
  • Thanks for your answer and sorry fort the duplicate but I tried it like that but it does not work. My docs is always empty. So, I think I may have ask the wrong question or look on the wrong place for the problem. – Karl Schnitmann Mar 06 '17 at 22:02