mongodb是一个文档数据库。
https://blog.csdn.net/qq_44300280/article/details/123936134
java客户端使用:https://article.itxueyuan.com/MRjEAp
go客户端使用:
命令行客户端命令:
查询:
db.$collectionName.find():查全表数据
db.$collectionName.distinct(“name”):等价于select distinct name from t
db.$collectionName.find({“name”:”张三”}):等价于select * from t where name = ‘张三’
db.$collectionName.find({“age”:18}):等价于select * from t where age = 18
db.$collectionName.find({“age”:{$gt:18}}):等价于select * from t where age >18
db.$collectionName.find({“age”:{$gte:18}}):等价于select * from t where age >= 18
db.$collectionName.find({“age”:{$lt:60}}):等价于select * from t where age < 60
db.$collectionName.find({“age”:{$lte:60}}):等价于select * from t where age <= 60
db.$collectionName.find({“age”:{$gt:18,$lt:60}}):等价于select * from t where age > 18 and age < 60
db.$collectionName.find({}, {“name”:1, “age”:1}):等价于select name, age from t
db.$collectionName.find({“age”:{$gt:18}}, {“name”:1}):等价于select name from t where age > 18
db.$collectionName.find({}, {“name”:0}):查询name之外的全部列
db.$collectionName.find().sort({“age”:1}):等价于select * from t order by age
db.$collectionName.find().sort({“age”:-1}):等价于select * from t order by age desc
db.$collectionName.find({“name”:”张三”, “age”: 55}):等价于select * from t where name = ‘张三’ and age = 55
db.$collectionName.find().limit(5):等价于select * from t limit 5
db.$collectionName.find().skip(10):跳过前10条数据
db.$collectionName.find().limit(10).skip(5):等价于select * from t limit 5, 10,即跳过前5条数据,查询10条数据
db.$collectionName.find({$or:[{“age”:20}, {“age”:30}]}):等价于select * from t where age = 20 or age = 30
db.$collectionName.findOne():等价于select * from t limit 1
db.$collectionName.find().count():等价于select count(1) from t
新增:
db.$collectionName.save({“name”:”李四”, “age”:25}):等价于insert into t (name, age) values (‘李四’, 25)
修改:
db.$collectionName.update({“name”:”李四”}, {$set:{“age”:60}}, false, true):等价于update t set age = 60 where name = ‘李四’,第一个false表示的是,第二个true表示的是
db.$collectionName.update({“name”:”李四”}, {$inc:{“age”:10}}, false, true):等价于update t set age = age + 10 where name = ‘李四’
db.$collectionName.update({“name”:”李四”}, {$set:{“name”:”张三”}, $inc:{“age”:10}}, false, true):等价于update t set name = ‘张三’, age = age + 10 where name = ‘李四’
删除:
db.$collectionName.remove({“name”:”李四”}):等价于delete * from t where name = ‘李四’
清空表数据:
db.$collectionName.drop():等价于truncate table t
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289958.html