(本文假设你已经安装好mongodb数据库)

1.首先让我们在命令行中进入到mongodb中,本例中使用Linux(ubuntu),如有不适,请见谅.

打开终端,输入mongo ,将显示(mongodb 的版本,如图所示):

mongodb的 命令行简单使用

2.然后输入,

show dbs (用于显示当前有多少数据库)

然后是,use mydb(某数据库名字)

接着 show collections (显示当前数据库有那些集合)

如图所示:

mongodb的 命令行简单使用

 

 

 

 

 

 

 

然后,进入到下一步.

 

--查询--

1、查询所有记录

db.person.find();   //person 为集合名字

2.查询细分

查询age = 22的记录

db.person.find({"age": 22});

查询age>22的记录

db.person.find({age: {$gt: 22}});

查询age<22的记录

db.person.find({age: {$lt: 22}});

查询age>= 25的记录

db.person.find({age: {$gte: 25}});

查询age>= 23 并且 age<= 26

db.person.find({age: {$gte: 23, $lte: 26}});

查询name中包含 mongo的数据

db.person.find({name: /mongo/});

查询name中以mongo开头的

db.person.find({name: /^mongo/});

查询指定列name、age数据

db.person.find({}, {name: 1, age: 1});

按照年龄排序

升序:db.person.find().sort({age: 1});

降序:db.person.find().sort({age: -1});

查询前5条数据

db.person.find().limit(5);

查询10条以后的数据

db.person.find().skip(10);

or与 查询

db.person.find({$or: [{age: 22}, {age: 25}]});

查询第一条数据

db.person.findOne();

查询某个结果集的记录条数

db.person.find({age: {$gte: 25}}).count();

--以上查询方法,可方便用于代码插入的数据 查找--

对于数据库的其他的操作,下次补上。谢谢

相关文章:

  • 2021-10-04
  • 2021-11-22
  • 2022-12-23
  • 2021-08-22
  • 2021-12-04
  • 2021-04-27
  • 2021-08-22
  • 2022-01-09
猜你喜欢
  • 2021-09-18
  • 2021-06-04
  • 2022-02-03
  • 2021-11-07
  • 2022-02-07
  • 2021-05-03
  • 2021-05-24
相关资源
相似解决方案