IT评测·应用市场-qidao123.com技术社区

标题: MongoDB 基础语法【增删改查】 [打印本页]

作者: 滴水恩情    时间: 2024-11-29 22:23
标题: MongoDB 基础语法【增删改查】
前言

作为软件测试工程师,我们常用的是查询操作以下是我工作中处理处罚的一些题目,非基础教程分享,是工作履历分享。mongodb中查询用的也是比较多,用法也比较多,但此次查询不做深入的讲解,底下有截图查询的常用语法,后续会出一篇完整的讲解,需要资料的小伙伴也可以私信我。
1、mongodb集合插入数据:insertMany 与 insertOne
题目:统计列表由于每天只能天生一条数据,每页的数据是十条,为了上线前验证前端界面分页查询及展示。需要通过sql进行造数据。
  1. db.lucky_gift_statistics.insertMany([
  2.   {
  3.     "date": "2024-02-15",
  4.     "loseCoin": NumberLong("1260"),
  5.     "theNumberOfPeople": NumberLong("3"),
  6.     "theNumberOfVisitors": NumberLong("11"),
  7.     "winCoin": NumberLong("1980"),
  8.     "poolValueBalance": NumberLong("3325"),
  9.     "winLoseCoin": NumberLong("720")
  10.   },
  11.   {
  12.     "date": "2024-02-16",
  13.     "loseCoin": NumberLong("1340"),
  14.     "theNumberOfPeople": NumberLong("4"),
  15.     "theNumberOfVisitors": NumberLong("12"),
  16.     "winCoin": NumberLong("2020"),
  17.     "poolValueBalance": NumberLong("3425"),
  18.     "winLoseCoin": NumberLong("680")
  19.   },
  20.   {
  21.     "date": "2024-02-17",
  22.     "loseCoin": NumberLong("1500"),
  23.     "theNumberOfPeople": NumberLong("5"),
  24.     "theNumberOfVisitors": NumberLong("15"),
  25.     "winCoin": NumberLong("2100"),
  26.     "poolValueBalance": NumberLong("3500"),
  27.     "winLoseCoin": NumberLong("600")
  28.   },
  29.   {
  30.     "date": "2024-02-18",
  31.     "loseCoin": NumberLong("1700"),
  32.     "theNumberOfPeople": NumberLong("6"),
  33.     "theNumberOfVisitors": NumberLong("16"),
  34.     "winCoin": NumberLong("2200"),
  35.     "poolValueBalance": NumberLong("3650"),
  36.     "winLoseCoin": NumberLong("500")
  37.   },
  38.   {
  39.     "date": "2024-02-19",
  40.     "loseCoin": NumberLong("1800"),
  41.     "theNumberOfPeople": NumberLong("7"),
  42.     "theNumberOfVisitors": NumberLong("18"),
  43.     "winCoin": NumberLong("2400"),
  44.     "poolValueBalance": NumberLong("3800"),
  45.     "winLoseCoin": NumberLong("600")
  46.   }
  47. ]);
复制代码
讲解 
该语句的作用是将 5 条记录批量插入到 MongoDB 的 lucky_gift_statistics 集合中。
db.lucky_gift_statistics.insertMany({...})



2、mongodb集合删除数据:insertMany 与 insertOne
题目:需求测试过程中由于bug等各种原因会产生一些脏数据,为了解决报错等信息我们需求手动进行删除对应的脏数据。先查询报错信息然后确定题目。
  1. db.lucky_gift_statistics.deleteMany({
  2.   _id: {
  3.     $in: [
  4.       ObjectId("66f3d84d9b06489828bc8e43"),
  5.                 ObjectId("66f3d84d9b06489828bc8e42")
  6.     ]
  7.   }
  8. });
复制代码
 该语句的作用是批量删除 lucky_gift_statistics 集合中指定id的数据。



 3、修改mongodb集合数据:updateOneupdateMany
题目:有些功能的推送只在特定的国家进行推送,我们需要测试符合条件和不符合条件的国家地区有无收到。此时配置是读取redis火缓存的数据,redis则是从mongodb上读取对应的字段,所以我们需要修改mongodb的字段。
  1. db.biz_config.updateOne({"key":"cps-register-reward-config"},
  2. {$set:{"eliminateCountryCode":"IN,ID,JP"}})
复制代码
讲解 



 4、查询mongodb集合数据:find
 题目:有些数据前端界面不展示大概不方便查询出来,此时假如要排查题目可以查询查询数据库进行查看,测试历史渠道统计——新增字段3天充值率时界面没有充值人数的字段,此时界面没法判断是人数统计有误,没有统计还是运算有误等原因,数据库则有记录,此时便可以边操作边查询数据库。
  1. db.channel_daily_statistics.find(
  2. {"channelNo":"FacebookAds",createDate: ISODate("2023-07-20T00:00:00.000Z"),
  3. childChannelNo:null}).sort({_id:-1})
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4