-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmongo.txt
More file actions
executable file
·111 lines (88 loc) · 4.47 KB
/
Copy pathmongo.txt
File metadata and controls
executable file
·111 lines (88 loc) · 4.47 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
命令行
use 切换表
mongodump 备份全部数据, 需启动mongod
mongorestore 回复数据,需启动mongod
db.createCollection(name, {capped: true, size: 1000}) 创建表 固定大小的表,数据太多会覆盖之前的
db.collection.drop() 删除数据
db.aa 表 aa
.count({}) 相当于find({}).count()
.insert insertOne insertMany 插入数据
.update(query,{$set: {newdata}, upsert, multi}) 更新数据,不存在是否删除,是否更新多条
.remove({data}) 删除
.find({data}, {name: 0}) 查找: name 不返回, 1为返回。
.limit(Number) 查询条数
.skip(Number) 忽略前xx条
.sort({age: -1}) 1升序,-1 降序
.ensureIndex({"title":1, 'age': 1}, {background: true}) 建立索引, 会提设置了索引字段的搜索速度(按title,age搜索)。background 等需要查表
.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}]) 聚合后的result 是个数组,_id 使用by_user字段的值,num_tutorial, num_tutorial相似的id 相加条数
查询语句 参数
{$or:[{data}, {data2}]} 或者
key: {$lt: 50} $lt小于 $gte 大于等于 $lte 小于等于 $gt 大于
key: {$type: 2} 2 String $type值需查表
key: {$in:['xxx', 'xx']}
原子操作命令
$set 用来指定一个键并更新键值,若键不存在并创建。
$unset 用来删除一个键。
$inc 可以对文档的某个值为数字型(只能为满足要求的数字)的键进行增减的操作。 $inc:{age: -1} 年龄-1
$push { $push : { field : value } 把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去
$pushAll { $pushAll : { field : value_array } } 同$push,只多个值到一个数组字段内。
$pull { $pull : { field : _value } } 从数组field内删除一个等于value值。是一次可以追加
$addToSet 增加一个值到数组内,而且只有当这个值不在数组内才增加。
$pop { $pop : { field : 1 } } 删除数组的第一个或最后一个元素
$rename { $rename : { old_field_name : new_field_name } }
$ne { field: { $in: [<value1>, <value2>, ... <valueN> ] } } 字段值不等于
$nin { field: { $nin: [ <value1>, <value2> ... <valueN> ]} } 字段值不在指定数组或者不存在
ObjectId("5aeab5907ab3603042ee5275").getTimestamp() 获取创建时间
new ObjectId("5aeab5907ab3603042ee5275").str 返回字符串
mongose
链接
mongoose.connect(dbConf.url)
mongoose
.connection
.on('error', console.log)
.once('open', _ => console.log(`success: db connect to ${dbConf.url}`.blue))
模板
Schema = new mongoose.Schema({
account: { type: String, required: true },
password: { type: String, required: true },
nickName: { type: String, default: '' },
headUrl: { type: String, default: '' , unique: true}, 重复将报错
age: { type: Number }
})
// schema 也可是json 但这样写可以在schema上添加methods.func
User = mongoose.model('User', Schema)
子文档
parentSchema = new mongoose.Schema({child: childSchema }) 子文档会有_id
添加数据
let user = new User({ account: 'gs', password: 'password3' })
user.account = 'ddd'
user.save()
或者: User.create({account: 'gs',password: 'pass'})
修改数据
User.update()
User.findByIdAndUpdate(id, {$set:{data}})
查询数据
find(query, '要显示的字段 xx xx', function(err, data)) 查询出来的字段如果没有,则空着,不会改变查出来的条数
上面相当于
find(query).select('xx, xx').then()
中间件 // 不好用
Schema
schema.path('keypath').validate(function(value)) 验证某个字段
.pre('save', async func) 在save之前触发
.post('save', func(doc)) 后中间件,在某个事件之后触发
remove
validate 在pre save 之前触发
init
populate({path: key, select: 'email' , populate: {xx}, model: 跨数据库链接model名字}})
select: 需要的属性
A中表的一个属性指向B表一个_id 将A表属性替换成B表的所有数据
store中必须定义 link:{ type: Schema.Types.ObjectId, ref: 'B表的名字', refPath: 'connections.name'}
Store.findOne({ name: 'cc' }).populate('link').then()
或者、
store.virtual('虚拟属性', {
ref: 'tablename',
localField: 'storekey' store 表的字段
foreignField: 'aa' 另一张表对应的字段
})
Store.find({}).populate('虚拟属性').then 虚拟属性的值 就是 另一张表.find({aa:storeValue})
操作符