Common statements in MongoDB

MOngoDB delete statement

delete() delete

  1. Delete a collection

db.collection.deleteOne()

  1. Delete multiple collections

db.collection.deletMany();

remove() delete

  1. Delete all name: Li Si's data

db.student.remove({name:"李四"});

  1. Delete only one sex: only delete one male data

db.student.remove({sex:"男"},true);

  1. Delete all

db.student.remove({});

Fake database deletion

Sometimes when the user deletes the operation, the demand is like this, only to hide the data, not to delete it from the database.
At this time, fake deletion is used,
For example, these are two Weibo posts by Zhang Sanfa:

db.student.insert([
    {name:"张三",content:"今天心情好",isDel:0},
    {name:"张三",content:"今天心情一般",isDel:0},
]);

b21d2b7140562e21fcbe9c168e02da9.png

The user adds two pieces of data, but only keeps the latter one and deletes the previous one. At this time, false deletion is used, and a field isDel:0
So when the user deletes data, it is not the remove method but the update method that is executed.

db.student.update({"_id" : ObjectId("5bd6a46f1eb7a22fa07cb382")},{
    $set:{
      isDel:1
    }
});

98cf763807e662cd724e6d65f1638fb.png
When isDel:0 means that the user has not been deleted, 1 means that the user has been deleted

So when querying, you need to filter the name and isDel conditions.

db.student.find({name:"张三",isDel:0});

Query the data that the user did not delete:

121b7fc4d6f40af1de843359cdbf585.png

Then you can implement false deletion.

Operation and modification of bulk data

  1. Insert 10,000 documents into the collection
var arr= [];
for(var i=0;i<10000;i++){
   arr.push({counter:i});
}
db.demos.insert(arr);
db.demos.find();
  1. Query the document whose counter is 666 in demos

db.demos.find({counter:666});

  1. Query documents with counter less than 66 in demos

db.demos.find({counter:{$lt:666}});

  1. Query the documents of counter T666 in demos

db.demos.find({counter:{$gt:666}});

  1. Query documents in demos whose counter is greater than 66 and less than 666 1120 Check the top 10 data in the demos collection

db.demos.find({counter:{$gt:66, $lt:666}});

  1. Check the first to the 20th data in the demos collection

db.demos.find().limit(10);

  1. The number of data paging function skips from the 21st to 30th items in the Zhachun demos collection starts and limits how many items are queried each time
db.demos.find().skip(0).limit(10);//第一页 从0条开始 每查询10条
db.demos.find().skip(10).limit(10);//第二页 从10条开始 每查询10条
db.demos.find().skip(20).limit(10);//第三页 从20条开始 每查询10条

Document relationship in collection

  1. One to one:

For example: person and ID card husband and wife

  1. One to many:

For example: parent and child users and items

  1. Many to many:

For example: teachers and students

One to one

Reflected in the form of embedded documents,

//一对一
db.aAndb.insert([
 {name:"杨过",wife:{name:"小龙女",sex:"女"},sex:"男"},
  {name:"杨过",wife:{name:"小龙女",sex:"女"},sex:"男"}
])

db.aAndb.find();

One to many

Realized in the form of embedded documents or in the form of collections

//一对多  比如  微博 和 微博评论
//添加微博
db.weibo.insert([
{weibo:"世界这么大,我想去看看"},
{weibo:"我要做一名web开发者!!!"}
])

db.weibo.find();

add comment

db.comments.insert([
{
weibo_id: ObjectId("5bdd89e06a5e78f4cfc2b9c8"),
list:[
   "那你有钱吗",
    "一个人吗??去呢啊??",
    "加油!!"
]
},
{
weibo_id: ObjectId("5bdd89e06a5e78f4cfc2b9c9"),
list:[
   "那你要学习HTML",
   "那还要你要学习css",
    "加油!!"
]
}
]);

db.comments.find();

Query one-to-many

var weibo_id= db.weibo.findOne({"weibo" : "世界这么大,我想去看看"})._id;
db.comments.find({weibo_id: weibo_id});

Many-to-many relationship

For example: students and teachers
Can be associated through multiple documents,

//多对多  老师《------》学生

//插入老师集合
db.teachers.insert([
{
  name:"语文老师",
  teacher_id: 1,
  student_id:[
     1001,
     1002,
     1003
  ]
  },
{
  name:"数学老师",
  teacher_id: 2,
  student_id:[
     1001,
     1002,
     1003
  ]
  },
{
  name:"英语老师",
  teacher_id: 3,
  student_id:[
     1001,
     1002,
     1003
  ]
 }
])

db.teachers.find();


