MOngoDB delete statement

Delete () to delete

  1. Delete a collectiondb.collection.deleteOne()
  2. Delete multiple collectionsdb.collection.deletMany();

Remove () to delete

  1. Delete all data of name: li SiDb. Student. Remove ({name: "bill"});
  2. Delete only one sex: Delete only one male dataDb. Student. Remove ({sex: "male"}, true);
  3. Delete alldb.student.remove({});

Database fake delete

Sometimes when a user deletes something, the requirement is to hide the data and not actually delete it from the database. At this time, fake deletion is used, for example, this is zhang Sanfa’s two weibo posts:

db.student.insert([
    {name:"Zhang",content:"In a good mood today.",isDel:0},
    {name:"Zhang",content:"I'm in an ordinary mood today.",isDel:0},
]);
Copy the code

When the user adds two pieces of data, only the latter one is retained and the former one is deleted. In this case, fake delete is used. When the user adds data, a field isDel:0 is added

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

isDel:0

Therefore, the name and isDel criteria should be filtered in the query

db.student.find({name:"Zhang",isDel:0});
Copy the code

The data that is not deleted is displayed:

Then you can implement fake delete.

Batch data operation and modification

  1. Insert 10000 documents into the collection
var arr= [];
for(var i=0; i<10000; i++){ arr.push({counter:i}); } db.demos.insert(arr); db.demos.find();Copy the code
  1. Query the document whose counter is 666 in the demosdb.demos.find({counter:666});
  2. Query documents whose counter is less than 66 in the demosdb.demos.find({counter:{$lt:666}});
  3. Query the documentation for counter large T666 in the demosdb.demos.find({counter:{$gt:666}});
  4. Query documents in demos where counter is greater than 66 and less than 666db.demos.find({counter:{$gt:66, $lt:666}});
  5. Word 1 through 20 in the Chashi demos collectiondb.demos.find().limit(10);
  6. Skip from 21 to 30 items in the demos and limit how many items to query at a time
db.demos.find().skip(0).limit(10); Db.demos. Find ().skip(10).limit(10); db.demos. Db.demos. Find ().skip(20).limit(10); db.demos. // page 3 start with 20 items per 10 itemsCopy the code

Document relationships in collections

  1. One to one: for example, man and his ID card husband and wife
  2. One to many: for example: parents and children users and objects
  3. Many to many: for example, teachers and students

One to one

In the form of embedded documents,

// one-to-one db.aandb. insert([{name:"Yang guo",wife:{name:"Little Dragon Lady",sex:"Female"},sex:"Male"},
  {name:"Yang guo",wife:{name:"Little Dragon Lady",sex:"Female"},sex:"Male"}
])

db.aAndb.find();
Copy the code

More than a pair of

It is implemented either as an embedded document or as a collection

Db.weibo. Insert ([{weibo:"The world is so big, I want to see it."},
{weibo:"I want to be a Web developer!!"}
])

db.weibo.find();
Copy the code

Add comments

db.comments.insert([
{
weibo_id: ObjectId("5bdd89e06a5e78f4cfc2b9c8"),
list:[
   "Do you have any money?"."Alone?? Go??"."Come on!!
]
},
{
weibo_id: ObjectId("5bdd89e06a5e78f4cfc2b9c9"),
list:[
   "Then you need to learn HTML."."You have to learn CSS."."Come on!!]}]); db.comments.find();Copy the code

Querying one-to-many

var weibo_id= db.weibo.findOne({"weibo" : "The world is so big, I want to see it."})._id;
db.comments.find({weibo_id: weibo_id});
Copy the code

Many-to-many relationships

For example, students and teachers can connect through multiple documents,

Db.teachers. insert([{name: [{name:]);"Chinese teacher",
  teacher_id: 1,
  student_id:[
     1001,
     1002,
     1003
  ]
  },
{
  name:"Math teacher",
  teacher_id: 2,
  student_id:[
     1001,
     1002,
     1003
  ]
  },
{
  name:"English teacher", teacher_id: 3, student_id:[ 1001, 1002, 1003 ] } ]) db.teachers.find(); Db.students. insert([{name:"Xiao Ming",
  student_id: 1001,
  teacher_id:[
     1,
     2,
     3
  ]
  },
{
  name:"Little red",
  student_id: 1002,
  teacher_id:[
     1,
     2,
     3
  ]
  },
{
  name:"Xiao gang",
  student_id: 1003,
  teacher_id:[
     1,
     2,
     3
  ]
 }
])

db.students.find();
db.teachers.find();
Copy the code

Sorting and indexing

Sorting:

Sort () can be used to specify the sort of documents, and an object needs to be passed inside sort() to specify the sort of documents, where 1 indicates the ascending order and -1 indicates the descending limit skip sort order can be changed at any time. It adjusts automatically at runtime. You don’t want it to sort by ID by default you want it to sort by salary

Db.section.find ().sort({wages:1}); db.section.find().sort({wages:1}); Db.section.find ().sort({_id: -1}); db.section.find().sort({_id: -1}); db.section.find().sort({_id: -1});Copy the code

Index:

Show part of the content of the field or extract part of the content of the field in the query, you can set the query result projection in the second parameter

Index: find({search criteria}, {search scope (1 show 0 hide)}) note: _id if not set default is 1 (show) can be manually hidden

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

Db.section. find({}, {name: 1, _id: 0, wages: 1}); `Copy the code