MongoDB(1)

2021-06-13  本文已影响0人  royluck
db.shsxt.insert({
  "name": "roy"
})
db.shsxt.find()
db.dropDatabase()
db.createCollection("shsxt") // 创建集合(表)
// MongoDB不需要创建集合,在创建文档时会自动创建集合
db.table1.insert({"a":1}) # 当第一个文档插入时,集合就会被创建并包含该文档
db.table2 # 创建一个空集合
db.shsxt.drop() # 删除集合
image.png

注:表和库最好不要重名,因为你删表会同时删了库;

insert()
save()
insertMany() 
update() // 等价于updateOne()
updateOne()
save() // 更新必须指定id
db.user.update(
{}, // 所有集合
{"$inc"}: { "age": -1}, // age字段减1
{"multi": true}
)
remove() // 移除集合中的数据
deleteOne() // 删除符合条件的第一个文档
db.user.remove({}) // 等价于 db.user.deleteMany({})
db.user.find()
db.user.distinct('name') // 去重
db.user.find().pretty() // 以格式化的方式来显示所有文档
= 
!= ('$ne')
> ('$gt')
< ('$lt')
>= ('$gte')
<= ('$lte')
image.png
$and
$or
$not
image.png
image.png
image.png
image.png
$in 
$nin
image.png image.png
db.user.find({
"hobbies": { $all: ["read", "music"] }
})
image.png
image.png
image.png
嵌入文档
上一篇 下一篇

猜你喜欢

热点阅读