Mongodb语法总结
mongodb与mysql命令对比 mongodb语法MongoDB对数据的操作很丰富,下面做一些举例说明,内容大部分来自官方文档,另外有部分为自己理解。
db.colls.find() //select * from colls
db.colls.find({‘last_name’: ‘Smith’});//select * from colls where last_name=’Smith’
db.colls.find( { x : 3, y : “foo” } );//select * from colls where x=3 and y=’foo’
db.colls.find({j: {$ne: 3}, k: {$gt: 10} });//select * from colls where j!=3 and k>10
db.colls.find({}, {a:0});//查询除a为0外的所有数据
db.colls.find({ “field” : { $gt: value } } ); db.colls.find({ “field” : { $gte: value } } );也可对某一字段做范围查询不等于查询用字符$nein查询用字符$indb.colls.find({j:{$in: }});
db.colls.find({j:{$nin: }});
db.colls.find( { a : { $mod : [ 10 , 1 ] } } )// where a % 10 == 1
db.colls.find( { a: { $all: [ 2, 3 ] } } );//指定a满足数组中任意值时
db.colls.find( { a : { $size: 1 } } );//对对象的数量查询,此查询查询a的子对象数目为1的记录
db.colls.find( { a : { $exists : true } } ); // 存在a对象的数据$type查询$type值为bsonhttp://bsonspec.org/数 据的类型值db.colls.find( { a : { $type : 16 } } ); // 匹配a为int类型数据
db.colls.find( { name : /acme.*corp/i } );//类似于SQL中like
db.colls.find( { “author.name” : “joe” } );
db.colls.find( { name : { $not : /acme.*corp/i } } );sort()排序limit()对限制查询数据返回个数skip()跳过某些数据snapshot()快照保证没有重复数据返回或对象丢失
db.students.find({‘address.state’ : ‘CA’}).count();//效率较高group()对查询结果分组和SQL中group by函数类似shell命令操作语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的。1、Help查看命令提示
[*]> help
[*]> db.help();
[*]> db.yourColl.help();
[*]> db.youColl.find().help();
[*]> rs.help();
当创建一个集合(table)的时候会自动创建当前数据库4、删除当前使用数据库
[*]> db.dropDatabase();
将指定机器上的数据库的数据克隆到当前数据库将本机的mydb的数据复制到temp数据库中8、查看当前使用的数据库
[*]> db.getName();
[*]> db;
9、显示当前db状态
[*]> db.stats();
11、查看当前db的链接机器地址
[*]> db.getMongo();
1、创建一个聚集集合(table)
[*]> db.createCollection(“collName”, {size: 20, capped: 5, max: 100});
3、得到当前db的所有聚集集合
[*]> db.getCollectionNames();
? 用户相关添加用户、设置密码、是否只读3、显示当前所有用户
[*]> show users;
? 其他2、清除错误记录
[*]> db.resetError();
三、Collection聚集集合操作1、查看帮助
[*]> db.yourColl.help();
3、查看数据空间大小
[*]> db.userInfo.dataSize();
5、得到当前聚集的状态> db.userInfo.stats();7、聚集集合储存空间大小> db.userInfo.storageSize();9、聚集集合重命名> db.userInfo.renameCollection("users");10、删除当前聚集集合> db.userInfo.drop();1、查询所有记录> db.userInfo.find();默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。注意:键入it命令不能带“;”2、查询去掉后的当前聚集集合中的某列的重复数据> db.userInfo.distinct("name");相当于:select distict name from userInfo;相当于:select * from userInfo where age = 22;相当于:select * from userInfo where age > 22;相当于:select * from userInfo where age < 22;相当于:select * from userInfo where age >= 25;8、查询age >= 23 并且 age <= 26> db.userInfo.find({age: {$gte: 23, $lte: 26}});相当于:select * from userInfo where name like ‘%mongo%’;select * from userInfo where name like ‘mongo%’;相当于:select name, age from userInfo;12、查询指定列name、age数据, age > 25> db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});13、按照年龄排序降序:> db.userInfo.find().sort({age: -1});相当于:select * from userInfo where name = ‘zhangsan’ and age = ‘22’;相当于:select top 5 * from userInfo;相当于:select * from userInfo where id not in ( select top 10 * from userInfo );可用于分页,limit是pageSize,skip是第几页*pageSize相当于:select * from userInfo where age = 22 or age = 25;相当于:select top 1 * from userInfo;> db.userInfo.find().limit(1);相当于:select count(*) from userInfo where age >= 20;相当于:select count(sex) from userInfo;1、创建索引> db.userInfo.ensureIndex({name: 1});> db.userInfo.ensureIndex({name: 1, ts: -1});3、查看总索引记录大小> db.userInfo.totalIndexSize();5、删除指定索引> db.users.dropIndex("name_1");? 修改、添加、删除集合数据添加的数据的数据列,没有固定,根据添加的数据为准相当于:update users set name = ‘changeName’ where age = 25;> db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);相当于:update users set age = age + 50, name = ‘hoho’ where name = ‘Lisi’;4、查询修改删除 view plaincopy
[*]> db.users.findAndModify({
[*]... query: {age: {$gte: 25}},
[*]... sort: {age: -1},
[*]... update: {$set: {name: 'a2'}, $inc: {age: 2}},
[*]... remove: true
[*]... });
[*]
[*]> db.runCommand({ findandmodify : "users",
[*]... query: {age: {$gte: 25}},
[*]... sort: {age: -1},
[*]... update: {$set: {name: 'a2'}, $inc: {age: 2}},
[*]... remove: true
[*]... });
修改器对象<span]1、简单Hello World
[*]> print("Hello World!");
2、将一个对象转换成json
[*]> tojson(new Object());
[*]> tojson(new Object('a'));
这样就循环添加了30条数据,同样也可以省略括号的写法
[*]> for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
4、find 游标查询
[*]>var cursor = db.users.find();
[*]> while (cursor.hasNext()) {
[*]... printjson(cursor.next());
[*]... }
同样可以省略{}号forEach中必须传递一个函数来处理每条迭代的数据信息取得下标索引为4的那条数据那样我们也可以用循环显示数据
[*]> for (var i = 0, len = c.length(); i < len; i++) printjson(c);
用toArray方法将其转换为数组只显示age <= 28的并且只显示age这列数据
[*]> db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson);
[*]> db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);
9、forEach传递函数显示信息
[*]> db.things.find({x:4}).forEach(function(x) {print(tojson(x));});
那么关于mongodb的shell操作的讲解就先到这了,基本涵盖了mongodb最为常用的操作方法,那么下一节我们会讲述用java如何驱动mongodb,即所谓的CRUD。
页:
[1]