mongo 常用语法

// mysql: select * from users;
db.appResource.find()

// mysql: select name, skills from users;
db.appResource.find({},{"appType":1})

// mysql: select name, age, skills from users where name = 'hurry';
db.appResource.find({"appType":"ncp"},{"appType":1})

// MySQL:select name, age, skills from users where name = 'hurry' and age = 18;
db.appResource.find({"appType":"ncp","desc":"测试"})

// MySQL:select name, age, skills from users where name = 'hurry' or age = 18;
db.appResource.find({'$or':[{"appType":"ncp"},{"desc":"测试"}]},{})

// MySQL:select * from users where age >= 20 and age <= 30;
db.users.find({'age' : {'$gte' : 20, '$lte' : 30}});

// 使用in, not in ($in, $nin)
// MySQL:select * from users where age >= 20 and age <= 30;
db.appResource.find({'version': {'$in': [0, 1]}});

// 匹配null
// MySQL:select * from users where age >= 20 and age <= 30;
db.appResource.find({'appType': null});

// like (mongoDB 支持正则表达式)
// select * from users where name like "%hurry%";
db.appResource.find({'appType': /ncp/});
// select * from users where name like "hurry%";
db.appResource.find({'appType': /^ncp/});

// =================================================================
// select count(*) from users
db.appResource.count()

// 数组查询 (mongoDB自己特有的)
// 如果skills是 ['java','python']
db.users.find({'skills': 'java'});

// $all skills中必须同时包含java 和 python
db.users.find({'skills': {'$all': ['java', 'python']}})

// 正则表达式
db.appResource.find({link: /.*baidu*/i})

// $elemMatch
db.appResource.find({"classifications": {"$elemMatch": {"_id": 1750020198238208}}})
//嵌套对象属性查询
db.appResource.find({"profile.appIdentify": "APP020200409002"})

// $not 取反
db.appResource.find({link: {"$not": /.*baidu*/i}})

//分页排序查询,倒序(-1),正序(1)
db.appResource.find({link: {"$not": /.*baidu*/i}}).sort({"createTime":1}).skip(0).limit(2);

// 整个表删除
// db.collection.drop()

// 删除某些条件的数据,删除type类型是test的数据
// db.collection.remove({"type" : "test"})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值