[Sorting] Summary of common MongoDB commands and common mongodb commands

Source: Internet
Author: User
Tags comparison table install mongodb mongodb commands mongodb tutorial

[Sorting] Summary of common MongoDB commands and 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.

 

Sort and add a series of articles on MongoDB technical knowledge 

1 basic knowledge for getting started with MongoDB

2MongoDB getting started tutorial (including installation, Common commands, related concepts, usage skills, and Common Operations)

3 MongoDB getting started

Introduction to common O & M technologies in the 4MongoDB getting started tutorial

C # driver operation instance in 5MongoDB getting started tutorial

Step 6 MongoDB master-slave replication Configuration

Introduction to aggregation and cursor operations in the 7MongoDB getting started tutorial

An Introduction to MongoDB database addition, deletion, query, modification, and so on

9 MongoDB getting started tutorial-index Operation Analysis

10MongoDB getting started tutorial-MongoDB database installation diagram in Windows

11MongoDB connection timeout caused by no index created in the query field case study

12. Solution to excessive MongoDB log files

Differences between Community edition and Enterprise Edition of 13MongoDB

14 mongoDB Chinese community sponsors will show you how to learn mongoDB

15 Analysis on MongoDB database performance bottlenecks

16 MongoDB paging query methods and Performance

Cluster architecture Implementation of 17MongoDB shard Storage

18Mongodb batch Delete gridfs file instances

19Mongodb add and remove Shard Server instances

Add and remove Arbiter node instances in apsaradb for MongoDB

Tutorial on installing and configuring MongoDB in 21CentOS

22MongoDB modifies or deletes a file's domain attribute instance

MongoDB basic operations in 23Python: connecting to and querying instances

Example of exporting query results to files in 24MongoDB

Notes for creating indexes in MongoDB

Some pitfalls in 26MongoDB (not recommended)

27. Add a user permission to MongoDB

Simple installation and basic operations of MongoDB in 28Linux

Basic management commands for the 29MongoDB tutorial

Aggregation of 30MongoDB tutorial (count, distinct, and group)

Index introduction in 31 MongoDB tutorial

Data operation instance in the 32MongoDB tutorial

33MongoDB Basics

Query operation instances in the 34MongoDB tutorial

35 MongoDB series tutorial (4): Set User Access Permissions

36MongoDB series tutorial (8): GridFS storage details

37MongoDB features and advantages

38MongoDB introduction five features

39MongoDB series tutorial (6): java operations on mongodb instances

40MongoDB series tutorial (7): Detailed description of MongoDb Data Structure

41MongoDB series tutorial (5): Comparison of mongo syntax and mysql syntax

42MongoDB series tutorial (II): MongoDB Introduction

43MongoDB series tutorial (1): NoSQL Origin

Introduction to MapReduce in 44MongoDB

45 MongoDB series tutorial (3): download and install MongoDB in Windows

46. MongoDB backup methods

47 summary of common MongoDB commands

48MongoDB and MySQL operations comparison table and differences

49MongoDB security configuration details

Introduction and use of bson in 50MongoDB

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.