一、插入操作
1. 单个文档插入
db.COLLECTION_NAME.insert(document)
2. 批量文档插入
db.COLLECTION_NAME.insert([document,document1.....])
注:在高版本mongodb中又新增了
db.collection.insertOne()
和db.collection.insertMany()
这两个方法,分别用来插入单个文档和多个文档
二、查询操作
1. 查集合中所有文档
db.collection.find( {} )
等同于SQL查询中的 SELECT * FROM collection
2. 指定条件查询 { <field1>: <value1>}
例: db.users.find({"name":"zs"});
类似于 select * from t_user where name = 'zs'
注:在这里我们先学习下最基本的语法,更复杂的查询在后续介绍
三、修改文档
1. db.collection.update(<filter>, <update>);
2. db.collection.updateOne(<filter>, {<update operator>: { <field1>: <value1>, ... })
只修改符合条件的第一条文档
注:
$set
操作符 使用指定的value替换旧值,如果修改的属性不存在,则创建
3. db.collection.updateMany(<filter>, {<update operator>: { <field1>: <value1>, ... })
将符合条件的所有文档都进行修改
4. db.collection.replaceOne()
替换除_id外的所有属性
四、删除文档
1. db.collection.deleteOne({<field1>: <value1>,...})
2. db.collection.deleteMany({<field1>: <value1>,...})
在这里插入图片描述
五、 复杂查询
1. 使用查询操作符指定条件
语法: { <field1>: { <operator1>: <value1> }, ... }
- $in
db.collection.find( { status: { $in: [ "A", "D" ] } } )
等同于SELECT * FROM collection WHERE status in ("A", "D")
- $and | $lt
db.collection.find( { status: "A", qty: { $lt: 30 } } )
等同于SELECT * FROM collection WHERE status = "A" AND qty < 30
- $or
db.collection.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
等同于SELECT * FROM collection WHERE status = "A" OR qty < 30
- $and 和 $or
db.collection.find( { status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] } )
等同于SELECT * FROM collection WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
- $lt | $gt | $lte | $gte
2. 查询结果排序
语法: db.collection.find().sort({_id:1})
Note:
sort({排序字段:排序规则})
排序规则:1 升序 -1 降序
3. 分页查询
语法: db.collection.find().sort().skip(起始条数).limit(结束条数)
4. 总条数
语法: db.collection.find().count()
5. 模糊查询
语法: db.collection.find({"name":/zs/});