关于mongodb:mongodb-增删查改-数据库实验

2次阅读

共计 2274 个字符,预计需要花费 6 分钟才能阅读完成。

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")

result :

正文完
 0