目录
Spring boot中mongoDB的查询--Query, QueryBuilder、BasicQuery
>菜鸟教程:http://www.runoob.com/html/html5-websocket.html
学习任务:
how mongodb working
WebSocket,
MongoDB Queries
Json data format
学习网站:
mongodb
>中文手册:http://www.mongodb.org.cn/manual/
>菜鸟教程:http://www.runoob.com/mongodb/mongodb-tutorial.html
>大佬博客1:http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html
>大佬博客2:https://www.cnblogs.com/tugenhua0707/p/9250673.html
>杂网站
>>易佰教程:https://www.yiibai.com/mongodb/>QQ群:227270512
练习/在线编译:
>实验楼:https://www.shiyanlou.com/courses/12
MongoDB笔记:
安装配置:
mongodb连接到Robo 3T:
Q:在Robo 3T上连接不上,具体情况忘记复制黏贴下来了,报错好像是network
A:在D盘新建一个文件夹data,data里新建db->打开mongodb的bin文件夹里的mongod.exe(cmd界面它会自己把东西下载到db文件夹里)
MongoDB Queries
网站:
>官方手册:https://docs.mongodb.com/manual/tutorial/query-documents/(看不懂英文,暂不看)
v大佬博客
>大佬博客1:https://www.cnblogs.com/jjg0519/p/6733061.html
>大佬博客2:https://www.cnblogs.com/malcolmfeng/p/6924562.html
>大佬博客3:https://blog.csdn.net/russle/article/details/80373560
>大佬博客1:https://www.cnblogs.com/jjg0519/p/6733061.html
MongoDB-Query查询条件
Query.All("name", "a","b"); //通过多个元素来匹配数组
Query.And(Query.EQ("name","a"), Query.EQ("title", "t"));//同时满足多个条件
Query.EQ("name", "a");//等于
Query.Exists("type", true);//判断键值是否存在
Query.GT("value", 2);//大于>
Query.GTE("value", 3);//大于等于>=
Query.In("name", "a","b");//包括指定的所有值,可以指定不同类型的条件和值
Query.LT("value", 9);//小于<
Query.LTE("value", 8);//小于等于<=
Query.Mod("value", 3, 1);//将查询值除以第一个给定值,若余数等于第二个给定值则返回该结果
Query.NE("name", "c");//不等于
Query.Nor(Array);//不包括数组中的值
Query.Not("name");//元素条件语句
Query.NotIn("name", "a",2);//返回与数组中所有条件都不匹配的文档
Query.Or(Query.EQ("name","a"), Query.EQ("title", "t"));//满足其中一个条件
Query.Size("name", 2);//给定键的长度
Query.Type("_id", BsonType.ObjectId);//给定键的类型
Query.Where(BsonJavaScript);//执行JavaScript
Query.Matches("Title",str);//模糊查询 相当于sql中like -- str可包含正则表达式
>大佬博客2:https://www.cnblogs.com/malcolmfeng/p/6924562.html
Query操作
一、shell执行mongodb查询(简单json数据结构)
查询所有:db.inventory.find()
按条件查询:db.inventory.find( { status: "D" } )
in条件查询:db.inventory.find( { status: { $in: [ "A", "D" ] } } )
and和范围条件查询:db.inventory.find( { status: "A", qty: { $lt: 30 } } )
or条件查询:db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
and和or的复合条件:db.inventory.find( {status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]})
查询一条:db.collection.findOne()
二、python执行mongodb查询(简单json数据结构)
查询所有:cursor = db.inventory.find({})
条件查询:cursor = db.inventory.find({"status": "D"})
in条件查询:cursor = db.inventory.find({"status": {"$in": ["A", "D"]}})
and条件查询:cursor = db.inventory.find({"status": "A", "qty": {"$lt": 30}})
or条件查询:cursor = db.inventory.find({"$or": [{"status": "A"}, {"qty": {"$lt": 30}}]}
and和or的复合条件:cursor = db.inventory.find({"status": "A","$or": [{"qty": {"$lt": 30}}, {"item": {"$regex": "^p"}}]})
查询一条:pymongo.collection.Collection.find_one()
三、java执行mongodb查询(简单json数据结构)
查询所有:FindIterable<Document> findIterable = collection.find(new Document());
条件查询:findIterable = collection.find(eq("status", "D"));
in条件查询:findIterable = collection.find(in("status", "A", "D"));
and条件查询:findIterable = collection.find(and(eq("status", "A"), lt("qty", 30)));
or条件查询:findIterable = collection.find(or(eq("status", "A"), lt("qty", 30)));
and和or的复合条件:findIterable = collection.find(and(eq("status", "A"),or(lt("qty", 30), regex("item", "^p"))));
四、嵌套json数据格式的查询:
1.shell方式:
如果数据格式如下:
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
那么,shell如下用.的方式进行json数据获取,也可以进行各种条件的查询。
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )能查到
db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )查不到 (json中的数据一体化,位置不可移动)
db.inventory.find( { "size.uom": "in" } ) # 执行条件的查询
db.inventory.find( { "size.h": { $lt: 15 } } ) # 范围条件的查询
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } ) #and条件的查询
2.python方式:
如果数据格式如下:
from bson.son import SON
db.inventory.insert_many([
{"item": "journal",
"qty": 25,
"size": SON([("h", 14), ("w", 21), ("uom", "cm")]),
"status": "A"},
{"item": "notebook",
"qty": 50,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"status": "A"},
{"item": "paper",
"qty": 100,
"size": SON([("h", 8.5), ("w", 11), ("uom", "in")]),
"status": "D"},
{"item": "planner",
"qty": 75,
"size": SON([("h", 22.85), ("w", 30), ("uom", "cm")]),
"status": "D"},
{"item": "postcard",
"qty": 45,
"size": SON([("h", 10), ("w", 15.25), ("uom", "cm")]),
"status": "A"}])
python嵌入式查询语句如下:
cursor = db.inventory.find({"size": SON([("h", 14), ("w", 21), ("uom", "cm")])})
cursor = db.inventory.find({"size": SON([("w", 21), ("h", 14), ("uom", "cm")])})
cursor = db.inventory.find({"size.uom": "in"})
cursor = db.inventory.find({"size.h": {"$lt": 15}})
cursor = db.inventory.find({"size.h": {"$lt": 15}, "size.uom": "in", "status": "D"})
五、嵌套array数据格式的查询:
1.shell查询array嵌套数据如下:
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);
shell查询语句如下:
db.inventory.find( { tags: ["red", "blank"] } ) # 查询array固定内容的数据
db.inventory.find( { tags: { $all: ["red", "blank"] } } ) # 数据含有 red和blank同时有 的数据
db.inventory.find( { tags: "red" } ) # 数据含有 red 的数据
db.inventory.find( { dim_cm: { $gt: 25 } } ) # 数据含有 数据大于25 的数据
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } ) # 数据含有 其中一个数据大于15 另一个数据小于20 的数据
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } ) # 数据含有 可以同时满足 大于22并且小于30 的数据
db.inventory.find( { "dim_cm.1": { $gt: 25 } } ) # 查询dim_cm中的第二个元素大于25 的数据
db.inventory.find( { "tags": { $size: 3 } } ) # 查询tags array中的元素为3个 的数据
python查询语句如下:
cursor = db.inventory.find({"tags": ["red", "blank"]})
cursor = db.inventory.find({"tags": {"$all": ["red", "blank"]}})
cursor = db.inventory.find({"tags": "red"})
cursor = db.inventory.find({"dim_cm": {"$gt": 25}})
cursor = db.inventory.find({"dim_cm": {"$gt": 15, "$lt": 20}})
cursor = db.inventory.find({"dim_cm": {"$elemMatch": {"$gt": 22, "$lt": 30}}})
cursor = db.inventory.find({"dim_cm.1": {"$gt": 25}})
cursor = db.inventory.find({"tags": {"$size": 3}})
六、同时嵌套array和json数据格式的查询:
如果数据如下:
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
shell查询语句如下:
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } ) # 查询含有 instock array中的json元素为{ warehouse: "A", qty: 5 } 的数据
db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } ) # 查询含有 instock array中的json元素为{qty: 5, warehouse: "A" } 的数据查不到 (json中的数据一体化,位置不可移动)
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } ) # 查询含有 instock中的第一个json元素中的qty字段小于或等于 20 的数据
db.inventory.find( { 'instock.qty': { $lte: 20 } } ) # 查询含有 instock中的任意json元素中的qty小于或等于 20 的数据
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } ) # 查询含有 instock中的json元素 同时满足qty为5,warehouse为A 的数据
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } ) # 查询含有 instock中的json元素 同时满足qty大于10并小于等于20 的数据
# 如果不用 $elemMatch: 函数
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )# 查询含有 instock中的qty为5 或者 instock中的warehousr为A 的数据
db.inventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } ) # 查询含有 instock中的qty 大于10 或 小于等于20 的数据
七、筛选查询结果的字段:
如果数据结构为:
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
查询语句和字段筛选的操作为:
db.inventory.find( { status: "A" } ) # 管道1:查询status为A的数据
db.inventory.find( { status: "A" }, { item: 1, status: 1 } ) # 管道1:查询status为A的数据; 管道2:只显示item和status字段 (和_id字段 默认显示)
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } ) # 管道1:查询status为A的数据; 管道2:只显示item和status字段 (隐藏_id字段)
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } ) # 管道1:查询status为A的数据; 管道2:除去status和instock 其他都显示
db.inventory.find({ status: "A" }, { item: 1, status: 1, "size.uom": 1 } ) # 管道1:查询status为A的数据; 管道2:只显示item和status和size中的uom字段
db.inventory.find({ status: "A" },{ "size.uom": 0 }) # 管道1:查询status为A的数据; 管道2: 除去size中的uom字段 其他都显示
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } ) # 管道1:查询status为A的数据; 管道2:只显示 item和status和instock中的qty字段
db.inventory.find( { status: "A" }, { name: 1, status: 1, instock: { $slice: -1 } } ) # 管道1:查询status为A的数据; 管道2:只显示name和status和instock中的倒数第一个元素
八、字段不存在和null字段的不同之处:
数据为:
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
查询语句为:
db.inventory.find( { item: null } ) # 查询所有 item不存在或为null 的数据
db.inventory.find( { item : { $type: 10 } } ) # 查询所有 item存在 但是为null 的数据
db.inventory.find( { item : { $exists: false } } ) # 查询所有 不存在item 的数据
>大佬博客3:https://blog.csdn.net/russle/article/details/80373560
Spring boot中mongoDB的查询--Query, QueryBuilder、BasicQuery
WebSocket
网站:
>菜鸟教程:http://www.runoob.com/html/html5-websocket.html
>菜鸟教程:http://www.runoob.com/html/html5-websocket.html
1.以下 API 用于创建 WebSocket 对象。
var Socket = new WebSocket(url, [protocol] );
第二个参数 protocol 是可选的,指定了可接受的子协议。
2.属性:
属性 | 描述 |
---|---|
Socket.readyState | 只读属性 readyState 表示连接状态,可以是以下值:
|
Socket.bufferedAmount | 只读属性 bufferedAmount 已被 send() 放入正在队列中等待传输,但是还没有发出的 UTF-8 文本字节数。 |
3.事件
事件 | 事件处理程序 | 描述 |
---|---|---|
open | Socket.onopen | 连接建立时触发 |
message | Socket.onmessage | 客户端接收服务端数据时触发 |
error | Socket.onerror | 通信发生错误时触发 |
close | Socket.onclose | 连接关闭时触发 |
4.方法:
方法 | 描述 |
---|---|
Socket.send() | 使用连接发送数据 |
Socket.close() | 关闭连接 |
5.实例:
附加头信息"Upgrade: WebSocket"
javascript/html在客户端:
runoob_websocket.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script type="text/javascript">
function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("您的浏览器支持 WebSocket!");
// 打开一个 web socket
var ws = new WebSocket("ws://localhost:9998/echo");
ws.onopen = function()
{
// Web Socket 已连接上,使用 send() 方法发送数据
ws.send("发送数据");
alert("数据发送中...");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
alert("数据已接收...");
};
ws.onclose = function()
{
// 关闭 websocket
alert("连接已关闭...");
};
}
else
{
// 浏览器不支持 WebSocket
alert("您的浏览器不支持 WebSocket!");
}
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:WebSocketTest()">运行 WebSocket</a>
</div>
</body>
</html>
6.安装 pywebsocket
在执行以上程序前,我们需要创建一个支持 WebSocket 的服务。从 pywebsocket 下载 mod_pywebsocket ,或者使用 git 命令下载:
git clone https://github.com/google/pywebsocket.git
mod_pywebsocket 需要 python 环境支持
mod_pywebsocket 是一个 Apache HTTP 的 Web Socket扩展,安装步骤如下:
-
解压下载的文件。
-
进入 pywebsocket 目录。
-
执行命令:
$ python setup.py build $ sudo python setup.py install
-
查看文档说明:
$ pydoc mod_pywebsocket
7.开启服务
在 pywebsocket/mod_pywebsocket 目录下执行以下命令:
$ sudo python standalone.py -p 9998 -w ../example/
以上命令会开启一个端口号为 9998 的服务,使用 -w 来设置处理程序 echo_wsh.py 所在的目录。
//没成功!!python命令怎么运行!!
1012见有道云