连接mongodb
mongo;
查询数据库名称
show tables;
查询所有数据集合
show collections;
创建一个名为"table1"的集合
db.createCollection(“table1”);
删除集合(指定需要删除的集合名称)操作:
db.table1.drop();
创建索引,索引字段为title
db.table1.createIndex({“title”:1})
查询语句
查询所有记录
db.userInfo.find();
查询所有记录数据数量
db.userInfo.find().count();
查询某个结果集的记录条数
db.userInfo.find({age: {$gte: 25}}).count();
只显示特定字段name的值(1为显示,0为不显示)
db.userInfo.find({},{name:1});
查询指定列 name、age 数据, age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
查询去掉后的当前聚集集合中的某列的重复数据
db.userInfo.distinct(“name”);
查询 age = 22 的记录
db.userInfo.find({“age”: 22});
查询范围gt(大于) 、lt(小于) 、gte(大于等于) 、lte(小于等于)
- 查询 age > 22 的记录
db.userInfo.find({age: {$gt: 22}}); - 查询 age < 22 的记录
db.userInfo.find({age: {$lt: 22}});