BlueXIII's Blog

热爱技术,持续学习

0%

Mongodb学习笔记

brew安装

1
2
3
4
5
6
brew services stop mongodb
brew uninstall mongodb

brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-community

如遇报错BadValue: Invalid value for version, found 3.6, expected '4.2' or '4.0'.,备份并删除/usr/local/var/mongodb

基本操作

连接

1
2
mongo --host 127.0.0.1
show dbs

创建db

1
2
user school
db.school.insert({name:'为民小学',age:10});

查看删除db

1
2
db.getName()
db.dropDatabase()

查看帮助

1
db.school.help()

查看数据库下的集合

1
show collections

创建集合

1
2
3
db.createCollection('grade3')
db.grade1.insert({name: 'Lily', age: 8})

插入文档

1
2
3
db.grade1.insert({_id: '1',name: 'Han Meimei', age: 8})
db.grade1.save({_id: '1',name: 'Han Meimei', age: 9})
db.grade1.save({_id: '2',name: 'Han Meimei', age: 9})

更新文档

1
2
3
4
5
6
7
8
db.collection.update(
<query>,
<updateObj>,
{
upsert: <boolean>,
multi: <boolean>
}
)
1
2
3
4
5
6
7
8
9
db.grade1.update({name:'Tom'}, {$push: {'hobby':'reading'} })
db.grade1.update({_id:3}, {$addToSet: {friends:'huge'}})
db.grade1.update({_id:3}, {$pop:{friends: 1}})
db.grade1.update({_id:3}, {$addToSet:{friends:{$each: ['huangbo','zhangyixing']}}})
db.grade1.update({_id:3}, {$push:{friends:{$each: ['huangbo','zhangyixing']}}})
db.grade1.update({name: 'Han Meimei', hobby:{$ne:'reading'}, _id: {$ne:'2'}}, {$push: {hobby: 'drinking'}})
db.grade1.update({_id:3}, {$set:{"info11":{id:'11'}}})
db.grade1.update({name: 'Tom'}, {$unset:{'age':''}})

删除文档

1
2
3
4
5
6
db.collection.remove(
<query>,
{
justOne: <boolean>
}
)
1
2
db.grade1.remove({'name': 'Han Meimei'}, {justOne: true})
db.grade1.remove({'name': 'Han Meimei'})

查询文档

1
db.collection_name.find(query, projection);
1
2
3
4
5
6
db.grade1.find()
db.grade1.find({age:{$in:[9,11]}})
db.grade1.find({age:{$nin:[9,11]}})
db.grade1.find({age:{$not:{$lt:11}}})
db.grade1.find({age:{$ne:9}})

数组的用法

1
2
3
4
5
6
7
db.grade1.find({"friends" : [ "Lily", "Jobs", "Lucy", "Zhang San" ]})
db.grade1.find({"friends" : [ "Lily" ]})
db.grade1.find({"friends" :{$all: ["Zhang San"]}})
db.grade1.find({"friends" :{$in: ["Zhang San"]}})

db.grade1.find({$where:'this.name == "Tom1"'})
db.grade1.find({$where: function(){return this.age == 9}})

RoboMongo

https://robomongo.org/

mysql&postgre对nosql的支持

https://juejin.im/post/5cefd68551882559685b3c1c
https://learnku.com/laravel/t/13185/in-depth-understanding-of-json-data-type-of-mysql-nosql-in-relational-database
https://www.oschina.net/translate/friendfeed-schemaless-mysql-new

另一种思路,mysql5.7.8+ , postgre9.3+已经原生对nosql支持,提供json数据类型,天然就解决了动态表单受schema约束的问题
动态表单程序只需要提供mongo/mysql/postgres等多种数据源的可选项即可
对于业务系统来说,spring data对json类型支持比较好,mybatis较差需要写一些代码手工映射

参考文档

https://juejin.im/post/5add9e655188256735642122
https://juejin.im/entry/5926ca5044d9040064076569
https://segmentfault.com/a/1190000012357379
http://mysql.taobao.org/monthly/2019/03/02/
https://cloud.tencent.com/developer/article/1166593
https://www.oschina.net/translate/friendfeed-schemaless-mysql-new
https://docs.mongodb.com/manual/data-modeling/