我有以下代码:
collection.find({title:"foo"}, {title:1, _id:0})
返回
{title: "foo"}
但是当我尝试时:
str = "foo";
collection.find({title:str}, {title:1, _id:0})
我没有得到任何结果。
我在 stackoverflow 上找到的所有示例都使用硬编码字符串。 如何在 mongodb 数据库中搜索不断更改的字符串?
最佳答案
var str = "foo";
var query = {title: str};
collection.find(query, {title:1, _id:0})
查询查找任何包含str的单词:
collection.find({title : { $regex : str }}, {title:1, _id:0})
关于javascript - 在 mongodb 数据库中搜索特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196475/