mongodb
安装:`apt-get install mongodb`
基本使用
show dbs查看数据库use <db name>创建并切换数据库- 如果新建数据库的内容为空,用
show dbs不会显示空的新建数据库
- 如果新建数据库的内容为空,用
db dropDatabase()删除当前所在的数据库
集合的操作语句
db.createCollections("<collection name>")创建集合show collections查看集合db.<collection name>.drop删除集合db.<collection name>.insert(value)向集合中插入数据- 如果集合不存在,会自动创建集合
db.student.insert({"name": "ivy", sex: "male", age: 22})单条插入db.student.insert([{"name": "ivy", sex: "male", age: 22}, {"name": "ivy", sex: "male", age: 22}])多条插入- 如果不指定_id参数,mongodb会自动给该条数据制定一个唯一的_di
db.<collection name>.find()无条件查询db.<collection name>.find(condictions)** 有条件查询**db.student.find({age: 22})
db.<collection name>.find().pretty()格式化输出
逻辑运算符
| 操作符 | 描述 |
|---|---|
| $ne | 不等于 |
| $gt | 大于 |
| $lt | 小于 |
| $gte | 大于等于 |
| $lte | 小于等于 |
-
使用
db.<collection name>.find({age: {$lte: 25}})查找年龄小于等于25的数据
-
db.<collection name>.find({$or: [{condiction}, {condiction}, ....]})** or查询**- db.
.find({$or: [{name: ivy}, {age: 23}]})`` 查找name为ivy或者age为23的数据
- db.
-
db.<collection name>.find({$and: [{condiction}, {condiction}, ....]})and查询 -
db.<collection name>.update(<query>, <value>,{multi:false})更新数据, 当multi为true时,会修改所有匹配到的数据db.<collection name>.update({name: ivy},{age:20})将name为ivy的数据的全部改为age为20 全字段更新db.<collection name>.update({name: ivy},{$set: {age:20}})将name为ivy的数据的age该为20 局部字段更新
-
db.<collection name>.remove(<query>, {justOne: false})