s = 'hello %s, how are you doing' % (my_name)
Run Code Online (Sandbox Code Playgroud)
这就是你在python中的表现.你怎么能在javascript/node.js中做到这一点?
我有一个$text-Index和这样的元素的mongodb :
{
foo: "my super cool item"
}
{
foo: "your not so cool item"
}
Run Code Online (Sandbox Code Playgroud)
如果我搜索
mycoll.find({ $text: { $search: "super"} })
Run Code Online (Sandbox Code Playgroud)
我得到第一项(正确).
但我也想用"uper"搜索获得第一项 - 但如果我尝试:
mycoll.find({ $text: { $search: "uper"} })
Run Code Online (Sandbox Code Playgroud)
我没有得到任何结果.
我的问题:如果有一种方法可以使用$ text,那么它会找到搜索字符串的一部分?(例如像'%uper%'在mysql)
注意:我不要求只进行正则表达式搜索 - 我要求在$ text-search中进行正则表达式搜索!
我希望按名称查找帐户(在50个帐户的MongoDB集合中)
通常的方式:我们用字符串找到
db.accounts.find({ name: 'Jon Skeet' }) // indexes help improve performance!
Run Code Online (Sandbox Code Playgroud)
正则表达怎么样?这是一项昂贵的操作吗?
db.accounts.find( { name: /Jon Skeet/ }) // worry! how indexes work with regex?
Run Code Online (Sandbox Code Playgroud)
编辑:
根据WiredPrairie:
MongoDB使用RegEx的前缀来查找索引(例如:)/^prefix.*/:
db.accounts.find( { name: /^Jon Skeet/ }) // indexes will help!'
Run Code Online (Sandbox Code Playgroud)
这个问题与另一篇文章非常相似
我基本上想要使用mongodb版本的sql"like"'%m%'运算符
但在我的情况下,我正在使用java api for mongodb,而另一篇文章是使用mongodb shell
我尝试了在另一个帖子中发布的内容并且工作正常
db.users.find({"name": /m/})
Run Code Online (Sandbox Code Playgroud)
但在java中,我在BasicDBObject上使用put方法并将其传递给DBCollections对象上的find()方法
BasicDBObject q = new BasicDBOBject();
q.put("name", "/"+m+"/");
dbc.find(q);
Run Code Online (Sandbox Code Playgroud)
但这似乎没有用.
谁有任何想法?
由于使用mongodb全文搜索无法通过单词"blue"找到"blueberry",我想帮助我的用户完成"blue"到"blueberry"这个词.为此,是否可以查询mongodb全文索引中的所有单词 - >我可以将这些单词用作建议,即对于typeahead.js?
基本上这是我的问题: 如何用"喜欢"查询MongoDB?
但是我想这里的所有答案都适用于你使用mongodb shell命令行的时候,那么db.users.find({"name": /.*m.*/})当我们使用java时,它们是等价的.
这是我想要做的
Run Code Online (Sandbox Code Playgroud)DBCollection coll = MongoDBUtil.getDB().getCollection("post_details"); BasicDBObject query = new BasicDBObject(); query.put("price", new BasicDBObject("$gt", 5).append("$lt", 8));
/*what should i write instead of this line*/
query.put("title", "/.*m.*/");
DBCursor cur = coll.find(query);
Run Code Online (Sandbox Code Playgroud) 我已经对此进行了研究,但找不到为什么我正在尝试的方法不起作用,并且会警告我对 python 有点陌生,对 mongodb 非常陌生。我有一个 JSON 格式的 mongo 推文数据库,我试图通过 Python 和 pymongo 进行查询。我想为所有包含“IP”的推文返回“text”和“created_at”字段。
我已经尝试了以下方法,当我通过终端执行此操作时效果很好:
db.tweets.find({text:/IP/},{text:1,created_at:1})
Run Code Online (Sandbox Code Playgroud)
在 Python 中,经过试验我发现我需要将字段名称放在引号之间。我得到了以下类似的查询:
cursor = db.tweets.find({'created_at':"Thu Apr 28 09:55:57 +0000 2016"},{'text':1,'created_at':1})
Run Code Online (Sandbox Code Playgroud)
但是当我尝试:
db.tweets.find({"text": /.*IP.*/},{'text':1,'created_at':1})
Run Code Online (Sandbox Code Playgroud)
或者
cursor = db.tweets.find({'text':/IP/},{'text':1,'created_at':1})
Run Code Online (Sandbox Code Playgroud)
我得到一个
'SyntaxError: invalid syntax' at the "/IP/" part of the code.
Run Code Online (Sandbox Code Playgroud)
我正在使用 mongo 3.4.6 和 python 3.5.2
我想用查询字符串在我的用户存储库中搜索.
这应该返回所有用户名为"kyogron"的用户和类似的电子邮件"kyogron @gmail"
GET localhost:3000/users?username=kyogron&email=kyogron@gmail.com
Run Code Online (Sandbox Code Playgroud)
这应该返回所有用户:
GET localhost:3000/users
Run Code Online (Sandbox Code Playgroud)
我已经设法处理路由参数,但我坚持优化它:
app.get('/users', function(req, res) {
// creates a mongoosejs query object
var query = User.find({});
// to understand how expressjs handles queries:
// ?username=kyogron&email=kyogron@gmail.com
// { username: "kyogron", email: "kyogron@gmail.com" }
//console.log(req.query);
// this was one idea of optimizing the search query parameters
// this doesn't work don't know why I always get an array of ALL users
// even the key and value is right
Object.keys(req.query).forEach(function(key) {
query.select(key, req.query[key]);
});
// …Run Code Online (Sandbox Code Playgroud) 在我的沙箱中,我有一个集合,该集合的唯一键 ( _id) 是来自另一个数据库的唯一字符串。我已经预先分配了文件,它们看起来像这样
数据看起来像这样
{ _id : "UNIQUEKEY1:1463670000000", data: {value:NaN} }
{ _id : "UNIQUEKEY2:1463670000000", data: {value:NaN} }
Run Code Online (Sandbox Code Playgroud)
我想通过以下方式查询数据
{ "_id": {$regex : "/^UNIQUEKEY1.*/i"} }
Run Code Online (Sandbox Code Playgroud)
我已阅读,你可以查询_id它是否在布兰登的评论字符串这里
我不希望另一个属性的开销只是搜索何时_id可以为我提供足够的
我有一个包含products以下项目的集合
[
{
name: 'Product 1'
images: ['http://staging.example.com/1', 'http://production.example.com/2'],
...
},
{
name: 'Product 2'
images: ['http://production.example.com/3'],
...
}
]
Run Code Online (Sandbox Code Playgroud)
我想找到数组staging中包含字符串的文档images。
我尝试了一些方法,例如使用 $contains 但 mongo 抛出错误,说我不能将 $contains 与数组一起使用。