时间:2021-07-01 10:21:17 帮助过:20人阅读
mongo --quiet ip:host/dbname < test.js >> data
//--quiet执行静默模式,去掉系统打印信息
//ip mongo服务所在机的ip地址
//host mogo服务对外提供的访问端口
//dbname 是指要链接的数据库名字
// test.js 是我们要执行的js文件
// data 是我们输出的内容写入data文件里面
操作
SQ语法
Mongodb语法
建表
CREATE TABLE users (id MEDIUM INT NOTNULL AUTO_INCREMENT,user_idVarchar(30),ageNumber,statuschar(1),PRIMARYKEY(id))
db.users.insert({user_id:"abc123",age:55,status:"A"})
users集合可以不存在,在插入第一条时会创建,当然也可以
提前创建,执行语句:db.createCollection("users")
新增字段
ALTER TABLE users ADD join_date DATETIME
db.users.update({},{$set:{join_date:newDate()}},{multi:true})
删除字段
ALTER TABLE users DROP COLUMN join_date
db.users.update({},{$unset:{join_date:""}},{multi:true})
建立索引
CREATE INDEX idx_user_id_asc ON users(user_id)
db.users.createIndex({user_id:1})
建立索引指定排序
CREATE INDEX idx_user_id_asc_age_desc ON users(user_id,ageDESC)
db.users.createIndex({user_id:1,age:-1})
删除一个表
DROP TABLE users
db.users.drop()
插入数据
INSERTINT Ousers (user_id,age,status) VALUES ("bcd001",45,"A")
db.users.insert({user_id:"bcd001",age:45,status:"A"})
查询1
SELECT * FROM users
db.users.find()
查询2
SELECT id,user_id,status FROM users
db.users.find({},{user_id:1,status:1})
查询3
SELECT user_id,status FROM users
db.users.find({},{user_id:1,status:1,_id:0})
查询4
SELECT * FROM users WHERE status = "A"
db.users.find({status:"A"})
查询5
SELECT user_id,status FROM users WHERE status = "A"
db.users.find({status:"A"},{user_id:1,status:1,_id:0})
查询6
SELECT * FROM users WHERE status != "A"
db.users.find({status:{$ne:"A"}})
查询7
SELECT * FROM users WHERE status="A" AND age=50
db.users.find({status:"A",age:50})
查询8
SELECT * FROM users WHERE status="A" OR age = 50
db.users.find({$or:[{status:"A"},{age:50}]})
查询9
SELECT * FROM users WHERE age > 25
db.users.find( {age:{$gt:25}} )
查询10
SELECT * FROM users WHERE age < 25
db.users.find({age:{$lt:25}})
查询11
SELECT * FROM users WHERE age > 25 AND age <= 50
db.users.find({age:{$gt:25,$lte:50}})
查询12
SELECT * FROM users WHERE user_id like "%bc%"
db.users.find({user_id:/bc/})
查询13
SELECT * FROM users WHERE user_id like "bc%"
db.users.find({user_id:/^bc/})
查询14
SELECT * FROM users WHERE status="A" ORDER BY user_id ASC
db.users.find({status:"A"}).sort({user_id:1})
查询15
SELECT * FROM users WHERE status="A" ORDERBY user_id DESC
db.users.find({status:"A"}).sort({user_id:-1})
查询16
SELECT COUNT(*) FROM users
db.users.count() 或者
db.users.find().count()
查询17
SELECT COUNT (user_id) FROM users
db.users.count({user_id:{$exists:true}})
db.users.find({user_id:{$exists:true}}).count()
查询18
SELECT COUNT(*) FROM users WHERE age>30
db.users.count({age:{$gt:30}}) 或者
db.users.find({age:{$gt:30}}).count()
查询19
SELECTDISTINCT(status) FROMusers
db.users.distinct("status")
查询20
SELECT * FROM users LIMIT 1
db.users.findOne()
db.users.find().limit(1)
查询21
SELECT * FROM users LIMIT 5 , 10
db.users.find().limit(5).skip(10)
查询22
EXPLAIN SELECT * FROM users WHERE status="A"
db.users.find({status:"A"}).explain()
更新1
UPDATE users SET status="C" WHERE age>25
db.users.update({age:{$gt:25}},{$set:{status:"C"}},{multi:true})
更新2
UPDATE users SET age=age+3 WHERE status="A"
db.users.update({status:"A"},{$inc:{age:3}},{multi:true})
删除1
DELETE FROM users WHERE status="D"
db.users.remove({status:"D"})
删除2
DELETE FROM users
db.users.remove({})
版权声明:本文为博主原创文章,未经博主允许不得转载。
如何在shell下使用JavaScript和Mongodb交互?
标签:shell javascript mongodb