// 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"})
mongo 常用语法
最新推荐文章于 2022-09-30 13:56:29 发布