MongoDB Series - General Command Summary

1. Create a database
use DATABASE_NAME
2. Check all databases
show dbs
3. Delete the database
db.dropDatabase()
4. Delete the document
db.collection.drop()
5. show all documents
show collections
6. Create a document
db.createCollection("teacher")
7. Insert the document
db.teacher.insert({
    title: '我是JAVA开发工程师', 
    description: 'JAVA 是世界上最好的语言',
    by: '联通智网',
    url: 'http://www.java.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})
8. Update Document
db.teacher.update({"title":"我是JAVA开发工程师"},{$set : {"title":"我是直男"}})

db.teacher.update({"by" : {$in : ["联通智网"]}}, {$set: {"url" : "www.xxx.com"}}, {multi : true})
9. Review the document content
db.teacher.find()
10. Delete the contents of the document
db.teacher.remove({"_id":ObjectId("5e1822c462468a2aa45576ac")})
11. query document content

Here Insert Picture DescriptionHere Insert Picture DescriptionIf you want to get "col" collection "likes" data greater than 100, you can use the following command:

db.col.find({likes : {$gt : 100}})

Conditions query:

db.teacher.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

If you want to get "col" collection title as String data, you can use the following command:

db.col.find({"title" : {$type : 2}})
或
db.col.find({"title" : {$type : 'string'}})
12.Limit and Skip methods
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
13. Sorting

Used in MongoDB sort () method to sort the data, sort () method of sorting parameters can be specified by the fields, and -1 and using a specified sort of way, in which 1 is ascending, descending and -1 for arrangement.

db.COLLECTION_NAME.find().sort({KEY:1})
14. Index

The index is usually able to greatly improve the efficiency of the query, if there is no index, MongoDB has to scan each file in the collection when reading data and select those records that match the query criteria.

This query scans the whole collection efficiency is very low, especially when dealing with large amounts of data, queries can take tens of seconds or even minutes, the performance of this site is very deadly.

Index is a special data structure, a set of index data stored in a read easily traversed, the index value is a structure of a database table in one or more columns to sort

db.collection.createIndex(keys, options)

Grammar Key index field value you want to create an index is created to specify ascending, descending if you want to create an index of -1 can be specified.
Here Insert Picture Descriptiondb.values.createIndex({open: 1, close: 1}, {background: true})
By adding background when creating an index: true option, make creating work in the background

15. A polymerization

MongoDB polymerized (Aggregate) is mainly used for processing data (such as statistical averages, sums, etc.), and returns the data calculated results. Somewhat similar sql statement count (*).

db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

E.g:

db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{
   "result" : [
      {
         "_id" : "runoob.com",
         "num_tutorial" : 2
      },
      {
         "_id" : "Neo4j",
         "num_tutorial" : 1
      }
   ],
   "ok" : 1
}

The above examples similar sql statement:select by_user, count(*) from mycol group by by_user

In the above example, we grouped data field by_user field, and calculating the sum of the same values ​​by_user field.

The following table shows some aggregation of expression:
Here Insert Picture Descriptionpipeline concept

Unix and Linux pipe is typically used to output the current command as a command parameters.

Polymerization MongoDB MongoDB document after the pipeline a pipeline processed results to the next pipeline processing. Pipeline operation can be repeated.

Expression: document processing input and output. Expression is stateless, the polymerization can be used to calculate the current document pipeline, can not handle other documents.

Here we introduce several commonly used operating polymeric framework:

  • $ Project: to modify the structure of the input document. Can be used to rename, add or remove fields, it can also be used to create nested calculations and documentation.
  • m a t c h match: used to filter data, only the output of qualified documents. match using standard MongoDB query operation.
  • $ Limit: to limit the number of documents that the polymerization MongoDB return pipe.
  • $ Skip: skip a specified number of documents in the polymerization conduit, and return the rest of the document.
  • $ Unwind: a split the document into a plurality of array type field, contains a value for each array.
  • $ Group: a collection of documents in the packet, can be used for statistical results.
  • $ Sort: the sort output after the input document.
  • $ GeoNear: output close to the orderly documentation of a geographical location.

Examples pipeline operator

  1. $ Project examples
db.article.aggregate(
    { $project : {
        title : 1 ,
        author : 1 ,
    }}
 );

In this case the result would still only _id, tilte and author of three fields, by default _id field is included, if it is not included _id, then it can be:

db.article.aggregate(
    { $project : {
        _id : 0 ,
        title : 1 ,
        author : 1
    }});
  1. $ Match examples
db.articles.aggregate( [
    { $match : { score : { $gt : 70, $lte : 90 } } },
    { $group: { _id: null, count: { $sum: 1 } } }
] );

m a t c h 70 90 for acquiring recording match score is greater than or equal to less than 7090 records, and then to the next stage of qualifying group pipeline operator for processing.

  1. $ Skip examples
db.article.aggregate({ $skip : 5 });

After $ Skip processing pipeline operator, the first five documents are "filtered" out.

Resources:
rookie Tutorial

Published 215 original articles · won praise 135 · Views 1.14 million +

Guess you like

Origin blog.csdn.net/weinichendian/article/details/103922070