好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mongodb数据库实验之增删查改

mongodb 数据库 实验

一:增加数据

操作1:单条插入:Yelp数据库中的User数据集插入符和如下要求的数据

_id:自定义为自己的班级;

user_id:自己的学号+任意字符(多于22个字符取前22位,不足22个字符补充字母,数字或下划线);

name:姓名拼音;

review_count:任意随机数;

yelping_since:实验时间;

操作2:多条插入:

随机构建4条User数据,有序插入User数据集中;

?

1

2

3

4

5

6

7

8

9

db. user . insert (

  {

   _id: 2018211,

   user_id: 201821057900000000000000000000000,

   name : "xiao" ,

   review_count: 100,

   "yelping_since" : ISODate( "2020-11-17 07:58:51" ),

  }

)

the result

2: 插入多项数据:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

db. user .insertMany(

  [ {

   _id: 201821112,

   user_id: 201811111111111111111111,

   name : "xiaoxiao" ,

   review_count: 1,

   "yelping_since" : ISODate( "2020-11-18 07:58:51" ),

  },

    {

   _id: 201821114,

   user_id: 201822222222222222222,

   name : "xuexiao" ,

   review_count: 344,

   "yelping_since" : ISODate( "2030-11-18 07:58:51" ),

  },

    {

   _id: 201821117,

   user_id: 201833333333333333333,

   name : "xiaoxiao" ,

   review_count: 56,

   "yelping_since" : ISODate( "2020-11-19 07:58:51" ),

  },]

  

)

the result

二:删除数据

删除指定条件的数据:删除business数据集中 stars小于3且city位于Las Vegas的记录;

?

1

2

3

4

5

6

db.business.remove({

  "city" : "Las Vegas" ,

  stars: {

   $lt:3

  }

})

result :

三: 更新数据

整体更新:将1.1中插入的数据整体更新

user_id:自己的班级+任意字符(多于22个字符取前22位,不足22个字符补充字母,数字或下划线);

name:姓名拼音倒序;

review_count:任意随机数(与之前不同);

yelping_since:当前实验时间(与之前不同);

操作5:局部更新

​ 将business数据集内business_id为"8mIrX_LrOnAqWsB5JrOojQ"的记录对应的stars增加0.5

?

1

2

3

db. user . update ({_id: 2018211125},

 

  { name : "xiaoxiao" , review_count: 0,yelping_since: ISODate( "2020-11-18 21:58:51" )})

result: 查询后

部分更新

初始:

?

1

2

3

4

db.business. update ({business_id:8mIrX_LrOnAqWsB5JrOojQ},

{ "$inc" :{stars:0.5}

}

)

进行部分更新, 再次查询结果为:

四:查询

1: 查询business集合内latitude大于30,longitude小于50,state位于AZ的10条记录

查询business集合内city为"Charlotte"或"Toronto"或[Scottsdale]的记录(跳过前510条数据)

?

1

2

3

4

5

6

7

db.business.find({

  latitude: {

   "$gte" : 30,

   "$lte" : 50

  },

  state: "AZ"

}).limit(10)

result:

查询business集合内city为"Charlotte"或"Toronto"或[Scottsdale]的记录(跳过前510条数据)

?

1

2

3

4

5

db.business.find({

  city: {

   "$in" : [ "Charlotte" , "Toronto" , "cottsdale" ]

  }

}).skip(150)

result :

五索引:

创建索引:friend数据集上,建立user_id(升序)与friend_id(降序)多字段唯一索引

?

1

db.friend.createIndex({user_id:1 ,friend_id: -1})

result

查看索引:

?

1

db.friend.getIndexes()

六聚合:

统计review数据集中stars大于2.0对应的不同user_id(作为_id)的stars评分总和(重命名为starSum)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

db.review.aggregate([

  {

   $match: {

     "stars" : {

    "$gte" : 2.0

    }

   }

  },

  {

   $ group : {

    _id: "$user_id" ,

   starSum:{

     $ sum : "$stars"

    }

   }

  },

 

])

result :

统计friend数据集中friend_id为"BI4jBJVto2tEQ0NiaR0rNQ"的不同用户的总数(count)从第10条开始统计

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

db.friend.aggregate([

  {

   $match: {

    friend_id: "BI4jBJVto2tEQ0NiaR0rNQ"

   }

  },

  {

   $ group : {

    _id: "$friend_id" ,

   Sum :{

     $ sum : "$count" ,

    }

   }

  },

 

]).skip(10)

result :

统计friend数据集中不同的friend_id(distinct)

?

1

2

3

db.friend. distinct (

  "friend_id"

  )

result :

总结

到此这篇关于mongodb数据库实验之 增删查改 的文章就介绍到这了,更多相关mongodb增删查改 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://segmentfault.com/a/1190000038218558

查看更多关于mongodb数据库实验之增删查改的详细内容...

  阅读:26次