mongodb 基本查询操作


MongoDB 使用 insert() 或 save() 方法向集合中插入文档,语法如下:

db.<collection>.insert()
向集合中插入一个或多个文档
当我们向集合中插入文档时,如果没有给文档指定_id属性,则数据库会自动为文档添加_id该属性用来作为文档的唯一标识
_id我们可以自己指定,如果我们指定了数据库就不会在添加了,如果自己指定_id 也必须确保它的唯一性
db.COLLECTION_NAME.insert(document)
或
db.COLLECTION_NAME.save(document)

3.2 版本之后新增了 db.collection.insertOne() 和 db.collection.insertMany()。
db.collection.insertOne() 用于向集合插入一个新文档,语法格式如下:

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>
   }
)

db.collection.insertMany() 用于向集合插入一个多个文档,语法格式如下:

db.collection.insertMany(
   [ <document 1> , <document 2>, ... ],
   {
      writeConcern: <document>,
      ordered: <boolean>
   }
)

参数说明:
document:要写入的文档。
writeConcern:写入策略,默认为 1,即要求确认写操作,0 是不要求。
ordered:指定是否按顺序写入,默认 true,按顺序写入。

show dbs

admin 180.00 KiB
city 72.00 KiB
icmpy 344.00 KiB
config 96.00 KiB

use icmpy
向icmpy数据库的skd集合插入文档:

db.skd.insert({
name:"hyq",
age:19,
gender:"男",
hobby:["football","music","game"],
major:"计算机应用"
})
{
"acknowledged" : true,
"insertedIds" : {
"0" : "668f52f4301d1375d03a07f0"
}
}

//插入多个文档

db.skd.insert([
{
name:"wfq",
age:21,
gender:"男",
hobby:["football","running","history"],
major:"历史"
},
{
name:"shc",
age:18,
gender:"女",
hobby:["running","music","drawing"],
major:"医学"
},
{
name:"sq",
age:20,
gender:"女",
hobby:["running","music","dancing"],
major:"计算机信息管理"
},
])
{
"acknowledged" : true,
"insertedIds" : {
"0" : "668f528f301d1375d03a07ed",
"1" : "668f528f301d1375d03a07ee",
"2" : "668f528f301d1375d03a07ef"
}
}

//查询文档
db.collection.find(query, projection)
find()用来查询集合中所有符合条件的文档
find()可以接收一个对象作为条件参数
{} 表示查询集合中所有的文档
{属性:值} 查询属性是指定值的文档
find()返回的是一个数组

  • query :可选,使用查询操作符指定查询条件

  • projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。
    如果你需要以易读的方式来读取数据,可以使用 pretty() 方法,语法格式如下:

    *等于**{:**}**db.stus.find({“name”:”shc”}).pretty()**where name = ‘shc’*
    小于{:{$lt:}}db.stus.find({“age”:{$lt:20}}).pretty()where age <20
    小于或等于{:{$lte:}}db.stus.find({“age”:{$lte:30}}).pretty()where age <= 30
    大于{:{$gt:}}db.stus.find({“age”:{$gt:30}}).pretty()where age > 30
    大于或等于{:{$gte:}}db.stus.find({“age”:{$gte:30}}).pretty()where age >= 30
    不等于{:{$ne:}}db.stus.find({“age”:{$ne:30}}).pretty()where age != 30
    操作格式范例RDBMS中的类似语句

比如:
//查询名字是”sq”的文档:

db.skd.find({name:"sq"})
{
"_id" : "668f528f301d1375d03a07ef",
"name" : "sq",
"age" : 20,
"gender" : "女",
"hobby" : [
"running",
"music",
"dancing"
],
"major" : "计算机信息管理"
}

//查询年龄>20的文档:

db.skd.find({age:{$gt:20}})
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wfq",
"age" : 21,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "历史"
}

//查询年龄<=18的文档

db.skd.find({age:{$lte:18}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"age" : 18,
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}

//查询年龄不等于19的文档:

db.skd.find({age:{$ne:19}})
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wfq",
"age" : 21,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "历史"
}
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"age" : 18,
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}
{
"_id" : "668f528f301d1375d03a07ef",
"name" : "sq",
"age" : 20,
"gender" : "女",
"hobby" : [
"running",
"music",
"dancing"
],
"major" : "计算机信息管理"
}
{
"_id" : "668f52f4301d1375d03a07f0",
"name" : "sjk",
"age" : 20,
"gender" : "男",
"hobby" : [
"football",
"riding",
"running"
],
"major" : "计算机应用"
}

//查询19<年龄<21的文档

db.skd.find({age:{$lt:21,$gt:19}})
{
"_id" : "668f528f301d1375d03a07ef",
"name" : "sq",
"age" : 20,
"gender" : "女",
"hobby" : [
"running",
"music",
"dancing"
],
"major" : "计算机信息管理"
}
{
"_id" : "668f52f4301d1375d03a07f0",
"name" : "sjk",
"age" : 20,
"gender" : "男",
"hobby" : [
"football",
"riding",
"running"
],
"major" : "计算机应用"
}

