The Hard Way
做个全栈
真是不容易啊,前后端都要懂,还得能写数据库的东西,连运维也给一起兼了。
JavaScript
现在JavaScript
真是完成了屌丝逆袭的全过程。
原来这货长这样:
<div onclick="function()"> foo </div>
现在这货长这样:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
听说下个版本要从0.12.7
直接跳到4.0
随着js
雄起的还有json
这货长这样:
{
"key":"value"
}
这是一种数据传输的新形式,好吧,其实他也老大不小了。
随着这种数据形式的自然就有数据库。
号称是NoSQL
(Not Only SQL)
不扯了,开始说用法吧。
Symbols
这里是一些符号的转义
大于 | $gt |
---|---|
大于等于 | $gte |
小于 | $lt |
小于等于 | $lte |
不等 | $ne |
包含于 | $in |
不包含于 | $nin |
Insert
db.collection.insert(
{
name:'Annatar',
age:20
}
)
这里的执行环境是JavaScript
哦,所以呢,可以使用For
循环,那么,做点儿什么吧!
for (i = 0; i < 100; i++)
db.collection.insert({x:1})
Select
// 相当于 SELECT * FROM `collection`
db.collection.find()
find()
中,可以添加条件
这一条意思是:
从users
里面找数据,age
大于18的,找name
和address
这两个字段,找五条
db.users.find(
{
age:{
$gt: 18
}
},
{
name: 1,
address: 1
}
)
.limit(5)
全文查询
db.collection.find({
$text: {
$search: "coffee"
}
})
-
表示没有,例如 ‘aa bb -cc’ 有aa,有bb,但是没有cc”” 表示
和
,但是需要转义。
相似度查询
db.collection.find({
$text: {$search: "aa bb"}
},
{
$score: {$meta: "text Score"}
})
.sort({
score: {$meta: "text Score"}
})
update
完全更新:
把x
等于1的都更新成999,如果不存在就创建(insert)
db.collection.update(
{
x:1
},
{
x:999
},
true
)
部分更新:
把x
等于1的字段,只更新y
到222,其他不变
db.collection.update(
{
x:1
},
{
$set: {
y:222
}
}
)
多表更新:
db.collection.update(
{
c:1
},
{
$set:{
c:2
}
},
false,
true
)
Remove
删除掉status
为D
的字段
db.users.remove(
{
status: "D"
}
)
Drop
删除掉collection
吧,看他不顺眼了。
db.collection.drop()
limit…
在collection
寻找数据,先跳过20条数据,然后只取两条数据,这两条数据要按照x
正序排列
其实英文稍微好一些,这都能看懂
db.collection.find()
.skip(20)
.limit(2)
.sort({ x:1 })
Notice
skip
语句会消耗较多的性能,能不用就别用
Index
索引可以加快搜索速度,在几百万条数据的时候就更突出性能优势了
查看索引
db.collection.getIndex()
创建索引
按照x
正向排序
db.collection.ensureIndex({
x:1
})
全文索引:
//key 是变量,而text是不变的
db.collection.ensureIndex({ key: "text" })
// 多关键字查询
db.collection.ensureIndex({
key_1: "text",
key_2: "text"
})
// 所有词均为关键字的索引
db.collection.ensureIndex({ "$**": "text" })
命名索引
db.collection.ensureIndex({ x:1, y:1 }, {name: "foo" }, {unique: true})