Mongodb基本使用
链接: mongodb官方文档crud操作
db.[collectionName]和db.getCollection([collectionName])的区别:
后者接受集合名称包含空格或连字符、以数字开头或与内置函数冲突
1.创建操作
执行文档的写入操作时,若集合不存在会创建该集合.
单条文档的写入操作都是原子性的.
1.1 单条写入
db.collection.insertOne()
db.collection.insertMany()
db.collection.insert()
2.读取操作
db.collection.find()
指定过滤条件的查询
3.更新操作
# update操作只是更新部分字段,replace操作是替换整个文档
db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <update>, <options>)
db.collection.update()
4.删除操作
db.collection.deleteMany()
db.collection.deleteOne()
db.collection.remove()