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"
}
3
  • 1
    stackoverflow.com/questions/3305561/… Commented Mar 5, 2017 at 15:19
  • Karl, see this answer in the linked duplicate as suche is a variable. What you're current attempt is doing is searching for the string 'suche' itself. Commented Mar 7, 2017 at 14:20
  • I am also having the same problem... did you find the answer? Commented Apr 27, 2018 at 7:24

1 Answer 1

1

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

Your query would be like:

db.personen.find({name: /suche/})
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.