[Sorting] Summary of common MongoDB commands

Source: Internet
Author: User
Tags mongodb commands

[Sorting] Summary of common MongoDB commands

Summary of common MongoDB commands

 

Simple addition, deletion, modification, and query data

Specify whether to display or not to display a field in the query result.


For example, we want to search for all the data in the lessons collection, but do not want to include the slides field in the returned result. Because slides is a huge array of pictures represented by base64, reading query results is affected.

Then we can keep up with a parameter after the query object. As follows:
db.lessons.find({}, {slides:0});

You can also specify the fields to display:

db.bios.find({ },{ name: 1, contribs: 1, _id: 0 })

Comparison operation-greater than or less

We want to query the data records in the time range between startTime and endTime, And the Content must be 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 comparison operator $ gt, $ lt, $ in.

For MongoDB operators, see: http://docs.mongodb.org/manual/reference/operator/query/

Updates certain attributes of a record.

$ Set indicates that only the specified field is updated without modifying other fields. This is usually the intention.

db.lessons.update({}, {$set:{'course_id':'c.101'}});

Update multiple records

Use {multi: true}, see: http://www.lai18.com/content/411017.html

db.lessons.update({}, {$set:{'course_id':'c.101'}}, {multi: true});db.muusers.update({username: 'tom'}, {$set: {mobile: '6508639713'}}, {multi: true});

Sort query results

Use sort

db.muusers.find().sort({firsttime: -1});

-1 in descending order and 1 in ascending order

Refreshing query results

Use the pretty Method

db.lessons.find({lesson: 1}, {slides: 0, mp3voice:0, wavvoice:0, wavvoicemin: 0}).pretty();

View records without a field

Use $ exists

db.questions.find({'sequence_id': 1, 'pngslide': {$exists: false}});db.mycollection.find( { "price" : { "$exists" : false } } )

Limit the number of query results to limit and how many Record Skips are skipped

Use limit and skip

db.translation_memory.find({mp3voice: {$exists: false}}, null, {limit: 100});

Delete all contents in the collection.

Use the remove method of collection

db.collection.remove();

Obtain the length of a field in the collection.

Chain call

db.lessons.find({lesson: 1}).toArray()[0].slides.join('').length

Collection operations

Rename collection


Use the renameCollection Method

db.quizzes.renameCollection('questions');

Delete A Field

Use $ 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

Use $ rename

db.students.update( { _id: 1 }, { $rename: { 'nickname': 'alias', 'cell': 'mobile' } } )

Note: Some MongoDB keywords cannot be used as Collection names, such as group.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.