Summary of common MongoDB commands
- 2020-05-24 06:25:12
- OfStack
Simply add, delete, change and check the data
Specify whether to display or not to display a field in the query result
For example, we want to find all the data in the lessons collection, but we don't want to include the slides field in the return result; Because slides is a huge array of images in base64, it affects the reading of the query results.
So we can follow query object with one parameter. As follows:
db.lessons.find({}, {slides:0});
You can also specify which fields to display:
db.bios.find(
{ },
{ name: 1, contribs: 1, _id: 0 }
)
The comparison operation is larger than and smaller than
We want to query the data records between startTime and endTime, and we want Content to contain the Numbers 1 to 5.
db.wemessages.find( {$and: [
{ CreateTime: {$gt: (startTime.getTime()/1000).toString()} },
{ CreateTime: {$lt: (endTime.getTime()/1000).toString()} },
{ Content: {$in: ['1','2','3','4','5']} }
]}
);
Here we use the $and logical operator, and the $gt, $lt, $in comparison operators.
About MongoDB operator, see: http: / / docs mongodb. org/manual/reference/operator/query /
Updates some properties of a record
Using $set means updating only the specified field without modifying the other fields, which is our general intent.
db.lessons.update({}, {$set:{'course_id':'c.101'}});
Update multiple records
Use {multi: true}
db.lessons.update({}, {$set:{'course_id':'c.101'}}, {multi: true});
db.muusers.update({username: 'tom'}, {$set: {mobile: '6508639713'}}, {multi: true});
Sort query results
Use the sort method
db.muusers.find().sort({firsttime: -1});
Minus 1 descending, 1 ascending
View the query results cleanly
Use the pretty method
db.lessons.find({lesson: 1}, {slides: 0, mp3voice:0, wavvoice:0, wavvoicemin: 0}).pretty();
View a record that does not have a field
Using the $exists
db.questions.find({'sequence_id': 1, 'pngslide': {$exists: false}});
db.mycollection.find( { "price" : { "$exists" : false } } )
Limit the number of query results limit and how many records skip is skipped to begin with
Use limit and skip
db.translation_memory.find({mp3voice: {$exists: false}}, null, {limit: 100});
Delete everything in collection
Use the remove method of collection
db.collection.remove();
Get the length of one field in collection
Chain calls
db.lessons.find({lesson: 1}).toArray()[0].slides.join('').length
Collection operation
Renamed collection
Use the renameCollection method
db.quizzes.renameCollection('questions');
Delete 1 field
Using the $unset
db.questions.update({}, {$unset: {quiz_name:1}}, {multi: true});
db.learning_progress.update({}, {$unset: {lesson:1}}, {multi: true});
db.lessons.update({}, {$unset: {wavvoice:1, wavvoicemin:1}}, {multi: true});
Modify the name of a field
Using the $rename
db.students.update( { _id: 1 }, { $rename: { 'nickname': 'alias', 'cell': 'mobile' } } )
Note: some keywords of MongoDB cannot be used as the name of Collection, for example: group.
That's all for this article, I hope you enjoy it.