- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章mongo中模糊查询的综合应用由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前言 。
以下是一个mongo查询的综合应用,即介绍一个生产中实际应用的模糊查询,当然其实也很简单,主要用到mongo中的模糊查询和$or查询,以及并的关系,下面是一个mongo中的一条记录 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
{
"_id"
:
"ffe6a068-9043-4334-97d2-75387340e655"
,
"file_id"
:
"ffe6a068-9043-4334-97d2-75387340e655"
,
"name"
:
"中国正大"
,
"update_time"
: NumberInt(1554975642),
"create_time"
: NumberInt(1554975642),
"content"
:
"中国正大相关信息"
,
"file_url"
:
""
,
"file_type"
:
""
,
"user_ids"
: [
1.0,
10.0
],
"group_ids"
: [
],
"is_common"
: NumberInt(0),
"confidence"
: -1.0,
"obj_id"
:
""
,
"source"
:
""
,
"content_time"
:
""
,
"author"
:
""
,
"summary"
:
""
,
"info_type"
:
"00"
,
"sub_info_type"
:
""
,
"title"
:
""
,
"word_num"
: NumberInt(8)
}
|
对上面一条记录或者更多条记录我们生产中的需求是:查询出集合中(mongo中的集合即是mysql中的表),name或content中包含"正大"二字的记录(关键词即是用户随机输入的,其实是一个变量),并且时间戳的值大于某一个开始时间和某一个结束时间(这个也是用户在前端进行选择,然后我们拿到前端的请求来进行查询的),并且文件的类型即info_type字段的值为"00",“00”代表的是word也是前端用户选择后我们获取的条件之一,当然还有其他条件想进行尝试可以自由发挥 。
下面就是使用mongo语句进行实现的上面的需求:
1
|
db.getCollection(
"subscribe_test"
).find({$
or
:[{
"name"
:{
"$regex"
:
"正大"
}},{
"content"
:{
"$regex"
:
"正大"
}}],
"update_time"
:{$gte:1,$lte:2000000000},info_type:
"00"
})
|
对于查询我们有的时候会选择在程序中进行,有的小伙伴会问上面的mongo语句怎么在编程语言中进行实现,下面是用python语言中进行实现的,我们会引用python中操作mongo的一个模块即pymongo模块可以使用pip install pymongo在控制台或cmd中进行一键安装,至于如何使用也很简单,可以自行百度或者访问我的另一篇博客:pymono的简单使用,下面附上用python代码实现上面需求的业务代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import pymongo
import re
# 创建数据库连接
client = pymongo.MongoClient(host=
'127.0.0.1'
, port=8014) #填写自己本机数据库的ip和port或者远程服务器数据库的ip和port
# 指定数据库db1,没有则创建数据库db1
db = client.dataretrieve
#指定数据库中指定的表
collection=db.subscribe_test
""
"1、对表中的数据进行查询"
""
""
"
db.collection.find(query, projection)
query :可选,使用查询操作符指定查询条件
projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。
"
""
query = {}
query[
"$or"
] = [
{
"name"
: re.compile(
"正大"
)},
{
"content"
: re.compile(
"正大"
)},
]
query[
"file_type"
] =
"00"
query[
"update_time"
] = {
"$gte"
: 0,
"$lte"
: 2000000000}
row=collection.find(filter=query)
for
r
in
row:
print(r[
"content"
])
|
下面是生产中实际的开发代码,只供参考,只是把上面的一些常量,换成了从前端请求的数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
def person_handler(req_params, page_size, search_offset):
""
"
去mongo中查询个人数据
:param req_params:
:param page_size:
:param search_offset:
:return:
"
""
results = []
query = {}
update_time = {}
if
'start_time'
in
req_params
and
req_params[
"start_time"
]:
start_time =
int
(req_params[
"start_time"
])
update_time[
'$gte'
] = start_time
if
'end_time'
in
req_params
and
req_params[
'end_time'
]:
end_time =
int
(req_params[
"end_time"
])
update_time[
'$lte'
] = end_time
if update_time:
query[
"update_time"
] = update_time
if
'file_type'
in
req_params
and
req_params[
'file_type'
]:
query[
"file_type"
] = req_params[
"file_type"
]
if
'user_ids'
in
req_params
and
req_params[
'user_ids'
]:
query[
'user_ids'
] =
int
(req_params[
'user_id'
])
serch_keywords = req_params[
"search_keywords"
]
query[
"$or"
] = [
{
"name"
: re.compile(serch_keywords)},
{
"content"
: re.compile(serch_keywords)},
]
print(query)
result = person_mongodao.search(filter=query).skip(search_offset).limit(page_size)
count
= person_mongodao.search(filter=query).skip(search_offset).limit(page_size).
count
()
for
row
in
result:
results.append(row)
additions = {
"word_segs"
: req_params[
"search_keywords"
],
"remind"
: 0}
print(
"查询结果"
, results)
return
results, additions,
count
|
如果有小伙伴说我用的不是python语言譬如java用代码怎么实现呢?那么如果你会写mysql来实现上面的需求的话本博主可以推荐你使用mongo的一款可视化工具Studio 3T来将mysql语句转换成mongo语句,python语句,java语句等 。
mysql语句也类似mongo语句有一个控制台可以来进行书写mysql语句,然后进行查询之后将结果进行转换 。
总结 。
以上就是关于mongo模糊查询的简单使用,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我的支持.
原文链接:http://www.cnblogs.com/sui776265233/p/10694958.html 。
最后此篇关于mongo中模糊查询的综合应用的文章就讲到这里了,如果你想了解更多关于mongo中模糊查询的综合应用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在学习 Javascript,我正在尝试创建一个简单的下拉菜单。我想要的功能的示例可以在 Google 主页的顶部菜单中看到,其中包含“更多”和“设置”下拉菜单。 我有一个使用 onclick()
我尝试捕捉 tinyMce 编辑器的模糊和焦点事件。 我为此找到了以下方法。 ed.onInit.add(function(ed) { tiny
这里完全被难住了。尝试一些很简单的东西,但它不起作用: $("input.input1, textarea.input1").focus(function(){ $(this).addClas
我有以下 jQuery 函数: 提交表单 $(".content").delegate('.entryButton','click', function() {var form = $(this).c
如何使用 jQuery 在焦点/模糊上切换元素的 CSS? $('.answerSpace').bind('blur', function(){ $('.normProf').toggleClass(
在我的 iPhone 应用程序中,我有一个黑白 UIImage。我需要模糊该图像(高斯模糊即可)。 iPhone 显然知道如何模糊图像,如 it does that when it draws sha
这个问题已经有答案了: Blurring an image via CSS? (6 个回答) 已关闭 7 年前。 我有一个场景。我想随着循环的进行模糊我的图像。我怎样才能做到这一点?这是我的代码。
这个错误是在子字符串方法上抛出的,我发现很多线程都在处理这个问题,但我遇到的问题似乎有所不同。我知道如果您的字符串短于子字符串(开始,结束)大小,它会抛出此错误,但在任何内容传递到方法调用之前都会抛出
是否有简单的解决方案可以在 Qt 中为图像添加运动模糊?还没有找到任何关于模糊的好教程。我需要一些非常简单的东西,我可以理解,如果我可以改变模糊角度,那就太好了。 最佳答案 Qt 没有运动模糊过滤器。
我的搜索框在正常状态下很小。焦点对准时,它会展开,并显示一个提交按钮。这样做是为了节省空间。现在,在模糊时,搜索框再次缩小,提交按钮消失。 问题是,通过使提交按钮成为“竞赛”以在正确的位置单击它,对提
您好,我正在使用 PngBitmapEncoder 从 Canvas 在内存中创建图像。 public void CaptureGraphic() { Canvas canvas = new
我已经搜索过谷歌、这个和其他论坛,但无济于事……太棒了,有没有可能有像 onMiddleClick="blur();"这样的东西?在单击鼠标中键时隐藏链接的焦点边框? 最佳答案 $('a').clic
我无法在我的应用程序中正确渲染我的纹理。我使用的艺术品是精确的,并且已经缩放且尺寸合适,但是当我在手机上渲染它时,我的纹理突然不如原始艺术品清晰/精确,我不明白为什么。 有人遇到过这个问题吗? 最佳答
这里有与上述主题相同的问题但没有得到答复 我这里有布局 我需要在底部布局中使用与顶部布局相同的图像,但使用模糊样式 设置 alpha 没有帮助 - TextView 也会影响 如何虚化down布局的背
我已经搜索了一段时间,但到目前为止没有找到适合我需要的东西。 ( This was helpful, but not convincing ) 从两个不同的来源,我得到两个不同的字符串。我想检查较短的
我有这样的代码: var passwordTextBox = angular.element("#password"); passwordTextBox.blur(function()
设置此 JQuery 函数无法正常工作。有时,如果我单击元素,什么也没有发生,并且它会触发隐藏可折叠菜单的功能,如果单击文档上的任意位置,则不会重定向到正确的 href。有什么更好的方法吗? HTML
尝试通过将坐标列表保存到数组来在多个位置裁剪我的图像后,裁剪区域中的字母变得非常模糊,我无法弄清楚原因。 原图看起来像 裁剪后的图像看起来像 题中代码如下: import numpy as np im
我知道我们可以调暗/模糊屏幕,如 this post 所示. 我应该怎么做才能使它的一部分变暗/模糊,使单个(或多个) View 没有任何效果,从而使整个屏幕具有突出显示 View 的效果? 此外,即
如果有人曾经向 digg 提交过故事,它会检查该故事是否已经提交,我假设是通过模糊搜索。 我想实现类似的东西,想知道他们是否使用开源的 php 类? Soundex 不这样做,句子/字符串的长度可达
我是一名优秀的程序员,十分优秀!