And:MongoDB 的 find() 方法可以传入多个键(key),每个键(key)以逗号隔开,即常规 SQL 的 AND 条件。语法格式如下:

db.col.find({key1:value1, key2:value2}).pretty()

db.skd.find({“major”:”计算机应用”,”age”:{$gt:18}}).pretty()

{
"_id" : "668f50b5301d1375d03a07ec",
"name" : "hyq",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"music",
"game"
],
"major" : "计算机应用"
}
{
"_id" : "668f52f4301d1375d03a07f0",
"name" : "sjk",
"age" : 20,
"gender" : "男",
"hobby" : [
"football",
"riding",
"running"
],
"major" : "计算机应用"
}

Or:语法格式如下:

db.col.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

查询年龄>20或者major“医学”的文档

db.skd.find(
{
$or:[{age:{$gt:20}},{"major":"医学"}]
}
).pretty()
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wfq",
"age" : 21,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "历史"
}
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"age" : 18,
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}

// and & or联合使用

db.skd.find(
{
"age":{$lte:20},
$or:[
{"major":"计算机信息管理"},
{"hobby":"music"}
] 
}).pretty()
{
"_id" : "668f50b5301d1375d03a07ec",
"name" : "hyq",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"music",
"game"
],
"major" : "计算机应用"
}
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"age" : 18,
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}
{
"_id" : "668f528f301d1375d03a07ef",
"name" : "sq",
"age" : 20,
"gender" : "女",
"hobby" : [
"running",
"music",
"dancing"
],
"major" : "计算机信息管理"
}
db.skd.find(
{
"age":{$lte:19},
$or:[
{"major":"计算机信息管理"},
{"hobby":"music"}
] 
}).pretty()
{
"_id" : "668f50b5301d1375d03a07ec",
"name" : "hyq",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"music",
"game"
],
"major" : "计算机应用"
}
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"age" : 18,
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}
db.skd.find(
{
"age":{$lte:18},
$or:[
{"major":"计算机信息管理"},
{"hobby":"music"}
] 
}).pretty()
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"age" : 18,
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}

//查询总量count

db.skd.find().count()
5

Limit() 如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数。limit()方法基本语法如下所示:

db.COLLECTION_NAME.find().limit(NUMBER)

以下实例为显示查询文档中的两条记录:
//limit()

db.skd.find().limit(2)
{
"_id" : "668f50b5301d1375d03a07ec",
"name" : "hyq",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"music",
"game"
],
"major" : "计算机应用"
}
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wfq",
"age" : 21,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "历史"
}

Skip()我们除了可以使用limit()方法来读取指定数量的数据外,还可以使用skip()方法来跳过指定数量的数据,skip方法同样接受一个数字参数作为跳过的记录条数。skip() 方法脚本语法格式如下:

db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
db.col.find().limit(1).skip(1)

 分页 每页显示10条
	1-10     0
	11-20    10
	21-30    20
	...
skip((页码-1) * 每页显示的条数).limit(每页显示的条数);
	skip()用于跳过指定数量的数据    
 	MongoDB会自动调整skip和limit的位置

//显示第二条文档数据

db.skd.find().limit(1).skip(1)  
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wfq",
"age" : 21,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "历史"
}

sort() 方法 在 MongoDB 中使用 sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。sort()方法基本语法如下所示:

db.COLLECTION_NAME.find().sort({KEY:1})

以下实例演示了 stus 集合中的数据按字段 age 的降序排列:
//sort()


```shell
db.skd.find().sort({age:1})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"age" : 18,
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}
{
"_id" : "668f50b5301d1375d03a07ec",
"name" : "hyq",
"age" : 19,
"gender" : "男",
"hobby" : [
"football",
"music",
"game"
],
"major" : "计算机应用"
}
...

投影:在查询时,可以在第二个参数的位置来设置查询结果的投影:

对于想显示的字段,可以设置为1,_id默认必显示,我们可以设置为0隐藏

db.emp.find({},{ename:1 , _id:0 , sal:1});
db.skd.find({},{"name":1,"major":1})
{
"_id" : "668f50b5301d1375d03a07ec",
"name" : "hyq",
"major" : "计算机应用"
}
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wfq",
"major" : "历史"
}
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"major" : "医学"
}
{
"_id" : "668f528f301d1375d03a07ef",
"name" : "sq",
"major" : "计算机信息管理"
}
{
"_id" : "668f52f4301d1375d03a07f0",
"name" : "sjk",
"major" : "计算机应用"
}
db.skd.find({},{"name":1,age:1,"major":1})
{
"_id" : "668f50b5301d1375d03a07ec",
"name" : "hyq",
"age" : 19,
"major" : "计算机应用"
}
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wfq",
"age" : 21,
"major" : "历史"
}
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "shc",
"age" : 18,
"major" : "医学"
}
{
"_id" : "668f528f301d1375d03a07ef",
"name" : "sq",
"age" : 20,
"major" : "计算机信息管理"
}
{
"_id" : "668f52f4301d1375d03a07f0",
"name" : "sjk",
"age" : 20,
"major" : "计算机应用"
}

文章作者: 云上的小雨滴
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 云上的小雨滴 !
评论
  目录