MongoDB Common Command Summary _mongodb

Source: Internet
Author: User
Tags logical operators mongodb

Simple additions and deletions to check the data

Specify the display in the query results or do not display a field

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 base64-represented images that affect the reading of query results.
Then we can follow the query object with an argument. As follows:

Copy Code code as follows:

Db.lessons.find ({}, {slides:0});

You can also explicitly specify which fields to display:

Copy Code code as follows:

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

Comparison operations – greater than and less than

We want to query the time range between StartTime and Endtime data records, while requiring content to be the number 1 to 5.

Copy Code code as follows:

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 $and logical operators, and $GT, $LT, $in comparison operators.

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

Update some properties of a record

Using the $set means that only the specified fields are updated without modifying other fields, which we would normally be.

Copy Code code as follows:

Db.lessons.update ({}, {$set: {' course_id ': ' C.101 '}});

Update more than one record

Use {multi:true}

Copy Code code as follows:

Db.lessons.update ({}, {$set: {' course_id ': ' C.101 '}}, {multi:true});
Db.muusers.update ({username: ' Tom '}, {$set: {mobile: ' 6508639713 '}}, {multi:true});

Sort Query Results

Using the Sort method

Copy Code code as follows:

Db.muusers.find (). Sort ({firsttime:-1});

-1 Descending, 1 ascending

View query results with refreshing

Using the Pretty method

Copy Code code as follows:

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

View records that do not exist in a field

Use $exists

Copy Code code as follows:

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

Limit the number of query results limit and skip the number of records that started skip

Using Limit and Skip

Copy Code code as follows:

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

Delete all content in collection

Remove method Using Collection

Copy Code code as follows:

Db.collection.remove ();

Get the length of a field in collection

Chained call

Copy Code code as follows:

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

Collection operation

Renamed Collection

Using the Renamecollection method

Copy Code code as follows:

Db.quizzes.renameCollection (' questions ');

Delete a field

Use $unset

Copy Code code as follows:

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

Copy Code code as follows:

Db.students.update ({_id:1}, {$rename: {' nickname ': ' Alias ', ' cell ': ' Mobile '}})

Note: Some MongoDB keywords cannot be used as collection names, for example, group.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.