# mysql-shell
# 把字段 x 值为 1 的记录更新为 MySQL
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.modify('x=1').set('x','mysql')
Query OK, 1 item affected (0.0047 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# 检索 x='mysql' 的记录
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.find("x='mysql'")
{
"x": "mysql",
"y": 2,
"z": 101,
"_id": "00005e006018000000000000000c"
}
1 document in set (0.0006 sec)
# 更新字段 y 值为 'dble' 没有 where 过滤条件,也就是更新全表
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.modify("true").set('x','dble')
Query OK, 10 items affected (0.0075 sec)
Rows matched: 10 Changed: 10 Warnings: 0
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.find();
{
"x": "dble",
"y": 0,
"z": 100,
"_id": "00005e006018000000000000000b"
}
...
{
"x": "dble",
"y": 18,
"z": 109,
"_id": "00005e0060180000000000000014"
}
10 documents in set (0.0010 sec)
MySQL ytt-pc:33050+ ssl ytt JS >
# 比如现在要把刚才的 c1 数组嵌入到字段 x 中,该怎么做呢?
# 把 x 置为空数组
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.modify("true").set("x",[])
Query OK, 10 items affected (0.0048 sec)
Rows matched: 10 Changed: 10 Warnings: 0
# 用 array push 的方法更新字段 x
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.modify("true").arrayAppend("$.x",c1);
Query OK, 10 items affected (0.0064 sec)
Rows matched: 10 Changed: 10 Warnings: 0
# 查看刚才更新的结果(简化显示)
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.find()
{
"x": [
[
{"x": 0, "y": 0, "z": 100},
{ "x": 1,"y": 2,"z": 101},
{"x": 2,"y": 4,"z": 102},
{"x": 3,"y": 6,"z": 103},
{"x": 4,"y": 8,"z": 104},
{"x": 5,"y": 10,"z": 105},
{"x": 6,"y": 12,"z": 106},
{"x": 7,"y": 14,"z": 107},
{"x": 8, "y": 16,"z": 108},
{"x": 9,"y": 18,"z": 109}
]
],
"y": 0,
"z": 100,
"_id": "00005e006018000000000000000b"
},
...
10 document in set (0.0003 sec)
# 那看下删除操作
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.remove('true');
Query OK, 10 items affected (0.0032 sec)
# 数据已全部清空
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.find()
Empty set (0.0003 sec)
那最重要的是查询。查询通过 find() 方法过滤。看下面的例子。
#mysql-shell
# 重新插入 10W 条记录
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.remove('true');
Query OK, 10 items affected (0.0043 sec)
MySQL ytt-pc:33050+ ssl ytt JS > var c1 = []
MySQL ytt-pc:33050+ ssl ytt JS > for (var i =0;i< 100000;i++){c1.push({'x':i,'y':2*i,'z':i+100})}
100000
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.add(c1)
Query OK, 100000 items affected (2.5686 sec)
Records: 100000 Duplicates: 0 Warnings: 0
# 拿出过滤条件为 x>100 and x < 200 的记录,倒序输出前两条记录
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.find('x >100 and x < 200').sort(' x desc').limit(2)
{
"x": 199,
"y": 398,
"z": 299,
"_id": "00005e00601800000000000000e6"
}
{
"x": 198,
"y": 396,
"z": 298,
"_id": "00005e00601800000000000000e5"
}
2 documents in set (0.0766 sec)
# 查询时间 0.0766 秒
# 给字段 x 加一个索引,也非常方便
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.createIndex('idx_x',{fields:[{'field':'$.x','type':'int'}]});
Query OK, 0 rows affected (0.2854 sec)
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.find('x >100 and x < 200').sort(' x desc').limit(2)
{
"x": 199,
"y": 398,
"z": 299,
"_id": "00005e00601800000000000000e6"
}
{
"x": 198,
"y": 396,
"z": 298,
"_id": "00005e00601800000000000000e5"
}
2 documents in set (0.0004 sec)
# 查询时间0.0004秒
# 执行事物块更简单,转而使用 session 对象
# 类似 start transaction 语句
MySQL ytt-pc:33050+ ssl ytt JS > session.startTransaction()
Query OK, 0 rows affected (0.0002 sec)
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.remove('x=1')
Query OK, 1 item affected (0.0004 sec)
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.find('x=1')
Empty set (0.0004 sec)
# 类似 rollback 语句
MySQL ytt-pc:33050+ ssl ytt JS > session.rollback()
Query OK, 0 rows affected (0.0014 sec)
# 这条记录还在
MySQL ytt-pc:33050+ ssl ytt JS > db.f1.find('x=1')
{
"x": 1,
"y": 2,
"z": 101,
"_id": "00005e0060180000000000000020"
}
1 document in set (0.0004 sec)