mongodb 常用查询API

mongodb 常用查询API:

db.book.find(0.count()

db.book.find({},{bookid,name,status}) //select bookid,name,status from book

db.book.find({age:{$gt:30}})
db.book.find({age:{$gte:30}})
db.book.find({age:{$lt:30}})

db.book.find({age:{$gt:30,$lt:50}})

db.book.find({$or:[{status:"A"},{age:50}]) //select * from book where status="A" or age=50
db.book.find({$and:[{status:"A"},{age:50}]) //select * from book where status="A" and age=50

db.book.find(bookid:{$regex: /bc/}) //select * from book where bookid like "%bc%
db.book.find(bookid:{$regex: /^bc/}) //select * from book where bookid like "bc%

db.book.find({type:"travel"}).sort({age:1}) //1 升序,-1降序

db.book.find().skip(16).limit(10) //skip 跳过的记录,limit 返回的记录

db.book.updateMany({age:{$gt:25},{$set:{status:"A"}})
db.book.updateMany({status:"A"},{$inc:{age:3}}) // update book set age=age=3 where status="A"

db.book.updateMany({type:"novel"},{set:{data:new Date()}})
db.book.findAndModity({query:{_id:ObjectId("6243dasasfsaa")},update:{$inc:{price:10}}) //+10
db.book.findAndModity({query:{_id:ObjectId("6243dasasfsaa")},update:{$inc:{price:-10}}) //-10

db.book.getIndex
db.users.createIndex({username : 'hashed'})
1 # 创建唯一索引
db.values.createIndex({title:1},{unique:true})
2 # 复合索引支持唯一性约束
db.values.createIndex({title:1,type:1},{unique:true})
3 #多键索引支持唯一性约束
db.inventory.createIndex( { ratings: 1 },{unique:true} )

查询数据的分布
db.product.getShardDistribution()

posted @ 2024-10-17 18:02  大树2  阅读(9)  评论(0编辑  收藏  举报