首页 > 数据库 > MongoDB > 正文

一文看懂mongodb的增删查改操作

2022-07-04 18:37:57
字体:
来源:转载
供稿:网友
       mongodb是分布式文档存储数据库,有些朋友可能对于mongodb数据库增删查改的操作不是很清楚,这篇文章主要给大家介绍一下mongodb数据库增删查改,感兴趣的朋友就继续往下看吧。
 
       一:增加数据
 
       操作1:单条插入:Yelp数据库中的User数据集插入符和如下要求的数据
 
       _id:自定义为自己的班级;
 
       user_id:自己的学号+任意字符(多于22个字符取前22位,不足22个字符补充字母,数字或下划线);
 
       name:姓名拼音;
 
       review_count:任意随机数;
 
       yelping_since:实验时间;
 
       操作2:多条插入:
 
       随机构建4条User数据,有序插入User数据集中;
 
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: 插入多项数据:
 
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的记录;
 
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
 
db.user.update({_id: 2018211125},
 
 {name:"xiaoxiao", review_count: 0,yelping_since: ISODate("2020-11-18 21:58:51")})
 
 
       result: 查询后
 
       部分更新
 
       初始:
 
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条数据)
 
db.business.find({
 latitude: {
  "$gte": 30,
  "$lte": 50
 },
 state: "AZ"
}).limit(10)
       result:
 
 
 
       查询business集合内city为"Charlotte"或"Toronto"或“Scottsdale”的记录(跳过前510条数据)
 
db.business.find({
 city: {
  "$in": ["Charlotte", "Toronto", "cottsdale"]
 }
}).skip(150)
result :
 
 
 
       五索引:
 
       创建索引:friend数据集上,建立user_id(升序)与friend_id(降序)多字段唯一索引
 
db.friend.createIndex({user_id:1 ,friend_id: -1})
       result
 
 
 
       查看索引:
 
db.friend.getIndexes()
 
 
       六聚合:
 
       统计review数据集中stars大于2.0对应的不同user_id(作为_id)的stars评分总和(重命名为starSum)
 
db.review.aggregate([
 {
  $match: {
    "stars": {
   "$gte": 2.0
   }
  }
 },
 {
  $group: {
   _id: "$user_id",
  starSum:{
    $sum: "$stars"
   }
  }
 },
 
])
       result :
 
 
 
       统计friend数据集中friend_id为"BI4jBJVto2tEQ0NiaR0rNQ"的不同用户的总数(count)从第10条开始统计
 
db.friend.aggregate([
 {
  $match: {
   friend_id:"BI4jBJVto2tEQ0NiaR0rNQ"
  }
 },
 {
  $group: {
   _id: "$friend_id",
  Sum:{
    $sum: "$count",
   }
  }
 },
 
]).skip(10)
       result :
 
 
 
       统计friend数据集中不同的friend_id(distinct)
 
db.friend.distinct(
 "friend_id"
 )

(编辑:错新网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表