4

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


九旬
1.1k 声望1.2k 粉丝