//插入学生集合
db.students.insert([
{
  name:"小明",
  student_id: 1001,
  teacher_id:[
     1,
     2,
     3
  ]
  },
{
  name:"小红",
  student_id: 1002,
  teacher_id:[
     1,
     2,
     3
  ]
  },
{
  name:"小刚",
  student_id: 1003,
  teacher_id:[
     1,
     2,
     3
  ]
 }
])

db.students.find();
db.teachers.find();

Sorting and indexing

Sort:

When querying documents, the default is to sort according to the value of _id (ascending order)
sort() can be used to specify the sorting rules of the documents, sort() needs to pass an object to specify the sorting rules of the documents, where 1 means ascending order, -1 means descending order
The order of limit skip sort can be changed arbitrarily, and it will be adjusted automatically during runtime.
I don’t want it to be sorted by id by default, I want it to be sorted by salary

//按照工资升序排列

db.section.find().sort({wages:1});

//优先按照工资升序排列  如果遇到相同的就在  按照id升序排列
db.section.find().sort({wages: 1},{_id: -1});

index:

Part of the content in the display field
Or extract part of the content in this field
When querying, you can set the result projection of the query in the second parameter

Index: find({ query condition}, {search range (1 display 0 hide)})
Note: _id is not set, the default is 1 (display), which can be hidden manually

db.section.find({}, {name: 1});

//只显示name和wages字段
`db.section.find({}, {name: 1, _id: 0, wages: 1});`

a07a1f2945204ef671ddebc28871c00.png


九旬的博客
关注我,每周输出一篇前端技术文章, 博客地址:[链接]

关注我,每周输出一篇前端技术文章,

893 声望
1.2k 粉丝
0 条评论
推荐阅读
【译】20个实用的JavaScript技巧
JavaScript不断发展壮大,因为它是最容易上手的语言之一,因此为市场上的新“成为技术怪才”打开了大门。(真假的啊?)的确,JavaScript可以做很多出色的事情!还有很多东西要学习。而且,无论您是JavaScript的新...

九旬4阅读 978

从零搭建 Node.js 企业级 Web 服务器(零):静态服务
过去 5 年,我前后在菜鸟网络和蚂蚁金服做开发工作,一方面支撑业务团队开发各类业务系统,另一方面在自己的技术团队做基础技术建设。期间借着 Node.js 的锋芒做了不少 Web 系统,有的至今生气蓬勃、有的早已夭折...

乌柏木143阅读 12k评论 10

从零搭建 Node.js 企业级 Web 服务器(十五):总结与展望
总结截止到本章 “从零搭建 Node.js 企业级 Web 服务器” 主题共计 16 章内容就更新完毕了,回顾第零章曾写道:搭建一个 Node.js 企业级 Web 服务器并非难事,只是必须做好几个关键事项这几件必须做好的关键事项就...

乌柏木60阅读 6k评论 16

再也不学AJAX了!(二)使用AJAX ① XMLHttpRequest
「再也不学 AJAX 了」是一个以 AJAX 为主题的系列文章,希望读者通过阅读本系列文章,能够对 AJAX 技术有更加深入的认识和理解,从此能够再也不用专门学习 AJAX。本篇文章为该系列的第二篇,最近更新于 2023 年 1...

libinfs39阅读 6.2k评论 12

封面图
从零搭建 Node.js 企业级 Web 服务器(一):接口与分层
分层规范从本章起,正式进入企业级 Web 服务器核心内容。通常,一块完整的业务逻辑是由视图层、控制层、服务层、模型层共同定义与实现的,如下图:从上至下,抽象层次逐渐加深。从下至上,业务细节逐渐清晰。视图...

乌柏木41阅读 7.2k评论 6

CSS 绘制一只思否猫
欢迎关注我的公众号:前端侦探练习 CSS 有一个比较有趣的方式,就是发挥想象,绘制各式各样的图案,比如来绘制一只思否猫?思否猫,SegmentFault 思否的吉祥物,是一只独一无二、特立独行、热爱自由的(&gt;^ω^&lt...

XboxYan42阅读 2.8k评论 14

封面图
还在用 JS 做节流吗?CSS 也可以防止按钮重复点击
举个例子:一个保存按钮,为了避免重复提交或者服务器考虑,往往需要对点击行为做一定的限制,比如只允许每300ms提交一次,这时候我想大部分同学都会到网上直接拷贝一段throttle函数,或者直接引用lodash工具库

XboxYan34阅读 2.3k评论 2

封面图

关注我,每周输出一篇前端技术文章,

893 声望
1.2k 粉丝
宣传栏